I’m using stat module in loop to search for a file when defined.
-
name: Check for .pub file
stat:
path: “{{ playbook_dir }}/…/vars/{{ item.name }}.pub”
loop: “{{ UserAddList.add_users }}”
register: file_details
when: item.authorized_keys is defined and item.authorized_keys != “”
no_log: true -
name: Fail if .pub key doesn’t exist
fail:
msg: ‘{{ item.item.name }} does not have to public key {{ item.item.name }}.pub in the path {{ item.item.authorized_keys }} file’
when: item.stat.exists == false or item.stat.exists is not defined
loop_control:
label: “{{ item.stat }}”
with_items: “{{ file_details.results }}”
Input file:
add_users:
- name: test1_123
group: test123
password: test1_newcdsaf
authorized_keys: “{{ playbook_dir }}/…/vars/test1_123.pub” - name: test1_1234
group: test234
password: test1_newcdsaf
authorized_keys: “”
My input file will have an user entry with optional authorized keys field
Conditions to fail.
The playbook has to fail when authorized_keys is defined and the .pub key is present on in the location. I’m using stat module to search for the file.
The playbook should not fail when authorized_keys field is empty as it is valid case.
right now my playbook is failing when authorized_keys is empty. This is happening because of the loop.
for the item where the authorized_keys is not defined, I’m seeing the below output.
“skip_reason”: “Conditional result was False”,
“skipped”: true
When authorized_keys is defined.I’m seeing the stat variable in output
“stat”:
“exists”: false
Please provide any suggestions to fix this.
Thank you