Skip to content

mpenning/ansible_conditional_create_file

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 

Repository files navigation

Ansible: Conditionally create a file

Use ansible to conditionally create a file. There are two options for doing this...

Option 1: copy module with sudo

This uses a side-effect of the ansible copy module and force: no, which can create a file as root.

---
- name: Add a new file
  hosts: bobcat.att.net
  tasks:
    - name: Conditionally create a file if it does not exist
      copy:
        content: "insert-sensitive-data"
        dest: "{{ lookup('env', 'HOME') }}/data.txt"
        force: no
        owner: root
        group: root
        mode: 0400
      become: true
      become_with: sudo
      become_user: root

Option 2: shell module

The shell module is sometimes discouraged, but it also can create the file conditionally. You can also sudo with shell if you want.

---
- name: Add a new file
  hosts: foo.localdomain
  tasks:
    - name: Conditionally create a file if it does not exist
      shell: echo "insert-sensitive-data" > $HOME/data.txt
      args:
        creates: "$HOME/data.txt"

About

Use ansible to conditionally create a file

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published