Ansible - Manipuler fichiers/dossiers

Il existe plusieurs façon de créer un répertoire avec Ansible dans un playbook.
Base de création d’un playbook : https://n0tes.fr/2023/02/12/Ansible-Playbook/

Manipuler des fichiers

Copie

Copier un fichier :

- name: Copy file with owner and permissions
ansible.builtin.copy:
src: /srv/myfiles/john.conf
dest: /etc/john.conf

- name: Copy file with owner and permissions
ansible.builtin.copy:
src: /srv/myfiles/john.conf
dest: /etc/john.conf
owner: john
group: john
mode: '0644'

- name: Copy file with owner and permission, using symbolic representation
ansible.builtin.copy:
src: /srv/myfiles/john.conf
dest: /etc/john.conf
owner: john
group: john
mode: u=rw,g=r,o=r

- name: Another symbolic mode example, adding some permissions and removing others
ansible.builtin.copy:
src: /srv/myfiles/john.conf
dest: /etc/john.conf
owner: john
group: john
mode: u+rw,g-wx,o-rwx

- name: Copy a new "ntp.conf" file into place, backing up the original if it differs from the copied version
ansible.builtin.copy:
src: /mine/ntp.conf
dest: /etc/ntp.conf
owner: root
group: root
mode: '0644'
backup: yes

- name: Copy a new "sudoers" file into place, after passing validation with visudo
ansible.builtin.copy:
src: /mine/sudoers
dest: /etc/sudoers
validate: /usr/sbin/visudo -csf %s

- name: Copy a "sudoers" file on the remote machine for editing
ansible.builtin.copy:
src: /etc/sudoers
dest: /etc/sudoers.edit
remote_src: yes
validate: /usr/sbin/visudo -csf %s

- name: Copy using inline content
ansible.builtin.copy:
content: '# This file was moved to /etc/other.conf'
dest: /etc/mine.conf

- name: If follow=yes, /path/to/file will be overwritten by contents of john.conf
ansible.builtin.copy:
src: /etc/john.conf
dest: /path/to/link # link to /path/to/file
follow: yes

- name: If follow=no, /path/to/link will become a file and be overwritten by contents of john.conf
ansible.builtin.copy:
src: /etc/john.conf
dest: /path/to/link # link to /path/to/file
follow: no

Création

Créer un fichier vide avec des permissions et certains attributs du fichier :

- name: Touch a file, using symbolic modes to set the permissions (equivalent to 0644)
ansible.builtin.file:
path: /etc/john.conf
state: touch
mode: u=rw,g=r,o=r

- name: Touch the same file, but add/remove some permissions
ansible.builtin.file:
path: /etc/john.conf
state: touch
mode: u+rw,g-wx,o-rwx

- name: Touch again the same file, but do not change times this makes the task idempotent
ansible.builtin.file:
path: /etc/john.conf
state: touch
mode: u+rw,g-wx,o-rwx
modification_time: preserve
access_time: preserve

Créer un dossier :

- name: Create a directory if it does not exist
ansible.builtin.file:
path: /etc/some_directory
state: directory
owner: paul
group: scientist
mode: '0755'

Créer un lien symbolique :

- name: Create a symbolic link
ansible.builtin.file:
src: /file/to/link/to
dest: /path/to/symlink
owner: john
group: john
state: link

Créer 2 hard links :

- name: Create two hard links
ansible.builtin.file:
src: '/tmp/{{ item.src }}'
dest: '{{ item.dest }}'
state: hard
loop:
- { src: x, dest: y }
- { src: z, dest: k }

Modifiers des attributs

Changer le propriétaire, le groupe et les permissions :

- name: Change file ownership, group and permissions
ansible.builtin.file:
path: /etc/john.conf
owner: john
group: john
mode: '0644'

Changer récursivement le propriétaire :

- name: Recursively change ownership of a directory
ansible.builtin.file:
path: /etc/john
state: directory
recurse: yes
owner: john
group: john

Changer les dates d’accès :

- name: Update modification and access time of given file
ansible.builtin.file:
path: /etc/some_file
state: file
modification_time: now
access_time: now

Changer les dates d’accès avec epoch :

- name: Set access time based on seconds from epoch value
ansible.builtin.file:
path: /etc/another_file
state: file
access_time: '{{ "%Y%m%d%H%M.%S" | strftime(stat_var.stat.atime) }}'

Supression

Supprimer un fichier :

- name: Remove file (delete file)
ansible.builtin.file:
path: /etc/john.txt
state: absent

Supprimer récursivement :

- name: Recursively remove directory
ansible.builtin.file:
path: /etc/john
state: absent

Documentation

https://docs.ansible.com/ansible/latest/collections/ansible/builtin/file_module.html#ansible-collections-ansible-builtin-file-module
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/copy_module.html#ansible-collections-ansible-builtin-copy-module

> Partager <