Skip to content

Instantly share code, notes, and snippets.

@juanje
Last active February 21, 2021 10:52
Show Gist options
  • Select an option

  • Save juanje/4cf378efcb0748ad005fe52ea42212f6 to your computer and use it in GitHub Desktop.

Select an option

Save juanje/4cf378efcb0748ad005fe52ea42212f6 to your computer and use it in GitHub Desktop.
Different ways to compose variables from other variables in Ansible
---
- hosts: 127.0.0.1
connection: local
gather_facts: false
vars:
fedora_versions:
- 29
- 31
- 33
registry_url: 'registry.fedoraproject.org'
toolbox_image: 'fedora-toolbox'
toolbox_dirs: []
registry_urls: []
pre_tasks:
- set_fact:
toolbox_dirs: "{{ toolbox_dirs }} + [ '{{ toolbox_image }}-{{ item }}' ]"
registry_urls: "{{ registry_urls }} + [ '{{ registry_url }}/f{{ item }}/{{ toolbox_image }}:{{item }}' ]"
loop: "{{ fedora_versions }}"
tasks:
- name: Ensure a directory exist to store the toolbox images
debug:
msg: 'mkdir ./{{ item }}'
loop: "{{ toolbox_dirs }}"
- name: Ensure the toolbox image is at local directory
debug:
msg: "skopeo copy docker://{{ item.0 }} dir:{{ item.1 }}"
loop: "{{ registry_urls|zip(toolbox_dirs)|list }}"
$ ansible-playbook -i 127.0.0.1, compose_variables.yml
PLAY [127.0.0.1] *********************************************************************************************************************************************************************************************************************************
TASK [set_fact] **********************************************************************************************************************************************************************************************************************************
ok: [127.0.0.1] => (item=29)
ok: [127.0.0.1] => (item=31)
ok: [127.0.0.1] => (item=33)
TASK [Ensure a directory exist to store the toolbox images] **************************************************************************************************************************************************************************************
ok: [127.0.0.1] => (item=fedora-toolbox-29) => {
"msg": "mkdir ./fedora-toolbox-29"
}
ok: [127.0.0.1] => (item=fedora-toolbox-31) => {
"msg": "mkdir ./fedora-toolbox-31"
}
ok: [127.0.0.1] => (item=fedora-toolbox-33) => {
"msg": "mkdir ./fedora-toolbox-33"
}
TASK [Ensure the toolbox image is at local directory] ********************************************************************************************************************************************************************************************
ok: [127.0.0.1] => (item=['registry.fedoraproject.org/f29/fedora-toolbox:29', 'fedora-toolbox-29']) => {
"msg": "skopeo copy docker://registry.fedoraproject.org/f29/fedora-toolbox:29 dir:fedora-toolbox-29"
}
ok: [127.0.0.1] => (item=['registry.fedoraproject.org/f31/fedora-toolbox:31', 'fedora-toolbox-31']) => {
"msg": "skopeo copy docker://registry.fedoraproject.org/f31/fedora-toolbox:31 dir:fedora-toolbox-31"
}
ok: [127.0.0.1] => (item=['registry.fedoraproject.org/f33/fedora-toolbox:33', 'fedora-toolbox-33']) => {
"msg": "skopeo copy docker://registry.fedoraproject.org/f33/fedora-toolbox:33 dir:fedora-toolbox-33"
}
PLAY RECAP ***************************************************************************************************************************************************************************************************************************************
127.0.0.1 : 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