Introduction
As you may have seen in my previous article how to use Ansible (https://www.linkedin.com/pulse/configuration-automation-ansible-beginners-lionel-gurret/), we can use many modules to configure and developp our playbooks.
Here are in my opinion the 10 most useful Ansible modules you should know (on Linux environments) !
The Ping Module
The first thing you probably want to do is th check wheter the connection with our host(s) defined in the inventory file is established or not. It does not make sense in playbooks, but it is useful.
Example
# Example from an Ansible Playbook for Linux - ping: # Example from an Ansible Playbook for Windows - win_ping:
It will return “pong” on successful contact.
The Include Module
Use this module when you want to include another playbook or tasks in your current playbook.
Example
- hosts: localhost tasks: - debug: msg: test - name: Include a play after another play include: other_playbook.yaml
The Debug Module
In my opinion the most important module, it will help you to print a state,emton hosts. It will help you to debug your playbooks with its variables or expressions. Specially when you use “when:” statements
Example
- debug: msg: "The current value of our variable is : {{ variable_value }}"
The Set_Fact Module
This module allows setting new variables. Variables are set on a host-by-host basis just like facts discovered by the setup module. These variables will be available to subsequent plays during an ansible-playbook run, but will not be saved across executions even if you use a fact cache.
Example
# Example setting simple host fact - set_fact: my_variable: "a value"
The Service Module
Another very useful module, it will help you to manage services on your host(s).
Example
- name: Start service httpd, if not started service: name: httpd state: started - name: Stop service httpd, if started service: name: httpd state: stopped - name: Restart service httpd, in all cases service: name: httpd state: restarted
The Shell / Command Modules
We can use the “shell” module when we need to execute a command in remote servers, in the shell of our choice. By default, the commands are run on the /bin/sh shell. You can make use of the various operations like ‘|’,'<‘,’>’ etc. and environment variables like $HOME.
If you don’t need those, you should use the “command” module instead. The command module does not process the commands through a shell. So it doesn’t support the above-mentioned operations.
Example
- name: Executing a Command Using Shell Module shell: ls -lrt > temp.txt - name: Executing a command using command module command: cat hello.txt
The Template / Copy Modules
The Template module is used to copy a configuration file from the local system to the host server. It is the same as the copy module, but it dynamically binds group variables defined by us. So it depends of your need but basically if you need to configure variables in the pushed file, use “template”.
Example
- template: src: /mytemplates/foo.j2 dest: /etc/file.conf owner: root group: root mode: 0644 - name: example copying file with owner and permissions copy: src: /srv/myfiles/foo.conf dest: /etc/foo.conf owner: foo group: foo mode: 0644
The Yum Module
Installs, upgrade, downgrades, removes, and lists packages and groups with the yum package manager.
Example
- name: install the latest version of Apache yum: name: httpd state: latest - name: upgrade all packages yum: name: '*' state: latest - name: remove the Apache package yum: name: httpd state: absent
The Lineinfile / Replace Modules
- “Lineinfile” module ensures a particular line is in a file, or replace an existing line using a back-referenced regular expression. This is primarily useful when you want to change a single line in a file only.
- The “replace” module if you want to change multiple, similar lines.
Example
# Replace SELINUX= by SELINUX=enforcing - lineinfile: path: /etc/selinux/config regexp: '^SELINUX=' line: 'SELINUX=enforcing' # Remove Regexp from file - lineinfile: path: /etc/sudoers state: absent regexp: '^%wheel' # Replace Regexp by another line - replace: path: /etc/hosts regexp: '(\s+)old\.host\.name(\s+.*)?$' replace: '\1new.host.name\2'
The URI Module
Interacts with HTTP and HTTPS web services and supports Digest, Basic and WSSE HTTP authentication mechanisms. So it’s really useful to call API.
Example
- name: Create a JIRA issue uri: url: https://your.jira.example.com/rest/api/2/issue/ method: POST user: your_username password: your_pass body: "{{ lookup('file','issue.json') }}" force_basic_auth: yes status_code: 201 body_format: json - name: POST from contents of remote file uri: url: "https://httpbin.org/post" method: POST src: /path/to/my/file.json remote_src: true