Ansible Tutorial
Lets see how to create an empty file in ansible with example ansible playbook.
file module can be used to create an empty file.
path and state parameters are mandatory and path parameter is used to specify file path and state parameter is used to specify touch as empty file creation.
- hosts: localhost connection: local gather_facts: no tasks: - name: initialize file path set_fact: file_path: "test/testing" - name: "Creating empty {{ file_path }} file" become: true become_user: root file: path: "{{ file_path }}" state: touch - name: Print the msg debug: msg: "Created {{ file_path }} file."
Output:
$ ansible-playbook create_empty_file.yml [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all' PLAY [localhost] ************************************************************************************************************************************************************ TASK [initialize file path] ************************************************************************************************************************************************* ok: [localhost] TASK [Creating empty test/testing file] ************************************************************************************************************************************* changed: [localhost] TASK [Print the msg] ******************************************************************************************************************************************************** ok: [localhost] => { "msg": "Created test/testing file." } PLAY RECAP ****************************************************************************************************************************************************************** localhost : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Lets see whether testing empty file is created under test directory or not,
$ ls -all test/ -rw-r--r--. 1 root root 0 Nov 12 17:05 testing
Here mode is default, read and write to the owner and others and group users will have only read access.
How to update mode (file permission) in ansible?
We can use the mode parameter in ansible to set the file permission as required.
Permissions the resulting file or directory should have.
similar to using the /usr/bin/chmod command to set modes.
mode can be octal format like 0644 or 01777 and also string format like '644' or '1777'.
Ansible 1.8, the mode may be specified as a symbolic mode (for example, u+rwx or u=rw,g=r,o=r).
- hosts: localhost connection: local gather_facts: no tasks: - name: initialize file path set_fact: file_path: "test/testing" - name: "Creating empty {{ file_path }} file" become: true become_user: root file: path: "{{ file_path }}" mode: '647' state: touch - name: Print the msg debug: msg: "Created {{ file_path }} file."
Output:
$ ansible-playbook create_empty_file.yml [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all' PLAY [localhost] ************************************************************************************************************************************************************ TASK [initialize file path] ************************************************************************************************************************************************* ok: [localhost] TASK [Creating empty test/testing file] ************************************************************************************************************************************* changed: [localhost] TASK [Print the msg] ******************************************************************************************************************************************************** ok: [localhost] => { "msg": "Created test/testing file." } PLAY RECAP ****************************************************************************************************************************************************************** localhost : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Lets see the file permission is assigned,
$ ls -all test/ -rw-r--rwx. 1 root root 0 Nov 12 17:13 testing
Here an empty file is created as mentioned in the mode parameter. read and write to the owner, read only to group users and others will have read, write and executable permissions.
Ansible Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page