Ansible Module file: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
Line 34: Line 34:
</syntaxhighlight>
</syntaxhighlight>
==Create Files from Template Mirroring a Previously Created Directory Structure==
==Create Files from Template Mirroring a Previously Created Directory Structure==
The directory structure is created by the example above.
The directory structure is created by the [[#Create_Directories_Mirroring_an_Existing_Structure|example above]].
<syntaxhighlight lang='yaml'>
<syntaxhighlight lang='yaml'>
- name: Create the files from template
- name: Create the files from template

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'