Ansible Modules

  • Ansible works by connecting to your nodes and pushing out small programs, called modules.

  • Ansible modules are categorized into various groups based on their functionality

  • Ansible modules should follow the idempotency principle, which means that consecutive runs of the same module should have the same effect if nothing else changes.

  • Modules return data structures in JSON data. We can store these return values in variables and use them in other tasks or display them in the console.

Types of Ansible Modules

  1. Core Modules

    The Core Ansible team is responsible for maintaining these modules thus these come with Ansible itself

  2. Extras Modules

    The Ansible community maintains these modules

Common Ansible Modules

  1. yum and apt module
  • Both apt and yum is part of ansible core modules.

  • apt module is used for managing packages for Debian/Ubuntu Linux distributions.

  • yum module is used for managing packages for RHEL/Centos/Fedora Linux distributions.

# Installing package using apt module

---
- name: Playbook to install NGINX
  hosts: localhost
  become: yes
  tasks:
    - name: Installing nginx using apt module
      apt:
        name: nginx
        state: present
# Installing package using yum module

---
- name: Playbook to install Git
  hosts: localhost
  become: yes
  tasks:
    - name: Installing Git using yum module
      yum:
        name: git
        state: present
# Installing multiple packages

---
- name: Playbook to install multiple packages
  hosts: localhost
  become: yes
  tasks:
    - name: Installing multiple using yum module
      yum:
        name:
          - git
          - wget
          - httpd
        state: present
# Removing package

---
- name: Playbook to remove package
  hosts: localhost
  become: yes
  tasks:
    - name: Removing package using yum module
      yum:
        name: wget
        state: absent
  1. Service module
  • The service module is used to control services on remote hosts.

  • Using this module we can start, stop, and restart a service in a remote host.

# Start, stop, or restart a service on a remote host

---
- hosts: localhost
  gather_facts: not
  become: true
  tasks:
    - name: ensure nginx service is started
      service:
        name: nginx
        state: started
    - name: ensure nginx service is stopped
      service:
        name: nginx
        state: stopped
    - name: restart nginx service
      service:
        name: nginx
        state: restarted
  1. File Module
  • Ansible file module can -

  • create files, directories, and symlinks

  • Delete files, directories, and symlinks

  • Changing permission of files and directories

  • state: directory - ensures that path is a directory, but will throw an error if the path does not exist or if the path is not a directory.

# creating, deleting, and modifying permission of a file
---
- hosts: localhost
  gather_facts: no
  become: true
  tasks:
    - name: create an empty file if it doesn't exist
      file:
        path: $HOME/demo_file  # Path where empty file is created
        state: touch
    - name: delete a file if it exists
      file:
        path: $HOME/demo_file
        state: absent
    - name: modify permissions of a directory
      file:
        path: $HOME/demo_directory/demo1
        state: directory
        owner: root
        group: root
        mode: u=rwx,g=rx,o=rx
  1. command and shell module
  • Commands and shell modules are used to execute commands on remote hosts.

  • The main difference between both commands is that the command module bypasses the local shell and variables like $HOSTNAME and $HOME are not available and also operations like '&', '<' do not work.

  • But all these variables and operations are available in the shell module.

# Command Module example

---  
- name: Command module example
  hosts: localhost
  gather_facts: no
  become: true
  tasks:
    - name: Execute command 'date'
      command: date 
    - name: Display resolv.conf contents
      command: cat /ect/resolv.conf
    - name: Creating directory
      command: mkdir /home/demo_module
# Shell Module example

---
- name: Shell module example
  hosts: localhost
  gather_facts: no
  become: true
  tasks:
    - name: List text files in tmp directory and save result in a file
      shell: "ls -l /tmp | grep .txt > /tmp/dirlist.txt"
    - name: Display the contents of the dirlist file
      shell: cat /tmp/dirlist.txt
      register: displayfile
    - debug:
        msg: "{{ displayfile.stdout_lines }}"
  1. copy Module
  • Used to copy files and directories from the local machine to the remote machine
# Copy Module example

---
- name: Copy files with owner and permission
  hosts: localhost
  gather_facts: no
  become: true
  tasks:
    copy:
      src: test.yaml
      dest: /tmp/
      owner: CJ
      group: admin
      mode: '0755'
  1. lineinfile module
  • lineinfile module is used to find a line in a file and replace it or add it if it doesn't already exist
# lineinfile module example

- name: lineinfile module example
  hosts: localhost
  gather_facts: no
  become: true
  tasks:
    - name: Add DNS server to resolv.conf
      lineinfile:
        path: /ect/resolv.conf 
        line: 'nameserver 8.8.8.8'
  • The best feature of this module is that before appending, it checks whether the same entry already exists or not which helps avoid any duplicates.
  1. Debug module
  • Ansible debug module is used to print the message in the log output.

  • Printing Variable value with ansible debug module.

# To print variable value 

- name: printing variable
  debug: 
    var: abcd
# To print variable value with some message

- name: printing variable with adding some extra message
  debug: 
    msg: "Hi this is {{abcd.stdout}}"

I hope you enjoyed this article, if you have any questions, comments, or feedback, or if I made a mistake feel free to comment down below.


Did you find this article valuable?

Support DevOps With CJ by becoming a sponsor. Any amount is appreciated!