Cheatsheet
Using with_items and when conditional to
https://stackoverflow.com/questions/32214529/ansible-using-with-items-and-when-conditional-to
- name: Check if the disk is partitioned and also ignore sda
stat: path=/dev/{{item}}1
with_items: disk_var
when: item != 'sda'
register: device_stat
- name: Create GPT partition table
command: /sbin/parted -s /dev/{{ item.item }} mklabel gpt
with_items: "{{ device_stat.results }}"
when:
- not item | skipped
- item.stat.exists == false
inventory
Extract hostname and ip from - name: create cfs
debug:
msg: "{{ groups['workers'] | map('extract',hostvars,'ansible_ssh_host') | list }}"
tags: test
- name: create cfs
debug:
msg: "{{ groups['workers'] | list }}"
tags: test
Combine two separate lists into a list of dictionaries
https://stackoverflow.com/questions/51898227/ansible-how-to-combine-two-separate-lists-into-a-list-of-dictionaries Suppose we have
"ansible_interfaces": [
"ens32",
"ens34"
]
host_ipv4_list:
- "192.168.0.1"
- "192.168.1.1"
We want
host_network_info:
- { "interface": "ens32", "ip": "192.168.0.1" }
- { "interface": "ens34", "ip": "192.168.1.1" }
Solution:
set_fact:
host_network_info: "{{ host_network_info | default([]) + [dict(interface=item[0], ip=item[1])] }}"
loop: "{{ ansible_interfaces | zip(host_ipv4_list) | list }}"