Cannot create file on all hosts

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!

Have you tried doing a debug after your touch task? Something like adding this and rerun:

- name: log csv_path
  ansible.builtin.debug:
    var: csv_path

It is interesting that it works on some hosts but not others

Yeah yesterday in the beginning i still have the full files everywhere, but after lunch an error sneaked in the playbook - i have to keep a close watch whats the changes the chatgpt does.

The playbook is marked as successfully executed so i didnt see the error right away.

Somehow the hosts have now are included, but they say the file got edited, wehe the pw is 1,5 hours before i started to work today - even wehn th time

The problem was in two parts: wrong time and wrong timezone.
This made me thing the playbook didnt got executed in the same time than the one of the only server where the time was correct.

So everything is fine now i added in the playbook to set the timeserver and timezone to be shure all hosts will have the same time all the time.

Thank you!

1 Like