Modules are individual units of code that can be used from the command line or in a playbook task. Ansible executes the modules on the remote managed nodes and captures the return values. Each module supports taking arguments as key=value arguments with space as a delimiter. Till now, we have used modules in playbooks like copy, service, ping, command, etc. All modules return JSON format data.
In this guide, we will be learning a few important modules that you should know as a beginner.
Ad-hoc Commands - Execute modules from the command line
You can execute modules from the command line:
ansible webservers -m service -a "name=jenkins state=started" ansible webservers -m ping ansible webservers -m command -a "/sbin/reboot -t now"
From playbooks, Ansible modules are executed in a very similar way:
- name: reboot the servers command: /sbin/reboot -t now
In general, idempotence is “the property of certain operations in mathematics and computer science that can be applied multiple times without changing the result beyond the initial application”. For Ansible it means after 1 run of a module to set things to a desired state, further runs of the same playbook should result in 0 changes. In simplest terms, idempotency means you can be sure of a consistent state in your environment.
For an example, we have to create a user using ‘user’ module, so we will provide the state arguments as ‘present’ so that it does not create the new user again-
user: gaurav state: present
Ansible copy module is used to copy files and folders from the local machine to the remote machine
In the below example, we will copy the test.yml file in the local machine, to the destination is the /tmp/ directory on the remote machine default permission for the remote file will be set as 0664 as we are not specifying any permissions.
- name: this is our first play. hosts: webserver1 tasks: - name: "create a dummy file on websever1" copy: src=test.yml dest=/tmp/
If you want to specify the owner and group, and permissions for the specified file, you can modify the playbook as:
- name: this is our first play. hosts: webserver1 tasks: - name: "create a dummy file on websever1" copy: src=test.yml dest=/tmp/ owner=gaurav group=gaurav mode=0644
Ansible lineinfile module is used to insert a line, modify, remove, and replace an existing line.
This module comes handy when you work with the file and modify their content like adding a new line in the file or updating, replacing a line in the file when specific text is found, etc. The best feature of this module is that before appending, it checks whether the same entry already exists or not which helps avoiding any duplicates.
- name: this is our first play. hosts: webserver1 tasks: - name: "create a dummy file on websever1" lineinfile: path=/tmp/test.txt line="gaurav"
This will add the line to the defined path.
Ansible command module is used to run any commands or run any scripts in the remote server.
- name: this is our first play. hosts: webserver1 tasks: - name: Run the command if the specified file does not exist. command: touch /tmp/test.txt
Ansible script module runs a local script on a remote server after transferring it. This module takes the script name followed by a list of space-delimited arguments.
The local script at path will be transferred to the remote machine and then executed.
- name: this is our first play. hosts: webserver1 tasks: - name: "create a dummy file on websever1" script: testScript.sh Gaurav
Ansible service module controls services on remote hosts. It can be used to start, stop or check the status of the service on the remote server. Here the state is stopped rather than stop because of the idempotency concept.
- name: this is our first play. hosts: webserver1 tasks: - name: "create a dummy file on websever1" service: name: apache2 state: started #stopped
Package management module
There is a module for most popular package managers, such as DNF and APT, to enable you to install any package on a system.
- name: this is our first play. hosts: webserver1 tasks: - name: install the latest version of Apache and MariaDB dnf: name: - httpd - mariadb-server state: latest
This installs the Apache web server and the MariaDB SQL database.
Ansible user module helps us to manage user accounts in Linux systems including creating user accounts, deleting them, setting passwords, adding groups to each user, etc.
- name: this is our first play. hosts: webserver1 tasks: - name: "create a dummy file on websever1" user: name="mytestUser" state=present password="$6$A0JtS1g5vFZ$O/WkOD/ZyHTLm5Yn5HT213gLSwTzxLTT/V4D2ly0JYcdRvhDQV.HAMsiVHkYj5WhHdaaU/WwMDxgTW0pHQzrF0"
This playbook will create a user ‘mytestuser’ with the encrypted password provided in the playbook.
There are a variety of modules provided by Ansible, refer to the Ansible official documentation for more details.