This has got to be a simple question but I can't seem to find the answer anywhere.
I have the following list:
yum_repo_ip_addrs: ['172.16.130.4', '172.16.130.1']
I want to dynamically create a list called yum_baseurls where I copy in these values into the list along with the rest of the url. The list should ultimately look like this when run successfully:
yum_baseurls:
- "http://172.16.130.4/repos/elrepo-8-x86_64"
- "http://172.16.130.1/repos/elrepo-8-x86_64"
Instead, I'm finding that after the first iteration of my loop it's pasting in the variables literally.
Here's my playbook:
---
- name: Print the list of baseurl IP addresses.
debug:
msg: "{{ yum_repo_ip_addrs }}"
- name: Create the list of baseurls.
set_fact:
yum_baseurls: "{{ yum_baseurls + ['http://{{ item }}/repos/elrepo-{{ ansible_distribution_major_version }}-{{ ansible_userspace_architecture }}'] }}"
with_items:
- "{{ yum_repo_ip_addrs }}"
- name: print the list of baseurls.
debug:
msg: "{{ yum_baseurls }}"
And here's the output I get when I run it:
TASK [yum : Print the list of baseurl IP addresses.] ***************************************************************************************
ok: [ansibletarget3.jnk.sys] => {
"msg": [
"172.16.130.4",
"172.16.130.1"
]
}
TASK [yum : Create the list of baseurls.] **************************************************************************************************
ok: [ansibletarget3.jnk.sys] => (item=172.16.130.4)
ok: [ansibletarget3.jnk.sys] => (item=172.16.130.1)
TASK [yum : print the list of baseurls.] ***************************************************************************************************
ok: [ansibletarget3.jnk.sys] => {
"msg": [
"http://172.16.130.1/repos/elrepo-8-x86_64",
"http://{{ item }}/repos/elrepo-{{ ansible_distribution_major_version }}-{{ ansible_userspace_architecture }}"
]
}
Is there a better way to generate my list?
I'd remove it from the code and put it somewhere into the vars. For example,
yum_repo_ip_addrs: [172.16.130.4, 172.16.130.1]
version: 8
architecture: x86_64
yum_baseurls: |
{% filter from_yaml %}
{% for ip in yum_repo_ip_addrs %}
- http://{{ ip }}/repos/elrepo-{{ version }}-{{ architecture }}
{% endfor %}
{% endfilter %}
gives
yum_baseurls:
- http://172.16.130.4/repos/elrepo-8-x86_64
- http://172.16.130.1/repos/elrepo-8-x86_64
Generally, you can put all variables into the lists
yum_repo_ip_addrs: [172.16.130.4, 172.16.130.1]
version: [8]
architecture: [x86_64]
and iterate them all
yum_baseurls: |
{% filter from_yaml %}
{% for ip in yum_repo_ip_addrs %}
{% for v in version %}
{% for a in architecture %}
- http://{{ ip }}/repos/elrepo-{{ v }}-{{ a }}
{% endfor %}
{% endfor %}
{% endfor %}
{% endfilter %}
If you prefer filters the below declaration gives the same result
yum_baseurls: "{{ ['http://']
| product(yum_repo_ip_addrs)
| product(['/repos/elrepo-'])
| product(version)
| product(['-'])
| product(architecture)
| map('flatten')
| map('join') }}"
Example of a complete playbook for testing
- hosts: localhost
vars:
yum_repo_ip_addrs: [172.16.130.4, 172.16.130.1]
version: [8]
architecture: [x86_64]
yum_baseurls: |
{% filter from_yaml %}
{% for ip in yum_repo_ip_addrs %}
{% for v in version %}
{% for a in architecture %}
- http://{{ ip }}/repos/elrepo-{{ v }}-{{ a }}
{% endfor %}
{% endfor %}
{% endfor %}
{% endfilter %}
yum_baseurl2: "{{ ['http://']
| product(yum_repo_ip_addrs)
| product(['/repos/elrepo-'])
| product(version)
| product(['-'])
| product(architecture)
| map('flatten')
| map('join') }}"
tasks:
- debug:
var: yum_baseurls
- debug:
var: yum_baseurl2