Last active
May 4, 2018 07:35
-
-
Save jffz/ee355dff87996f29ed41fd5bfd556f17 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Create list using a specific attribute value | |
## Original task | |
- name: "Set fact" | |
set_fact: | |
hdd_list: "{{ hdd_list|default([]) + [item.key] }}" | |
with_dict: "{{ ansible_facts.ansible_devices }}" | |
when: item.value.model == "Virtual disk" | |
## Output | |
"hdd_list": [ | |
"sda", | |
"sdb" | |
] | |
# Create list with multiple items and their attrs | |
## Task | |
- name: "Set fact hdd_perfs" | |
set_fact: | |
hdd_perfs: "{{ hdd_perfs|default([]) + [ {'name': item.item, 'device': item.stdout_lines[1], 'cache_read': item.stdout_lines[2], 'buffer_read': item.stdout_lines[3]} ] }}" | |
#hdd_perfs: "{{ hdd_perfs|default([]) + [item.name] }}" | |
with_items: "{{ disks_perfs.results }}" | |
## Output | |
"hdd_perfs": [ | |
{ | |
"buffer_read": " Timing buffered disk reads: 790 MB in 3.00 seconds = 263.27 MB/sec", | |
"cache_read": " Timing cached reads: 10708 MB in 2.00 seconds = 5358.69 MB/sec", | |
"device": "/dev/sda:", | |
"name": "sda" | |
}, | |
{ | |
"buffer_read": " Timing buffered disk reads: 522 MB in 3.00 seconds = 173.87 MB/sec", | |
"cache_read": " Timing cached reads: 7654 MB in 2.00 seconds = 3829.39 MB/sec", | |
"device": "/dev/sdb:", | |
"name": "sdb" | |
} | |
] | |
# Create dict with attrs | |
# Task | |
- name: add new key / value pairs to dict | |
set_fact: | |
my_dict_var: "{{ my_dict_var|default({}) | combine({item.key: item.value}) }}" | |
with_items: | |
- { key: 'sda', value: {'device': "mondevice",'device2': "mondevice"} } | |
- { key: 'sdb', value: {'device': "mondevice",'device2': "mondevice"} } | |
## Output | |
[host] => { | |
"my_dict_var": { | |
"sda": { | |
"device": "mondevice", | |
"device2": "mondevice" | |
}, | |
"sdb": { | |
"device": "mondevice", | |
"device2": "mondevice" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment