Ansible Module file: Difference between revisions
Jump to navigation
Jump to search
(6 intermediate revisions by the same user not shown) | |||
Line 6: | Line 6: | ||
=Overview= | =Overview= | ||
Manage file, directory and symlinks properties: create, set attributes, remove. | Manage file, directory and symlinks properties: create, set attributes, remove. | ||
=Examples= | |||
=Create | ==Create Directories== | ||
<syntaxhighlight lang='yaml'> | <syntaxhighlight lang='yaml'> | ||
- name: create | - name: create volume directories | ||
file: | file: | ||
path: "{{ item }}" | path: "/mnt/ebs0/{{ item }}" | ||
state: directory | state: directory | ||
owner: root | owner: root | ||
with_items: | with_items: | ||
- " | - "local-pv0" | ||
- "local-pv1" | |||
- "local-pv2" | |||
when: | when: | ||
- inventory_hostname in groups['kube-node'] | - 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> | </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'