Ansible Module Examples
Now we will know about a few different modules. They are good to know and learn when we start or learn Ansible. The modules we will discuss are the debug, package, firewall, user, template and shell/command. For each module, we will describe what they will do, and we will have some samples to discuss together.
Debug
Let us start with our first module, "debug". The debug module is handy when we want to display stuff on the screen or when it does not work. We want to see the version, see if a step succeeded, and know why a condition was not met. We will then use the debug message to put it on the screen. Let us have a look at an example.
debug
msg: This is a debug message
debug
msg: "This is a debug message ((variabale}}"
debug
var: output
name: This is a debug task with a name attached.
debug
msg: "This is a debug message {(variabale}}"
The most simple example is the first one here. Just make a task called debug and add a message parameter. We must use quotes around the whole string to use a variable in the second example. If we want to display a variable, then we can use debug var option. Starting all tasks in our playbooks with a name tag is good practice.
So the last example starts with a name tag. this is a debug task with the name attached, which is just lovely to see. Otherwise, also in the login, we will only see debug, and we will see the whole message. Which is a little bit nicer, and as we said, it is an excellent practice to work like that.
Package
The next module we are going to discuss is the "package". Use the package module when we need to install an OS package. No need to know what package manager is used. However, we can specify it, but it is not needed. Let us have a look at some examples.
name: install the latest version of telnet
package:
name: [telnet, tcsh]
state: latest
name: just install telnet, don't care about the version
package:
name: telnet
state: present
name: make sure no telnet is available
package:
name: telnet
state: absent
Here we have three examples. The first example installs two packages. "telnet" and "c shell". It will inform Ansible to install the latest version available. The second example tells it to install telnet. However, it does not specify the latest. It just says to make sure it is installed. The last example is how we can say it must not be installed. So it must be absent, which is the option we can use. The state is absent, and depending on our playbook, we can use the latest or present depending on the speed we need. For example, telnet, we could make sure it is present because it is not that important that we have the latest features of telnet.
Firewalld
The next module we will look at is a firewall daemon. Manage firewall ports from our code. Quickly easily, and documented. Open ports temporarily or indefinite. Let us have a look at some examples.
name: Close port 80 permanently
firewalld:
port: 80/tcp
permanent: yes
state: disabled
name: "Open port 443 for HTTPS immediately"
firewalld
port: 443/tcp
permanent: yes
state: enabled
immediate: yes
name: "Open port {{port}} from variable port"
firewalld:
port: "{{port}}/tcp"
permanent: yes
state: enabled
immediate: yes
We have three examples. Option one is to close port 80 and make this permanent. The second example makes sure that the HTTPS port 443 is opened immediately. So in the previous example, port 80 will stay active until the firewall daemon has reloaded the rules or has been restarted. The second example makes it active immediately, and we can also use variables for ports.
User
Then we move on to the next module, the user module. The module to create, change and delete users on Linux. We have three examples here.
name: Create user CodingNinjasIndia
user
name: CodingNinjasIndia
comment: CodingNinjasIndia admin user
name: Create user CodingNinjasIndia part of group youtube (and adm)
user:
name: CodingNinjasIndia
comment: CodingNinjasIndia admin user
group: youtube
groups: adm
name: Create group youtube
group:
name: youtube
state: present
We create a user "CodingNinjasIndia", and we can give it a name. There are comments. We can also add groups to the user, so in this example, we add the group youtube and the group "adm". so a Linux user can be part of multiple groups. In the last example, we also show how to create a group. We can also specify group ids and user ids if the home directory needs to be in a different place Etc.
We can also remove users by making them absent. So we have got another state tag. We can then say absent. We can set passwords. Everything is possible to manage the users in this way.
Template
Then we move over to the next module, which is the template. We can use templates to move files from the code to the servers and change the variables in those templates, which is very handy.
name: Copy 01-CodingNinjasIndia file to the sudoers dir
template:
src: "templates/01-CodingNinjasIndia.j2"
dest: "/etc/sudoers.d/01-CodingNinjasIndia"
owner: root
group: root
mode: '0644'
# Managed by Ansible Automation
# Source location: templates/01-CodingNinjasIndia.j2
#Target location: /etc/sudoers.d/01-CodingNinjasIndia
%youtube ALL=({{application} }adm) NOPASSWD: ALL
In this example, we have only one example here. However, we create a new sudoers file, allowing the group youtube to become an application adm user and execute all commands without a password. The template is stated below the example and is in the code in the templates directory. The destination is on the remote server where we run the code. We can set it on our group mode; the file will be copied in this example. The application variable will be replaced with the application variable value. Which, for example, is stated in our inventory or playbook.
Shell
The following module is "shell". We can use it to execute OS commands to do virtually anything. Register the output,t return code or error message.
name: Install db only if /db/data/{{db_name}} not there
shell:
cmd: "{{software_share_dir}}/install"
creates: "/db/data/{{db_name}}"
name: Install db and register output
shell:
cmd: "{{software_share_dir}}/install"
creates: "/db/data/{{db_name}}
register: dbinstall
name: Execute uptime in csh specific
shell: uptime
args:
executable: /bin/csh
name: Execute uptime in csh specific
shell: echo $youtube_channel
environment:
youtube_channel: CodingNinjasIndia
youtube show: automation_show
Here we have four examples. In the first example, we install the db as an example. We install a database with the command install, and this will create the directory "db/data/{{db name}}". When this directory already exists, it will not execute the task.
In the following example, we are also registering the output. So we are going to install the database again using the install command, and the output will be in the db install variable. With the debug command, we can print out what comes out. And then, we can see the standard error, standard out, return code, what was the exact command, what arguments were given, Etc.
The third example is where we can also say which shell to use when executing the code. So in this example, the c shell is given. The last example shows how to add environment variables to the remotely executing script.
Frequently Asked Questions
How do we get a list of Ansible modules?
The ansible-doc command is a handy feature of Ansible. This command will provide a list of the system's installed modules.
How do we find an Ansible module code?
We can look for it in the lib/ansible/module_utils directory under the primary Ansible path. This is where we can find the module utility source code.
How to debug an ansible module?
Using epdb is the most straightforward approach to running a debugger in a module, whether it be local or remote. To use this, add the code "import epdb; epdb. serve()" to the module's control node at the desired breakpoint. Run epdb to establish an association with the debugger.
Conclusion
In the article, we read about Ansible Modules. These were the ansible modules that we use commonly. We also read about the types of Ansible modules and their uses. There are always more choices of ansible modules and more things to learn. Read articles on Ansible and Devops here. Find stuff on cloud, Azure and AWS. Explore Coding Ninjas Studio to find more exciting stuff. Happy Coding!
