Hi,
I’d like to automatically check all available disk devices if they are partitioned and filter out the ones that aren’t. Later I’d also like to use that list to decide which ones to partition.
I’m trying to achieve this (currently only for /dev/sdb) by using:
- name: Print out unmounted devices
debug: msg=“{{ hostvars[inventory_hostname].ansible_devices.sdb | rejectattr(‘partitions’, ‘equalto’, true) | list }}”
This however returns me an error:
fatal: [host1.com]: FAILED! => {“msg”: “The task includes an option with an undefined variable. The error was: ‘unicode object’ has no attribute ‘partitions’\n\nThe error appears to have been in ‘~/ansible/roles/devices/tasks/main.yml’: line 72, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Print out unmounted devices\n ^ here\n”}
fatal: [host2.com]: FAILED! => {“msg”: “The task includes an option with an undefined variable. The error was: ‘unicode object’ has no attribute ‘partitions’\n\nThe error appears to have been in ‘~/ansible/roles/devices/tasks/main.yml’: line 72, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Print out unmounted devices\n ^ here\n”}
fatal: [host3.com]: FAILED! => {“msg”: “The task includes an option with an undefined variable. The error was: ‘unicode object’ has no attribute ‘partitions’\n\nThe error appears to have been in ‘~/ansible/roles/devices/tasks/main.yml’: line 72, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Print out unmounted devices\n ^ here\n”}
Leaving the “| list” out at the end, it returns me a proper generator object, that I would expect from filtering…
ok: [ncfressnapf-p-jira001.westeurope.cloudapp.azure.com] => {
“msg”: “<generator object select_or_reject at 0x7f4673ac0960>”
}
ok: [ncfressnapf-p-logjira001.westeurope.cloudapp.azure.com] => {
“msg”: “<generator object select_or_reject at 0x7f4673ac0b90>”
}
ok: [ncfressnapf-p-confluence001.westeurope.cloudapp.azure.com] => {
“msg”: “<generator object select_or_reject at 0x7f4673ac0e60>”
}
Q1: Why is that error coming up? Is there a different way I would need to perform this filtering? I used a similar approach for filtering hosts missing ssh keys in the past and it worked well
This snippet however correctly prints out all the information I am looking for.
- name: Print out unmounted devices
debug: msg=“{{ hostvars[inventory_hostname].ansible_devices.sdb.partitions }}”
Q2: As an additional question… Is there a way to use a wildcard search or regex to search over all devices? Something in the lines of:
- name: Print out unmounted devices
debug: msg=“{{ hostvars[inventory_hostname].ansible_devices.sd*.partitions }}”