Ansible is a “Configuration Management” tool, ie a tool that supports, configures, and installs the system automatically. These tools enable smooth system implementation, reduce repetitive tasks, save time, and can be deployed to similar systems.
One of the key concepts in ansible is modules . Modules are a core component of Ansible, it is a block / processing unit of a task (the task will add variables and parameters to make the processing on the remote server more flexible). In Ansible, instead of saying, “Ansible, execute this command!” . then you will say: “Ansible, execute this module and allow it to run whatever command it needs to get the job done!” . Through the process of learning and using Ansible, I will together with you summarize some basic, frequently used Ansible modules that you should know!
Module 1: Package management
This module is designed for most of the popular package management (package management) such as apt, dnf, etc., allowing you to install packages on your system. In addition to the specific functions of each package manager, most call managers can install, update, upgrade, downgrade, remove, and list packages.
For example, here I have a task that is to install the httpd package on CentOS / RHEL 8 using the dnf package manager.
1 2 3 4 5 6 7 8 9 10 | - hosts: all user: ansible become: True tasks: - name: Install httpd package dnf: name: httpd state: present update_cache: True |
Module 2: Flie
In Ansible, there are many modules that work with files, folders, links on destination nodes (client node) such as copy, template, file, … which are commonly used. First, we will learn about module files. The file module helps to manage the file and its properties. In addition to creating, deleting, and locating module files also set the rights and ownership or set symlinks for the file.
For example,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | - hosts: all user: ansible become: True tasks: - name: Change file ownership, group and permissions file: path: /etc/thuntt.config owner: thuntt group: thuntt mode: '0644' - name: Create an insecure file file: path: /viblo owner: root group: root mode: '1777' - name: Create a symbolic link file: src: /file/to/link/to dest: /path/to/symlink owner: thuntt group: thuntt state: link |
Module 3: Template
There are many different ways with Ansible to help you manipulate the contents of the file, in addition to the template module, I also find the lineinfile module is also very popular and used a lot. However, for me, after using it, I feel that the template module is much clearer and easier to understand than the lineinfile module.
The template in Ansible is a file that contains all of your configuration parameters, but the dynamic values are provided as variables. During playbook execution, the variables can be replaced with values you need. Additionally, you can do a lot more than replace variables, with the help of the Jinj2 template generator. You can have conditional statements, loops, write macros, filters to transform data, do arithmetic operations, etc. Template files will usually have the extension .j2
. Variables in the template file will be denoted with double quotes, ‘{{variable}}’.
For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | - hosts: all vars: variable_to_be_replaced: 'Hello world' inline_variable: 'hello again' tasks: - name: Ansible Template Example template: src: hello_world.j2 dest: /Users/mdtutorials2/Documents/Ansible/hello_world.txt hello_world.j2 -------------- {{ variable_to_be_replaced }} This line won't be changed Variable given as inline - {{ inline_variable }} - :) output - hello_world.txt ------ Hello world This line won't be changed Variable given as inline - hello again - :) mdtutorials2$ ls -lrt hello_world.txt -rw-r--r-- 1 root wheel 81 Oct 16 07:23 hello_world.txt |
Module 4: Copy
Copy module is the module commonly used when we want to copy a file from the Ansible server (Management node) to the destination node (client node).
For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | - name: copy file from local to remote with owner, group and file permissions (octal) copy: src: test_file dest: $HOME/test_file owner: thuntt group: thuntt mode: 0644 - name: copy file from local to remote with owner, group and file permissions (octal) copy: src: test_file dest: $HOME/test_file owner: thuntt group: thuntt mode: 0644 - name: copy file from local to remote with root as the owner (become required) copy: src: test_file dest: "/home/{{ ansible_user }}/test_file" owner: root group: root mode: 0644 become: true |
Modlue 5: Service
For Unix / Linux client nodes, the service module is a very useful module to control the services running on these servers. Like other modules, the service module also comes with some parameters and these parameters have their own options or values accordingly. Using these parameters and required values, you can manage services with functions such as stop, start, reload, … on client nodes.
For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | - name: Start service httpd, if not running service: name: httpd state: started - name: Stop service httpd, if running service: name: httpd state: stopped - name: Restart service httpd, in all cases service: name: httpd state: restarted - name: Reload service httpd, in all cases service: name: httpd state: reloaded - name: Enable service httpd, and not touch the running state service: name: httpd enabled: yes |
Module 6: shell
In Ansible, we have a shell module that is used to run commands through shell (/ bin / sh) on remote target machines. This module takes commands as input along with a set of arguments.
For example:
1 2 3 4 5 6 7 8 | - name: Execute the command in remote shell; stdout goes to the specified file on the remote. shell: somescript.sh >> somelog.txt - name: Change the working directory to somedir/ before executing the command. shell: somescript.sh >> somelog.txt args: chdir: somedir/ |
Conclusion
At this point, hopefully you can remember and use some of the basic Ansible modules outlined above. Ansible comes bundled with thousands of different available modules and is still expanding, you can read more here . Besides, with Ansible, you can also write your own Ansible modules to suit your needs.