Back to menu

3 - Management of Files

Learning Ansible with Rocky

Rocky Linux Academy > Ansible courses > 3 - Management of Files
Back to menu

Objectives

In this chapter you will learn how to manage files with Ansible.

modify the content of file;
upload files to the targeted servers;
retrieve files from the targeted servers.

Rocky Linux Academy > Ansible courses > 3 - Management of Files
Back to menu
Rocky Linux Academy > Ansible courses > 3 - Management of Files
Back to menu




Depending on your needs, you will have to use different Ansible modules to modify the system configuration files.

Rocky Linux Academy > Ansible courses > 3 - Management of Files
Back to menu



ini_file module

Rocky Linux Academy > Ansible courses > 3 - Management of Files
Back to menu

ini_file module

When you want to modify an INI file eg:

[section]
key=value

the easiest way is to use the ini_file module.

Rocky Linux Academy > Ansible courses > 3 - Management of Files
Back to menu

ini_file module

The module requires:

  • The value of the section
  • The name of the option
  • The new value
Rocky Linux Academy > Ansible courses > 3 - Management of Files
Back to menu

ini_file module

Example of use:

- name: change value on inifile
  community.general.ini_file:
    dest: /path/to/file.ini
    section: SECTIONNAME
    option: OPTIONNAME
    value: NEWVALUE
Rocky Linux Academy > Ansible courses > 3 - Management of Files
Back to menu

ini_file module

More information can be found at https://docs.ansible.com/ansible/latest/collections/community/general/ini_file_module.html.

Rocky Linux Academy > Ansible courses > 3 - Management of Files
Back to menu



Questions ?

Rocky Linux Academy > Ansible courses > 3 - Management of Files
Back to menu



lineinfile module

Rocky Linux Academy > Ansible courses > 3 - Management of Files
Back to menu

lineinfile module

To ensure that a line is present in a file, or when a single line in a file needs to be added or modified, use the linefile module.

In this case, the line to be modified in a file will be found using a regexp.

Rocky Linux Academy > Ansible courses > 3 - Management of Files
Back to menu

lineinfile module

For example, to ensure that the line starting with SELINUX= in the /etc/selinux/config file contains the value enforcing:

- ansible.builtin.lineinfile:
    path: /etc/selinux/config
    regexp: '^SELINUX='
    line: 'SELINUX=enforcing'
Rocky Linux Academy > Ansible courses > 3 - Management of Files
Back to menu

lineinfile module

More information can be found at https://docs.ansible.com/ansible/latest/collections/ansible/builtin/lineinfile_module.html.

Rocky Linux Academy > Ansible courses > 3 - Management of Files
Back to menu



Questions ?

Rocky Linux Academy > Ansible courses > 3 - Management of Files
Back to menu



copy module

Rocky Linux Academy > Ansible courses > 3 - Management of Files
Back to menu

copy module

When a file has to be copied from the Ansible server to one or more hosts, it is better to use the copy module.

Rocky Linux Academy > Ansible courses > 3 - Management of Files
Back to menu

copy module

Here we are copying myflile.conf from one location to another:

- ansible.builtin.copy:
    src: /data/ansible/sources/myfile.conf
    dest: /etc/myfile.conf
    owner: root
    group: root
    mode: 0644
Rocky Linux Academy > Ansible courses > 3 - Management of Files
Back to menu

copy module

More information can be found at https://docs.ansible.com/ansible/latest/collections/ansible/builtin/copy_module.html.

Rocky Linux Academy > Ansible courses > 3 - Management of Files
Back to menu



Questions ?

Rocky Linux Academy > Ansible courses > 3 - Management of Files
Back to menu



fetch module

Rocky Linux Academy > Ansible courses > 3 - Management of Files
Back to menu

fetch module

When a file has to be copied from a remote server to the local server, it is best to use the fetch module.

Rocky Linux Academy > Ansible courses > 3 - Management of Files
Back to menu

fetch module

This module does the opposite of the copy module:

- ansible.builtin.fetch:
    src: /etc/myfile.conf
    dest: /data/ansible/backup/myfile-{{ inventory_hostname }}.conf
    flat: yes
Rocky Linux Academy > Ansible courses > 3 - Management of Files
Back to menu

fetch module

More information can be found at https://docs.ansible.com/ansible/latest/collections/ansible/builtin/fetch_module.html.

Rocky Linux Academy > Ansible courses > 3 - Management of Files
Back to menu



Questions ?

Rocky Linux Academy > Ansible courses > 3 - Management of Files
Back to menu



template module

Rocky Linux Academy > Ansible courses > 3 - Management of Files
Back to menu

template module

Ansible and its template module use the Jinja2 template system (http://jinja.pocoo.org/docs/) to generate files on target hosts.

Rocky Linux Academy > Ansible courses > 3 - Management of Files
Back to menu

template module

For example:

- ansible.builtin.template:
    src: /data/ansible/templates/monfichier.j2
    dest: /etc/myfile.conf
    owner: root
    group: root
    mode: 0644
Rocky Linux Academy > Ansible courses > 3 - Management of Files
Back to menu

template module

It is possible to add a validation step if the targeted service allows it (for example apache with the command apachectl -t):

- template:
    src: /data/ansible/templates/vhost.j2
    dest: /etc/httpd/sites-available/vhost.conf
    owner: root
    group: root
    mode: 0644
    validate: '/usr/sbin/apachectl -t'
Rocky Linux Academy > Ansible courses > 3 - Management of Files
Back to menu

template module

More information can be found at https://docs.ansible.com/ansible/latest/collections/ansible/builtin/template_module.html.

Rocky Linux Academy > Ansible courses > 3 - Management of Files
Back to menu



Questions ?

Rocky Linux Academy > Ansible courses > 3 - Management of Files
Back to menu



get_url module

Rocky Linux Academy > Ansible courses > 3 - Management of Files
Back to menu

get_url module

To upload files from a web site or ftp to one or more hosts, use the get_url module:

- get_url:
    url: http://site.com/archive.zip
    dest: /tmp/archive.zip
    mode: 0640
    checksum: sha256:f772bd36185515581aa9a2e4b38fb97940ff28764900ba708e68286121770e9a
Rocky Linux Academy > Ansible courses > 3 - Management of Files
Back to menu

get_url module

By providing a checksum of the file, the file will not be re-downloaded if it is already present at the destination location and its checksum matches the value provided.

Rocky Linux Academy > Ansible courses > 3 - Management of Files
Back to menu



Questions ?

Rocky Linux Academy > Ansible courses > 3 - Management of Files
Back to menu








Next Chapter >>

Rocky Linux Academy > Ansible courses > 3 - Management of Files