Oxs
(Oxs)
November 13, 2019, 11:00am
1
Hi guys,
I have create a loop in my playbook to set multiple values for lvol and vg.
How I can write my ansible-playbook command with multiple values in a variable?
Example:
ansible-playbook data-fs.yml -i inventory -e vg_name=vg01,vg02 -e lv_name=lv01,lv02
name: Create LV’s
lvol:
vg: “{{ vg_name }}”
lv: “{{ lv_name }}”
size: “{{ lv_size }}”
loop:
{ vg: ’ {{ vg_name }}, lv: ‘{{ vg_name }}’ }
Thanks
vbotka
(Vladimir Botka)
November 13, 2019, 11:56am
2
It's possible to put the extra variables into the lists and use the *zip*
filter. For example
ansible-playbook data-fs.yml -e "{'vg_name': ['vg01','vg02']}" -e
"{'lv_name': ['lv01', 'lv02']}"
- name: Create LV's
lvol:
vg: "{{ item.0 }}"
lv: "{{ item.1 }}"
size: "{{ lv_size }}"
loop: "{{ vg_name|default( )|
zip(lv_name|default( ))|
list }}"
Cheers,
-vlado
Oxs
(Oxs)
November 13, 2019, 6:03pm
3
Thanks for your feedback, work fine but I have change the syntax of the ansible-playbook command:
ansible-playbook data-fs.yml -e ‘{“vg_name”: [“vg0”,“vg02”]}’ -e ‘{“lv_name”: [“lv01”, “lv02”]}’
Last question: How to use the variable data_dir with this loop… or what is the correct syntax?
Name: mount Filesystem
mount:
path: “{{ data_dir }}”
src: /dev/mapper/{{ item.0 }}-{{ item.1 }}
fstype: xfs
state: mounted
opts: default,nosuid,nodev,noexec
loop: "{{ data_dir|default([]) |{{ vg_name|default( )|zip(lv_name|default( )) | list }}
Thanks in advance for your help!
vbotka
(Vladimir Botka)
November 13, 2019, 8:19pm
4
Thanks for your feedback, work fine but I have change the syntax of the
ansible-playbook command:
ansible-playbook data-fs.yml -e '{"vg_name": ["vg0","vg02"]}' -e
'{"lv_name": ["lv01", "lv02"]}'
It's possible to keep the command and *split* the arguments
ansible-playbook data-fs.yml -e vg_name=vg01,vg02 -e lv_name=lv01,lv02
- name: Create LV's
lvol:
vg: "{{ item.0 }}"
lv: "{{ item.1 }}"
size: "{{ lv_size }}"
loop: "{{ vg_name.split(',')|
zip(lv_name.split(','))|
list }}"
Last question: How to use the variable data_dir with this loop.. or what is
the correct syntax?
Name: mount Filesystem
- mount:
path: "{{ data_dir }}"
src: /dev/mapper/{{ item.0 }}-{{ item.1 }}
fstype: xfs
state: mounted
opts: default,nosuid,nodev,noexec
loop: "*{{ data_dir|default( )*|{{
vg_name|default( )|zip(lv_name|default( )) | list }}
The task below will mount the devices "/dev/mapper/vg01-lv01" and
"/dev/mapper/vg02-lv02" to the directory "/lv01" and "/lv02" respectively
- mount:
path: "/{{ item.1 }}"
src: /dev/mapper/{{ item.0 }}-{{ item.1 }}
fstype: xfs
state: mounted
opts: default,nosuid,nodev,noexec
loop: "{{ vg_name.split(',')|
zip(lv_name.split(','))|
list }}"
Try to describe where to mount the devices if this is not what you want.
Cheers,
-vlado
Oxs
(Oxs)
November 14, 2019, 9:15am
5
Hi Vladimir,
Thanks a lot !!! It was what I want with the mount point!
My question was, how I can manage 3 variables in a loop? for example the chown file (path, user and group variable)
have a great day
vbotka
(Vladimir Botka)
November 14, 2019, 9:39am
6
Let's create a list of dictionaries and loop the list. For example
vars:
my_files:
- path: /scratch/project1/test1
owner: admin
group: admin
- path: /scratch/project1/test2
owner: admin
group: admin
tasks:
- name: Create test files
file:
state: "{{ item.state|default('touch') }}"
path: "{{ item.path }}"
owner: "{{ item.owner|default(omit) }}"
group: "{{ item.group|default(omit) }}"
loop: "{{ my_files }}"
Cheers,
-vlado