Last active
February 4, 2020 22:12
-
-
Save sivel/4c2740abc4a6c9fd254f5ec87db38187 to your computer and use it in GitHub Desktop.
Ansible playbook showcasing conversion from with_X loops to loop using filters instead of lookup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --- | |
| - name: Playbook showcasing conversion from with_X loops to loop+filters | |
| hosts: localhost | |
| gather_facts: false | |
| vars: | |
| items: | |
| - | |
| - foo | |
| - | |
| - bar | |
| list: | |
| - foo | |
| - bar | |
| together: | |
| one: | |
| - foo | |
| - bar | |
| two: | |
| - baz | |
| - qux | |
| dictionary: | |
| foo: bar | |
| baz: qux | |
| list_one: | |
| - 1 | |
| - 2 | |
| - 3 | |
| list_two: | |
| - a | |
| - b | |
| list_three: | |
| - orange | |
| - apple | |
| - banana | |
| users: | |
| - name: alice | |
| authorized: | |
| - /tmp/alice/onekey.pub | |
| - /tmp/alice/twokey.pub | |
| mysql: | |
| password: mysql-password | |
| hosts: | |
| - "%" | |
| - "127.0.0.1" | |
| - "::1" | |
| - "localhost" | |
| privs: | |
| - "*.*:SELECT" | |
| - "DB1.*:ALL" | |
| groups: | |
| - wheel | |
| - name: bob | |
| authorized: | |
| - /tmp/bob/id_rsa.pub | |
| mysql: | |
| password: other-mysql-password | |
| hosts: | |
| - "db1" | |
| privs: | |
| - "*.*:SELECT" | |
| - "DB2.*:ALL" | |
| tasks: | |
| - name: with_items | |
| debug: | |
| msg: "{{ item }}" | |
| with_items: "{{ items }}" | |
| tags: items | |
| - name: with_items -> loop | |
| debug: | |
| msg: "{{ item }}" | |
| loop: "{{ items|flatten(levels=1) }}" | |
| tags: items | |
| - name: with_indexed_items | |
| debug: | |
| msg: "{{ item.0 }} - {{ item.1 }}" | |
| with_indexed_items: "{{ items }}" | |
| tags: indexed_items | |
| - name: with_indexed_items -> loop | |
| debug: | |
| msg: "{{ index }} - {{ item }}" | |
| loop: "{{ items|flatten(levels=1) }}" | |
| loop_control: | |
| index_var: index | |
| tags: indexed_items | |
| - name: with_flattened | |
| debug: | |
| msg: "{{ item }}" | |
| with_flattened: "{{ items }}" | |
| tags: flatten | |
| - name: with_flattened -> loop | |
| debug: | |
| msg: "{{ item }}" | |
| loop: "{{ items|flatten }}" | |
| tags: flatten | |
| - name: with_list | |
| debug: | |
| msg: "{{ item }}" | |
| with_list: "{{ list }}" | |
| tags: list | |
| - name: with_list -> loop | |
| debug: | |
| msg: "{{ item }}" | |
| loop: "{{ list }}" | |
| tags: list | |
| - name: with_together | |
| debug: | |
| msg: "{{ item.0 }} - {{ item.1 }}" | |
| with_together: | |
| - "{{ together.one }}" | |
| - "{{ together.two }}" | |
| tags: together | |
| - name: with_together -> loop | |
| debug: | |
| msg: "{{ item.0 }} - {{ item.1 }}" | |
| loop: "{{ together.one|zip(together.two)|list }}" | |
| tags: together | |
| - name: with_dict | |
| debug: | |
| msg: "{{ item.key }} - {{ item.value }}" | |
| with_dict: "{{ dictionary }}" | |
| tags: dict | |
| - name: with_dict -> loop (option 1) | |
| debug: | |
| msg: "{{ item.key }} - {{ item.value }}" | |
| loop: "{{ dictionary|dict2items }}" | |
| tags: dict | |
| - name: with_dict -> loop (option 2) | |
| debug: | |
| msg: "{{ item.0 }} - {{ item.1 }}" | |
| loop: "{{ dictionary|dictsort }}" | |
| tags: dict | |
| - name: with_sequence | |
| debug: | |
| msg: "{{ item }}" | |
| with_sequence: start=0 end=4 stride=2 format=testuser%02x | |
| tags: sequence | |
| - name: with_sequence -> loop | |
| debug: | |
| msg: "{{ 'testuser%02x'|format(item) }}" | |
| # range is exclusive of the end point | |
| loop: "{{ range(0, 4 + 1, 2)|list }}" | |
| tags: sequence | |
| - name: with_subelements | |
| debug: | |
| msg: "{{ item.0.name }} - {{ item.1 }}" | |
| with_subelements: | |
| - "{{ users }}" | |
| - mysql.hosts | |
| tags: subelements | |
| - name: with_subelements -> loop | |
| debug: | |
| msg: "{{ item.0.name }} - {{ item.1 }}" | |
| loop: "{{ users|subelements('mysql.hosts') }}" | |
| tags: subelements | |
| - name: with_cartesian | |
| debug: | |
| msg: "{{ item.0 }} - {{ item.1 }} - {{ item.2 }}" | |
| with_cartesian: | |
| - "{{ list_one }}" | |
| - "{{ list_two }}" | |
| - "{{ list_three }}" | |
| tags: cartesian | |
| - name: with_cartesian -> loop | |
| debug: | |
| msg: "{{ item.0 }} - {{ item.1 }} - {{ item.2 }}" | |
| loop: "{{ list_one|product(list_two, list_three)|list }}" | |
| tags: cartesian | |
| - name: with_nested | |
| debug: | |
| msg: "{{ item.0 }} - {{ item.1 }}" | |
| with_nested: | |
| - "{{ list_one }}" | |
| - "{{ list_two }}" | |
| tags: nested | |
| - name: with_nested -> loop | |
| debug: | |
| msg: "{{ item.0 }} - {{ item.1 }}" | |
| loop: "{{ list_one|product(list_two)|list }}" | |
| tags: nested | |
| - name: with_random_choice | |
| debug: | |
| msg: "{{ item }}" | |
| with_random_choice: "{{ list }}" | |
| tags: random | |
| - name: with_random_choice -> loop (Wait! no loop is even needed here) | |
| debug: | |
| msg: "{{ list|random }}" | |
| tags: random |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Using /Users/matt/projects/ansibledev/playbooks/ansible.cfg as config file | |
| PLAY [Playbook showcasing conversion from with_X loops to loop] ****************************************************************************************************************************************************************************** | |
| TASK [with_items] **************************************************************************************************************************************************************************************************************************** | |
| ok: [localhost] => (item=None) => { | |
| "msg": "foo" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "bar" | |
| } | |
| TASK [with_items -> loop] ******************************************************************************************************************************************************************************************************************** | |
| ok: [localhost] => (item=None) => { | |
| "msg": "foo" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "bar" | |
| } | |
| TASK [with_indexed_items] ******************************************************************************************************************************************************************************************************************** | |
| ok: [localhost] => (item=None) => { | |
| "msg": "0 - foo" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "1 - bar" | |
| } | |
| TASK [with_indexed_items -> loop] ************************************************************************************************************************************************************************************************************ | |
| ok: [localhost] => (item=foo) => { | |
| "index": 0, | |
| "msg": "0 - foo" | |
| } | |
| ok: [localhost] => (item=bar) => { | |
| "index": 1, | |
| "msg": "1 - bar" | |
| } | |
| TASK [with_flattened] ************************************************************************************************************************************************************************************************************************ | |
| ok: [localhost] => (item=None) => { | |
| "msg": "foo" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "bar" | |
| } | |
| TASK [with_flattened -> loop] **************************************************************************************************************************************************************************************************************** | |
| ok: [localhost] => (item=None) => { | |
| "msg": "foo" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "bar" | |
| } | |
| TASK [with_list] ***************************************************************************************************************************************************************************************************************************** | |
| ok: [localhost] => (item=None) => { | |
| "msg": "foo" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "bar" | |
| } | |
| TASK [with_list -> loop] ********************************************************************************************************************************************************************************************************************* | |
| ok: [localhost] => (item=None) => { | |
| "msg": "foo" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "bar" | |
| } | |
| TASK [with_together] ************************************************************************************************************************************************************************************************************************* | |
| ok: [localhost] => (item=None) => { | |
| "msg": "foo - baz" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "bar - qux" | |
| } | |
| TASK [with_together -> loop] ***************************************************************************************************************************************************************************************************************** | |
| ok: [localhost] => (item=None) => { | |
| "msg": "foo - baz" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "bar - qux" | |
| } | |
| TASK [with_dict] ***************************************************************************************************************************************************************************************************************************** | |
| ok: [localhost] => (item=None) => { | |
| "msg": "foo - bar" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "baz - qux" | |
| } | |
| TASK [with_dict -> loop (option 1)] ********************************************************************************************************************************************************************************************************** | |
| ok: [localhost] => (item=None) => { | |
| "msg": "foo - bar" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "baz - qux" | |
| } | |
| TASK [with_dict -> loop (option 2)] ********************************************************************************************************************************************************************************************************** | |
| ok: [localhost] => (item=None) => { | |
| "msg": "baz - qux" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "foo - bar" | |
| } | |
| TASK [with_sequence] ************************************************************************************************************************************************************************************************************************* | |
| ok: [localhost] => (item=None) => { | |
| "msg": "testuser00" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "testuser02" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "testuser04" | |
| } | |
| TASK [with_sequence -> loop] ***************************************************************************************************************************************************************************************************************** | |
| ok: [localhost] => (item=None) => { | |
| "msg": "testuser00" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "testuser02" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "testuser04" | |
| } | |
| TASK [with_subelements] ********************************************************************************************************************************************************************************************************************** | |
| ok: [localhost] => (item=None) => { | |
| "msg": "alice - %" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "alice - 127.0.0.1" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "alice - ::1" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "alice - localhost" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "bob - db1" | |
| } | |
| TASK [with_subelements -> loop] ************************************************************************************************************************************************************************************************************** | |
| ok: [localhost] => (item=None) => { | |
| "msg": "alice - %" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "alice - 127.0.0.1" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "alice - ::1" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "alice - localhost" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "bob - db1" | |
| } | |
| TASK [with_cartesian] ************************************************************************************************************************************************************************************************************************ | |
| [[1, 2, 3], ['a', 'b'], ['orange', 'apple', 'banana']] | |
| ok: [localhost] => (item=None) => { | |
| "msg": "1 - a - orange" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "1 - a - apple" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "1 - a - banana" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "1 - b - orange" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "1 - b - apple" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "1 - b - banana" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "2 - a - orange" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "2 - a - apple" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "2 - a - banana" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "2 - b - orange" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "2 - b - apple" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "2 - b - banana" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "3 - a - orange" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "3 - a - apple" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "3 - a - banana" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "3 - b - orange" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "3 - b - apple" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "3 - b - banana" | |
| } | |
| TASK [with_cartesian -> loop] **************************************************************************************************************************************************************************************************************** | |
| ok: [localhost] => (item=None) => { | |
| "msg": "1 - a - orange" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "1 - a - apple" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "1 - a - banana" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "1 - b - orange" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "1 - b - apple" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "1 - b - banana" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "2 - a - orange" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "2 - a - apple" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "2 - a - banana" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "2 - b - orange" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "2 - b - apple" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "2 - b - banana" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "3 - a - orange" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "3 - a - apple" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "3 - a - banana" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "3 - b - orange" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "3 - b - apple" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "3 - b - banana" | |
| } | |
| TASK [with_nested] *************************************************************************************************************************************************************************************************************************** | |
| ok: [localhost] => (item=None) => { | |
| "msg": "1 - a" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "1 - b" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "2 - a" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "2 - b" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "3 - a" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "3 - b" | |
| } | |
| TASK [with_nested -> loop] ******************************************************************************************************************************************************************************************************************* | |
| ok: [localhost] => (item=None) => { | |
| "msg": "1 - a" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "1 - b" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "2 - a" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "2 - b" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "3 - a" | |
| } | |
| ok: [localhost] => (item=None) => { | |
| "msg": "3 - b" | |
| } | |
| TASK [with_fileglob] ************************************************************************************************************************************************************************************************************************* | |
| ok: [localhost] => (item=None) => { | |
| "msg": "/Users/matt/projects/ansibledev/playbooks/with2loop.yml" | |
| } | |
| TASK [with_fileglob -> loop] ***************************************************************************************************************************************************************************************************************** | |
| ok: [localhost] => (item=None) => { | |
| "msg": "/Users/matt/projects/ansibledev/playbooks/with2loop.yml" | |
| } | |
| TASK [with_random_choice] ******************************************************************************************************************************************************************************************************************** | |
| ok: [localhost] => (item=None) => { | |
| "msg": "foo" | |
| } | |
| TASK [with_random_choice -> loop (Wait! no loop is even needed here)] ************************************************************************************************************************************************************************ | |
| ok: [localhost] => { | |
| "msg": "foo" | |
| } | |
| PLAY RECAP *********************************************************************************************************************************************************************************************************************************** | |
| localhost : ok=25 changed=0 unreachable=0 failed=0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment