Hello!
I had to change my playbook where i change the pw of a user and put the data in a csv in /tmp
Somehow anlong the way the clients doesnt create/add data in theyr files, but one host:
I use the hostname for the files because they have to be collected in another playbook which functions, but the csv generation is corrupted.
Thank you again for your input!!
---
- name: Do password work
hosts: all
become: yes
vars:
usr_2_edit: awx
tasks:
- name: Get the hostname
ansible.builtin.command: "hostname"
register: hostname_result
- name: Set csv_pth variable based on hostname
set_fact:
csv_pth: "/tmp/{{ hostname_result.stdout }}_bw_pw_add_gsa_test.csv"
- name: Ensure the CSV file exists
ansible.builtin.file:
path: "{{ csv_pth }}"
state: touch
- name: Add header to CSV file if missing
ansible.builtin.lineinfile:
path: "{{ csv_pth }}"
line: 'folder,favorite,type,name,notes,fields,reprompt,login_uri,login_username,login_password,login_totp'
insertbefore: BOF
state: present
- name: Get the current date in HH:MM_DD.mm.yyyy format
ansible.builtin.command: "date +'%H:%M_%d.%m.%Y'"
register: date_result
- name: Generate a random password
ansible.builtin.command: "openssl rand -base64 12"
register: password_result
- name: Get the primary IP address
ansible.builtin.set_fact:
ip_address: "{{ ansible_default_ipv4.address }}"
- name: Update or create user with the specified username and password
ansible.builtin.user:
name: "{{ usr_2_edit }}"
password: "{{ password_result.stdout | password_hash('sha512') }}"
state: present
- name: Append data to CSV file
ansible.builtin.lineinfile:
path: "{{ csv_pth }}"
line: ",,login,{{ hostname_result.stdout }},{{ date_result.stdout }},,0,{{ ip_address }},{{ usr_2_edit }},{{ password_result.stdout }},"
insertafter: EOF
- name: Install the python3-pexpect module
ansible.builtin.package:
name: python3-pexpect
state: present
- name: Test the user password
ansible.builtin.expect:
command: "su - {{ usr_2_edit }}"
responses:
"Password:": "{{ password_result.stdout }}"
register: password_test
failed_when: "'authentication failure' in password_test.stdout"
- name: Print password test result
ansible.builtin.debug:
msg: "Password change for user {{ usr_2_edit }} was successful."
when: password_test.rc == 0
192.168.151.237 is the ip of the host where the file gets generated.
Thank you very much again!