Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Different ways to compose variables from other variables in Ansible
---
- name: Compose variables and merge lists
hosts: localhost
connection: local
gather_facts: no
vars:
api_versions:
- 1
- 2
- 3
- 5
api_url: "https://api.example.com"
api_resource: "resource_num_"
api_resources: []
api_urls: []
pre_tasks:
- name: Compose the list of api_urls
set_fact:
# [A] = [A] + [item]
api_urls: "{{ api_urls }} + [ '{{ api_url }}/v{{ item }}' ]"
api_resources: "{{ api_resources }} + [ '{{ api_resource }}{{ item }}' ]"
loop: "{{ api_versions }}"
tasks:
- name: Show the list of api_urls
debug:
msg: "{{ item }}"
loop: "{{ api_urls }}"
- name: Show the list of api_resources
debug:
msg: "{{ item }}"
loop: "{{ api_resources }}"
- name: Show the WHAT resources get from WHERE
debug:
msg: "GET: {{ item.1 }} FROM: {{ item.0 }}"
# Merge api_urls and api_resources lists using the zip() filter
loop: "{{ api_urls | zip(api_resources) | list }}"
$ ansible-playbook compose_variables.yml
PLAY [Compose variables and merge lists] *****************************************************************************************************************************************************************************************************
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 [Show the list of api_resources] ************************************************************************************************************************************************************************************************************
ok: [localhost] => (item=resource_num_1) =>
msg: resource_num_1
ok: [localhost] => (item=resource_num_2) =>
msg: resource_num_2
ok: [localhost] => (item=resource_num_3) =>
msg: resource_num_3
ok: [localhost] => (item=resource_num_5) =>
msg: resource_num_5
TASK [Show the WHAT resources get from WHERE] ************************************************************************************************************************************************************************************************
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=4 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