Last active
February 21, 2021 10:52
-
-
Save juanje/4cf378efcb0748ad005fe52ea42212f6 to your computer and use it in GitHub Desktop.
Different ways to compose variables from other variables in Ansible
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
| --- | |
| - hosts: localhost | |
| connection: local | |
| gather_facts: no | |
| vars: | |
| api_versions: | |
| - 1 | |
| - 2 | |
| - 3 | |
| - 5 | |
| api_url: 'https://api.example.com' | |
| api_urls: [] | |
| pre_tasks: | |
| - name: Compose the list of api_urls | |
| set_fact: | |
| api_urls: "{{ api_urls }} + [ '{{ api_url }}/v{{ item }}' ]" | |
| loop: "{{ api_versions }}" | |
| tasks: | |
| - name: Show the list of api_urls | |
| debug: | |
| msg: '{{ item }}' | |
| loop: "{{ api_urls }}" |
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
| $ ansible-playbook compose_variables.yml | |
| PLAY [localhost] ********************************************************************************************************************************************************************************************************************************* | |
| TASK [Compose the list of api_urls] ************************************************************************************************************************************************************************************************************** | |
| ok: [localhost] => (item=1) | |
| ok: [localhost] => (item=2) | |
| ok: [localhost] => (item=3) | |
| ok: [localhost] => (item=5) | |
| TASK [Show the list of api_urls] ***************************************************************************************************************************************************************************************************************** | |
| ok: [localhost] => (item=https://api.example.com/v1) => { | |
| "msg": "https://api.example.com/v1" | |
| } | |
| ok: [localhost] => (item=https://api.example.com/v2) => { | |
| "msg": "https://api.example.com/v2" | |
| } | |
| ok: [localhost] => (item=https://api.example.com/v3) => { | |
| "msg": "https://api.example.com/v3" | |
| } | |
| ok: [localhost] => (item=https://api.example.com/v5) => { | |
| "msg": "https://api.example.com/v5" | |
| } | |
| TASK [Ensure the toolbox image is at local directory] ******************************************************************************************************************************************************************************************** | |
| ok: [localhost] => (item=['https://api.example.com/v1', 'resource_num_1']) => { | |
| "msg": "GET: resource_num_1 FROM: https://api.example.com/v1" | |
| } | |
| ok: [localhost] => (item=['https://api.example.com/v2', 'resource_num_2']) => { | |
| "msg": "GET: resource_num_2 FROM: https://api.example.com/v2" | |
| } | |
| ok: [localhost] => (item=['https://api.example.com/v3', 'resource_num_3']) => { | |
| "msg": "GET: resource_num_3 FROM: https://api.example.com/v3" | |
| } | |
| ok: [localhost] => (item=['https://api.example.com/v5', 'resource_num_5']) => { | |
| "msg": "GET: resource_num_5 FROM: https://api.example.com/v5" | |
| } | |
| PLAY RECAP *************************************************************************************************************************************************************************************************************************************** | |
| localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment