Top 10 Ansible Modules

Top 10 Ansible Modules

Table of contents

No heading

No headings in the article.

Module 1: Package management

There is a module for the most popular package managers, such as DNF and APT, to enable us to install any package on a system. Functionality depends entirely on the package manager, but usually, these modules can install, upgrade, downgrade, remove, and list the packages.

Example -

  • This installs the Apache web server and the MariaDB SQL database:

      - name: install the latest version of Apache and MariaDB
       dnf:
         name:
           - httpd
           - mariadb-server
         state: latest
    

Module 2: Service

The Service module enables us to start, stop and restart the installed packages.

Example -

  • This starts the service:
- name: Start service <service>, based on running process /usr/bin/
 service:
   name: service
   pattern: /usr/bin/
   state: started

Module 3: Copy

The copy module is used to copy the file from local/remote to a particular location.

Example -

  • This copies the file:
- name: Copy file
 copy:
   src: /srv/myfiles/gaurav.conf
   dest: /etc/gaurav.conf
   owner: gaurav
   group: gaurav
   mode: u=rw,g=r,o=r

Module 4: Debug

Debug module is used to print the statement during execution and it is very useful for debugging the variables.

Example -

  • This displays all the variables defined for the host in the inventory file:
- name: Display all variables/facts known for a host
 debug:
   var: hostvars[inventory_hostname]
   verbosity: 4

Module 5: File

The file module manages the file and its properties.

  • It sets attributes of files, symlinks, or directories.

  • It also removes files, symlinks, or directories.

Example -

  • This creates a file gaurav.conf and sets the permission to 0644:
- name: Change file ownership, group and permissions
 file:
   path: /etc/gaurav.conf
   owner: gaurav
   group: gaurav
   mode: '0644'
  • This creates a directory gaurav_directory and sets the permission to 0755:
- name: Create a directory if it does not exist
 file:
   path: /etc/gaurav_directory
   state: directory
   mode: '0755'

Module 6: Line in file

The Line in file module manages lines in a text file.

  • It ensures a particular line is in a file or replaces an existing line using a back-referenced regular expression.

  • It's primarily useful when you want to change just a single line in a file.

Example -

  • This sets the value of SELINUX=enforcing:
- name: Ensure SELinux is set to enforcing mode
 lineinfile:
   path: /etc/selinux/config
   regexp: '^SELINUX='
   line: SELINUX=enforcing
  • This adds the entry of IP & Hostname in resolve.config:
- name: Add a line to a file if the file does not exist
 lineinfile:
   path: /etc/resolv.conf
   line: 192.168.1.99 gaurav.lab.net
   create: yes

Module 7: Git

The git module manages the git checkouts for the deployments.

Example -

  • This creates an archive from git repo:
- git:
   repo: https://github.com/ansible/ansible-examples.git
   dest: /src/ansible-examples
   archive: /tmp/ansible-examples.zip
  • This clones a repo in a separate git directory:
- git:
   repo: https://github.com/ansible/ansible-examples.git
   dest: /src/ansible-examples
   separate_git_dir: /src/ansible-examples.git

Module 8: CLI Command

CLI Command module is used to push text-based configuration to network devices.

Example -

  • This sets the hostname for a switch and exits with the commit message:
- name: commit with comment
 cli_config:
   config: set system host-name foo
   commit_comment: this is a test
  • This takes the backup of the config to a different destination:
- name: configurable backup path
 cli_config:
   config: "{{ lookup('template', 'basic/config.j2') }}"
   backup: yes
   backup_options:
     filename: backup.cfg
     dir_path: /home/user

Module 9: Archive

The Archive module is used to create the compressed archive of one or more files.

Example -

  • This creates the compressed archive:
- name: Compress directory /path/to/gaurav/ into /path/to/gaurav.tgz
 archive:
   path: /path/to/gaurav
   dest: /path/to/gaurav.tgz

Module 10: Command Module

The Command module is used to take the command names followed by the list arguments.

Example -

  • This takes the command names followed by the list arguments:
- name: return motd to registered var
 command: cat /etc/motd
 register: mymotd

Thanks for going through this article.

See you next time :)