Ansible Module file: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
(Created page with "=External= * https://docs.ansible.com/ansible/latest/modules/file_module.html =Internal= * Ansible Concepts")
 
 
(10 intermediate revisions by the same user not shown)
Line 3: Line 3:
=Internal=
=Internal=
* [[Ansible Concepts#file|Ansible Concepts]]
* [[Ansible Concepts#file|Ansible Concepts]]
=Overview=
Manage file, directory and symlinks properties: create, set attributes, remove.
=Examples=
==Create Directories==
<syntaxhighlight lang='yaml'>
- name: create volume directories
  file:
    path: "/mnt/ebs0/{{ item }}"
    state: directory
    owner: root
  with_items:
    - "local-pv0"
    - "local-pv1"
    - "local-pv2"
  when:
    - inventory_hostname in groups['kube-node']
</syntaxhighlight>
==Create Directories Mirroring an Existing Structure==
Create a directory structure mirroring an existing directory structure, specified by <code>with_filetree</code>, where the "filetree" is relative to <code><role-name>/files</code> Ansible configuration directory:
<syntaxhighlight lang='yaml'>
- name: Create a directory structure
  file:
    path: "{{ ansible_env.TARGET_DIR }}/{{ item.path }}"
    state: directory
    mode: '{{ item.mode }}'
  with_filetree: "dirA/dirB/"
  when: item.state == 'directory'
</syntaxhighlight>
==Create Files from Template Mirroring a Previously Created Directory Structure==
The directory structure is created by the [[#Create_Directories_Mirroring_an_Existing_Structure|example above]].
<syntaxhighlight lang='yaml'>
- name: Create the files from template
  template:
    src: "{{ item.src }}"
    dest: "{{ ansible_env.TARGET_DIR }}/{{ item.path }}"
    mode: "u=rw,g=r,o=r"
    force: "no"
  with_filetree: "dirA/dirB/"
  when: item.state == 'file'
</syntaxhighlight>

Latest revision as of 05:46, 2 July 2021

External

Internal

Overview

Manage file, directory and symlinks properties: create, set attributes, remove.

Examples

Create Directories

- name: create volume directories
  file:
    path: "/mnt/ebs0/{{ item }}"
    state: directory
    owner: root
  with_items:
    - "local-pv0"
    - "local-pv1"
    - "local-pv2"
  when:
    - inventory_hostname in groups['kube-node']

Create Directories Mirroring an Existing Structure

Create a directory structure mirroring an existing directory structure, specified by with_filetree, where the "filetree" is relative to <role-name>/files Ansible configuration directory:

- name: Create a directory structure
  file:
    path: "{{ ansible_env.TARGET_DIR }}/{{ item.path }}"
    state: directory
    mode: '{{ item.mode }}'
  with_filetree: "dirA/dirB/"
  when: item.state == 'directory'

Create Files from Template Mirroring a Previously Created Directory Structure

The directory structure is created by the example above.

- name: Create the files from template
  template:
    src: "{{ item.src }}"
    dest: "{{ ansible_env.TARGET_DIR }}/{{ item.path }}"
    mode: "u=rw,g=r,o=r"
    force: "no"
  with_filetree: "dirA/dirB/"
  when: item.state == 'file'