Managing Files Using Ansible File-Related Modules

In this lesson, you will learn the different Ansible file modules.

Manipulating & Managing Files Using Ansible Files Modules

Files can be manipulated and managed by some Ansible modules such as; “copy”, “lineinfile”, “blockinfile”, sync, etc. In this lesson, we will learn and understand how to use some of these Ansible file-related modules.

Using Ansible copy Module With Examples

The Ansible copy module is used to copy files from the local or remote/control node to the managed hosts.

To know more about the copy module and what it can do, one can simply check the copy module man page by using the command, “ansible-doc copy” just as I mentioned in one of our previous lessons.

Apart from copying files, the copy module can also be used with some arguments to set file permissions or attributes and the SELinux contexts of files.

Let’s see some examples of how the copy module can be used.

To copy the file, “myfiles/file1” from the control node to the managed hosts in the destination, /tmp/ansible, the playbook below can be used.

1. create a playbook

[lisa@drdev1 ~]$ vim playbook15.yml
---
- name: Managing and manipulating files using various module
  hosts: all
  tasks:
     - name: copy file from control node to managed hosts
       copy:
         src: myfiles/file1
         dest: /tmp/Ansible/file1
         owner: root
         group: root
         setype: Ansible_test_t

src=Local path to a file to copy to the remote server. This can be absolute or relative.

dest=the remote absolute path where the file should be copied to. If src is a directory, this must be a directory too

owner=Name of the owner that should own the file/directory

group=Name of the group that should own the file/directory

setype=The type part of the SELinux file context

2. Run the playbook

[lisa@drdev1 ~]$ ansible-playbook playbook15.yml

PLAY [Managing and manipulating files using various module] *******************************

TASK [Gathering Facts] ********************************************************************
ok: [hqdev1.tekneed.com]
ok: [localhost]
.....................

3. Verify that the file has been copied.

[lisa@drdev1 ~]$ ansible all -m command -a 'cat /tmp/Ansible/file1' -u root
hqdev1.tekneed.com | CHANGED | rc=0 >>
file1 manipulated by Ansible

localhost | CHANGED | rc=0 >>
file1 manipulated by Ansible

The copy module can also be used to create a file. For example, to create a file with the name, “/tmp/file2” with some contents, the playbook below can be used.

1. Create the playbook

[lisa@drdev1 ~]$ vim playbook16.yml
---
- name: Managing and manipulating files using various module
  hosts: hqdev1.tekneed.com
  tasks:
     - name: copy some contents
       copy:
        dest: /tmp/file2
        content: |
           copied data using the ansible file module
           Another copied data using ansible file module

2. Run the playbook

[lisa@drdev1 ~]$ ansible-playbook playbook16.yml

PLAY [Managing and manipulating files using various module] ***************************
.................

3. Verify that the file has been created

[lisa@drdev1 ~]$ ansible hqdev1.tekneed.com -a 'cat /tmp/file2'

hqdev1.tekneed.com | CHANGED | rc=0 >>
copied data using the ansible file module
Another copied data using ansible file module

Using Ansible Fetch Module With Examples

The Ansible fetch module is similar to the copy module but the opposite of it. The fetch module is used to copy files from the managed hosts to the control node.

The fetch module also works with some arguments and can be used to set some file attributes. To know more about the fetch module, one can simply visit the fetch module man page by using the command, ‘ansible-doc fetch’

Let’s see an example of how the Ansible fetch module can be used.

To copy the fstab file of a managed hosts to the control node in the destination, “/tmp/Ansible”, the playbook below can be used.

1. Create a playbook

[lisa@drdev1 ~]$ vim playbook15.yml
---
- name: Managing and manipulating files using various module
  hosts: all
  tasks:
     - name: fetch file from managed hosts to controller
       fetch:
         src: /etc/fstab
         dest: /tmp/Ansible

2. Run the playbook.

[lisa@drdev1 ~]$ ansible-playbook playbook15.yml

PLAY [Managing and manipulating files using various module] *******************************

3. Verify that the file has been copied.

[lisa@drdev1 ~]$ ansible all -m command -a 'ls -l /tmp/Ansible' -u root

localhost | CHANGED | rc=0 >>
total 4
-rw-r--r--. 1 root root 29 Apr 22 10:34 file1
drwxrwxr-x. 3 lisa lisa 17 Apr 22 10:58 hqdev1.tekneed.com
drwxrwxr-x. 3 lisa lisa 17 Apr 22 10:58 localhost

hqdev1.tekneed.com | CHANGED | rc=0 >>
total 4
-rw-r--r--. 1 root root 29 Apr 22 10:34 file1
ansible file modules

The fetch module will create a directory with each managed host names that is specified in the playbook. In our case, the hosts value is “all” which includes hqdev1.tekneed.com and localhost in the inventory file. To alter this settings otherwise, the flat argument can be used.

*verify hqdev1.tekneed.com fstab file

[lisa@drdev1 ~]$ cat /tmp/Ansible/hqdev1.tekneed.com/etc/fstab

#
# /etc/fstab
# Created by anaconda on Wed Jan  6 07:30:46 2021
.........................

Using Ansible File Module With Examples

The Ansible file module is used to manage file and directories attributes including symbolic links, SELinux contexts, hard links, file creation, file deletion, etc. Many other modules such as the copy module can also be used to set or manage file and directories attributes.

To know more about the file module, one can simply use the command, “ansible-doc file”. As usual, let’s see how we can use the file module with examples.

To create an empty file, “/tmp/emptyfile1” and set some file attributes with Ansible file module, use the playbook below.

1. create a playbook

[lisa@drdev1 ~]$ vim playbook15.yml
---
- name: Managing and manipulating files using various module
  hosts: hqdev1.tekneed.com
  tasks:
     - name: Create an empty file
       file:
         path: /tmp/emptyfile1
         state: touch
         mode: 0777
         owner: kehinde

2. Run the playbook

[lisa@drdev1 ~]$ ansible-playbook playbook15.yml

PLAY [Managing and manipulating files using various module] ***************************
.....................

3. Verify that the file has been created, including its attributes

[lisa@drdev1 ~]$ ansible hqdev1.tekneed.com -m command -a 'ls -l /tmp/emptyfile1'

hqdev1.tekneed.com | CHANGED | rc=0 >>
-rwxrwxrwx. 1 kehinde root 0 Apr 22 22:49 /tmp/emptyfile1

Going forward with the example above, to set the SELInux type context of “emptyfile1” to home_root_t, the setype argument can be used, hence, the playbook can be written as below.

---
- name: Managing and manipulating files using various module
  hosts: hqdev1.tekneed.com
  tasks:
     - name: Create an empty file
       file:
         path: /tmp/emptyfile1
         state: touch
         mode: 0777
         owner: kehinde
         setype: home_root_t

*Run the playbook.

[lisa@drdev1 ~]$ ansible-playbook playbook15.yml

PLAY [Managing and manipulating files using various module] ***************************
.....................

*Verify the SELinux context

[lisa@drdev1 ~]$ ansible hqdev1.tekneed.com  -a 'ls -lZ /tmp/emptyfile1'

hqdev1.tekneed.com | CHANGED | rc=0 >>
-rwxrwxrwx. 1 kehinde root unconfined_u:object_r:home_root_t:s0 0 Apr 22 23:29 /tmp/emptyfile1

IMPORTANT: Using the setype argument to change SELinux context is like using the chcon command. To make a context persistent or survive context restoration, i.e restorecon, the sefcontext module has to be used.

The sefcontext module is similar to using the semanage-fcontext utility.

Going forward with the example above, to delete the file, /tmp/emptyfile1, use the absent value for the state key, the playbook will be as below

---
- name: Managing and manipulating files using various module
  hosts: hqdev1.tekneed.com
  tasks:
     - name: Delete an empty file
       file:
         path: /tmp/emptyfile1
         state: absent

*Run the playbook

[lisa@drdev1 ~]$ ansible-playbook playbook15.yml

PLAY [Managing and manipulating files using various module] ***************************
.....................

*Verify that the file has been deleted.

[lisa@drdev1 ~]$ ansible hqdev1.tekneed.com  -a 'ls -l /tmp/emptyfile1'

hqdev1.tekneed.com | FAILED | rc=2 >>
ls: cannot access '/tmp/emptyfile1': No such file or directorynon-zero return code

Using Ansible sefcontext module With Examples

The Ansible sefcontext module is similar to using the semanage-fcontext utility. It is used to manage SELinux file type context. More so, the SELInux context changed will be persistent.

To know more about the sefcontext module, see the man page by using the command, “ansible-doc sefcontext”

Let’s see how this module can be used with examples.

To create an empty file, emptyfile1 in /tmp and set the SELinux type context to home_root_t, the playbook can be written as below.

1. create a playbook

---
- name: Managing and manipulating files using various module
  hosts: hqdev1.tekneed.com
  tasks:
     - name: Create an empty file
       file:
         path: /tmp/emptyfile1
         state: touch

     - name: Set the SELinux type context
       sefcontext:
         target: /tmp/emptyfile1
         setype: home_root_t
         state: present

2. Run the playbook.

[lisa@drdev1 ~]$ ansible-playbook playbook15.yml

PLAY [Managing and manipulating files using various module] ***************************

TASK [Gathering Facts] ****************************************************************
ok: [hqdev1.tekneed.com]
..............

3. Verify that the file has been created and the SELinux type context has changed.

[lisa@drdev1 ~]$ ansible hqdev1.tekneed.com -a 'ls -lZ /tmp/emptyfle1'

hqdev1.tekneed.com | CHANGED | rc=0 >>
-rw-r--r--. 1 root root unconfined_u:object_r:user_tmp_t:s0 5 Apr 24 06:31 /tmp/emptyfle1

You will notice that the file context type did not change, however, the policy has been changed. That’s why you see return code to be 0 (rc=0). You can also see that the color is yellow, indicating that a change has been made.

Using Ansible Synchronize Module With Examples

The Ansible synchronize module is similar to using the rsync command in Linux or Unix systems, which is also similar to the copy command, only that if you are copying the same file, the differences in the target file is only copied to the destination and not the whole file.

To use the synchronize module, the rsync utility must be installed on both the controller and the managed hosts.

More information on how to use the synchronize module can be gotten by using the command, “ansible-doc synchronize”.

An example of playbook that uses the synchronize module can be written as below.

---
- name: Managing and manipulating files using various module
  hosts: all
  tasks:
     - name: copy file from control node to managed hosts
       synchronize:
         src: myfiles/file1
         dest: /tmp/Ansible/file1

Using Ansible lineinfile Module With Examples

The Ansible lineinfile module, from the word can be very useful to add a line in a file or ensure that a line is present in a file.

To know more about how to use this module, reference the module man page by using the command, “ansible-doc lineinfile”

Let’s see an example of how the ansible lineinfile module can be used.

To add the line, “line added by lineinfile module” to the file, /myfiles/file1, the playbook can be written as shown below.

1. create a playbook.

[lisa@drdev1 ~]$ vim playbook16.yml
---
- name: Managing and manipulating files using various module
  hosts: all
  tasks:
     - name: Add a line to a file
       lineinfile:
         path: /myfiles/file1
         line: This is a test line by lineinfile module
         state: present

2. Run the playbook

[lisa@drdev1 ~]$ ansible-playbook playbook16.yml

PLAY [Managing and manipulating files using various module] ******************************

3. Verify that the line has been added

[lisa@drdev1 ~]$ ansible all -a 'cat /myfiles/file1'

localhost | CHANGED | rc=0 >>
Test file
This is a test line by lineinfile module

hqdev1.tekneed.com | CHANGED | rc=0 >>
yrt
This line was added by the lineinfile module

Using Ansible blockinfile Module With Examples

The Ansible blockinfile module is similar to the lineinflie module but this time around used to add blocks of line in a file.

The command, “ansible-doc blockinfile” can be referenced to know more about how the blockinfile module can be used.

For example, to add a block of lines to a file, a playbook can be written as below.

1. create a playbook.

[lisa@drdev1 ~]$ vim playbook16.yml
---
- name: Managing and manipulating files using various module
  hosts: hqdev1.tekneed.com
  tasks:
     - name: Add a line to a file
       blockinfile:
         path: /myfiles/file1
         block: |
           This is a test statement1 added by blockinfile module
           This is a test statement2 added by blockinfile module
         state: present

2. Run the playbook

[lisa@drdev1 ~]$ ansible-playbook playbook16.yml

PLAY [Managing and manipulating files using various module] ******************************

3. Verify that the block of lines have been added

[lisa@drdev1 ~]$ ansible hqdev1.tekneed.com -a 'cat /myfiles/file1'

hqdev1.tekneed.com | CHANGED | rc=0 >>
old test line
# BEGIN ANSIBLE MANAGED BLOCK
This is a test statement1 added by blockinfile module
This is a test statement2 added by blockinfile module
# END ANSIBLE MANAGED BLOCK

Using Ansible stat Module With Examples

The Ansible stat module is used to get the status/facts/attributes of a file. It is not really used to make changes. This is similar to using the stat command in Linux/Unix system.

For more information about the stat module, refer to the stat documentation by using the command, “ansible-doc stat”

Click Here To Watch The Video On Using Ansible File Modules

RHCE Exam Practice Question & Answer On Ansible File Modules

Ansible Basics – Video

Your feedback is welcomed. If you love others, you will share with others

Be the first to comment

Leave a Reply

Your email address will not be published.


*