# Cannot create file on all hosts

**URL:** <https://forum.ansible.com/t/cannot-create-file-on-all-hosts/7995>\
**Category:** Get Help\
**Tags:** awx\
**Created:** [August 20, 2024, 2:28pm UTC](https://forum.ansible.com/t/cannot-create-file-on-all-hosts/7995 "2024-08-20T14:28:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gothsome](https://avatars.discourse-cdn.com/v4/letter/g/278dde/32.png) [@gothsome](https://forum.ansible.com/u/gothsome)\
**Post date:** [August 20, 2024, 2:28pm UTC](https://forum.ansible.com/t/cannot-create-file-on-all-hosts/7995/1 "2024-08-20T14:28:49Z")

</div>

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!!

```auto
---
- 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!

---

<div class="post-metadata">

**Author:** ![ramrexx](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/ramrexx/32/3691_2.png) [@ramrexx](https://forum.ansible.com/u/ramrexx)\
**Post date:** [August 20, 2024, 4:35pm UTC](https://forum.ansible.com/t/cannot-create-file-on-all-hosts/7995/2 "2024-08-20T16:35:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gothsome](https://avatars.discourse-cdn.com/v4/letter/g/278dde/32.png) [@gothsome](https://forum.ansible.com/u/gothsome)\
**Post date:** [August 21, 2024, 5:42am UTC](https://forum.ansible.com/t/cannot-create-file-on-all-hosts/7995/3 "2024-08-21T05:42:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gothsome](https://avatars.discourse-cdn.com/v4/letter/g/278dde/32.png) [@gothsome](https://forum.ansible.com/u/gothsome)\
**Post date:** [August 21, 2024, 6:45am UTC](https://forum.ansible.com/t/cannot-create-file-on-all-hosts/7995/4 "2024-08-21T06:45:26Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [September 20, 2024, 6:45am UTC](https://forum.ansible.com/t/cannot-create-file-on-all-hosts/7995/5 "2024-09-20T06:45:40Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
