I have written some Ansible code to put users from the users.yaml file in the variable 'nexus_local_users' to be used by a role. When doing this a random password is created and written to the 'credentials/{{ user }}/password.txt' file.
I'm looking for a condition that can make sure that once the user is created it won't be added to the variable again, to make sure the user isn't added twice. My first thought to achieve this was to check if a password has been generated already.
Is there any way to make sure a user is only added to this variable once and not on consecutive runs?
Here is a snapshot of the code I am using:
- name: Include users.yaml and setup all vars for nexus
include_vars:
file: users.yaml
name: users
- set_fact:
tmp_user:
- username: "{{ item.username }}"
first_name: "{{ item.first_name }}"
last_name: "{{ item.last_name }}"
email: "{{ item.email }}"
password: "{{ lookup('password', 'credentials/' + item.username + '/password.txt length=15 chars=ascii_letters') }}"
roles: "{{ item.roles }}"
loop: "{{ users.nexus_local_users }}"
register: tmp_users
- set_fact:
tmp_user: "{{ item.item | combine(item.ansible_facts.tmp_user) }}"
with_items: "{{ tmp_users.results }}"
register: tmp_users
- set_fact:
nexus_local_users: "{{ tmp_users.results | map(attribute='ansible_facts.tmp_user') | list }}"
I'm looking for something like this but can't figure out how to implement it (the when condition):
- set_fact:
tmp_user:
- username: "{{ item.username }}"
first_name: "{{ item.first_name }}"
last_name: "{{ item.last_name }}"
email: "{{ item.email }}"
password: "{{ lookup('password', 'credentials/' + item.username + '/password.txt length=15 chars=ascii_letters') }}"
roles: "{{ item.roles }}"
loop: "{{ users.nexus_local_users }}"
when: **directory/file credentials/{{ user }}/password.txt exists**
register: tmp_users
Thanks in advance!
Gijs
stat
before the loop, and then a when conditional based on the outcome of that?stat
as well? It needs to check all the directory's and based on that figure out if the user was already created. This means there is a directory per user in the credentials folder if that user was already created.