Ansible Module file

From NovaOrdis Knowledge Base
Jump to navigation Jump to search

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'