ansible stat file - ghdrako/doc_snipets GitHub Wiki

Check for the presence of a file in the group_vars called secrets.yml. We then register the results of this task as a variable called secrets_file.

- name: Check if the file secrets.yml exists
  ansible.builtin.stat:
    path: "group_vars/secrets.yml"
  register: secrets_file

Generate the secrets.yml file if exists is set to false generate the secrets.yml file if exists is set to false

- name: Generate the secrets.yml file using a template file if not exists
  ansible.builtin.template:
    src: "secrets.yml.j2"
    dest: "group_vars/secrets.yml"
  when: secrets_file.stat.exists == false
$ cat secrets.yml.j2
short_random_hash: "{{ lookup('community.general.random_string', length=5, upper=false, special=false, numbers=false) }}"
db_password: "{{ lookup('community.general.random_string', length=20, upper=true, special=true, override_special="@-&*", min_special=2, numbers=true) }}"
vm_password: "{{ lookup('community.general.random_string', length=20, upper=true, special=true, override_special="@-&*", min_special=2, numbers=true) }}"
wp_password: "{{ lookup('community.general.random_string', 

Now that we know the file is there, as we have just generated it, we can load the contents of the file in as variables:

- name: Load the variables defined in the secrets.yml file
  ansible.builtin.include_vars:
    file: "group_vars/secrets.yml"
- hosts: servers
  tasks:
  - name: Ansible check file exists.
    stat:
      path: /etc/issue
    register: p
  - debug:
      msg: "File exists..."
    when: p.stat.exists
  - debug:
      msg: "File not found"
    when: p.stat.exists == False

    tasks:
      - name: Check that the somefile.conf exists
        stat:
          path: /etc/file.txt
        register: stat_result

      - name: Create the file, if it doesnt exist already
        file:
          path: /etc/file.txt
          state: touch
        when: not stat_result.stat.exists
     when: not stat_result.stat.exists | bool == true
     when: stat_result.stat.exists == False
     when: not stat_result.stat.exists
- stat: path=/path/to/something
  register: p

- debug: msg="Path exists and is a directory"
  when: p.stat.isdir is defined and p.stat.isdir

To be ensure the file exists you would use the file module:

- name: make sure file exists
  file:
    path: /etc/file.txt
    state: touch

make this idempotent by adding modification_time=preserve access_time=preserve

stat is slow and collects a lot of info that is not required for file existence check

- raw: test -e /path/to/something && echo -n true || echo -n false
  register: file_exists

- debug: msg="Path exists"
  when: file_exists.stdout == "true"
- name: Register file
      stat:
        path: "/tmp/test_file"
      register: file_path

- name: Create file if it doesn't exists
      file: 
        path: "/tmp/test_file"
        state: touch
      when: file_path.stat.exists == False
- set_fact:
    file: "/tmp/test_file"

  - name: check file exists
    shell: "ls {{ file }}"
    register: file_path
    ignore_errors: true

  - name: create file if don't exist
    shell: "touch {{ file }}"
    when: file_path.rc != 0