10 Easy To Use Modules In Ansible

Reading Time: 2 minutes

Ansible is all about using modules in its playbook.

Here are my top 10 commonly used modules:-

Before reading this blog I would like to explain some terminologies used below:-

  1. test-servers:- It is the group of my hosts which I have written in my inventory file.



This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters


[test-servers]
18.223.152.37
18.191.108.32
18.222.226.59
view raw

gistfile1.txt

hosted with ❤ by GitHub

2. – m XYZ:- XYZ is the module name

3. -u ec2-user:- to define username that is ec2-user

4. SUCCESS:-  means that my module has done its task on destination nodes.

5. Changed:-  If it is true something is changed on destination nodes.

If it is false something is not changed on destination nodes.

Let’s start with modules:-

  1. ping module:- Used when we want to check whether the connection with our hosts defined in my inventory file is established or not.

Command :- ansible test-servers -m ping -u ec2-user



This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters


18.191.108.32 | SUCCESS => {
"changed": false,
"ping": "pong"
}
18.223.152.37 | SUCCESS => {
"changed": false,
"ping": "pong"
}
18.222.226.59 | SUCCESS => {
"changed": false,
"ping": "pong"
}
view raw

gistfile1.txt

hosted with ❤ by GitHub

ping changes to pong which means ssh connection is established.

2. Setup module:- Used when we want to see the information of our all the hosts their configuration and detailed information.

Command :- ansible test-servers -m setup -u ec2-user



This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters


18.223.152.37 | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"172.31.24.96"
],
"ansible_all_ipv6_addresses": [
"fe80::4e3:8bff:fec0:80bc"
],
"ansible_apparmor": {
"status": "disabled"
},
view raw

gistfile1.txt

hosted with ❤ by GitHub

This is the snapshot of the configuration of my machine running on AWS.

3. Copy module:- Often used in writing playbooks when we want to copy a file from remote server to destination nodes.
Example:- Suppose we want to copy a file from a remote server to all destination machines.

Command :- ansible test-servers -m copy -a 'src=/home/knoldus/Personal/blogs/blog3.txt dest=/tmp' -u ec2-user



This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters


18.223.152.37 | SUCCESS => {
"changed": true,
"checksum": "2274510a91dabeef3ca39083104d1544c218129b",
"dest": "/tmp/blog3.txt",
"gid": 1000,
"group": "ec2-user",
"md5sum": "a74feaac59d7c23f40cff5ea1e7baece",
"mode": "0664",
"owner": "ec2-user",
"secontext": "unconfined_u:object_r:user_home_t:s0",
"size": 2512,
"src": "/home/ec2-user/.ansible/tmp/ansible-tmp-1538636902.09-174963264765242/source",
"state": "file",
"uid": 1000
}
view raw

gistfile1.txt

hosted with ❤ by GitHub

4. Yum module:- To install a service we use Yum module

Command:- ansible test-servers -m yum -a 'name=httpd state=present' -become -u ec2-user



This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters


18.223.152.37 | SUCCESS => {
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Loaded plugins: amazon-id, rhui-lb, search-disabled-repos\nResolving Dependencies\n–> Running transaction check\n—> Package httpd.x86_64 0:2.4.6-80.el7_5.1 will be installed\n–> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nInstalling:\n httpd x86_64 2.4.6-80.el7_5.1 rhui-REGION-rhel-server-releases 1.2 M\n\nTransaction Summary\n================================================================================\nInstall 1 Package\n\nTotal download size: 1.2 M\nInstalled size: 3.7 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Installing : httpd-2.4.6-80.el7_5.1.x86_64 1/1 \n Verifying : httpd-2.4.6-80.el7_5.1.x86_64 1/1 \n\nInstalled:\n httpd.x86_64 0:2.4.6-80.el7_5.1 \n\nComplete!\n"
]
}
view raw

gistfile1.txt

hosted with ❤ by GitHub

Apache2 will be installed in our machines.

The key point to note here is that we have to use -become which is new in 2.6 version before we have to use -s.

5. Shell module:- When we want to run UNIX commands then we use shell module

Command:- ansible test-servers -m shell -a 'ls -la' -u ec2-user



This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters


18.191.108.32 | SUCCESS | rc=0 >>
total 16
drwx——. 5 ec2-user ec2-user 123 Oct 3 06:47 .
drwxr-xr-x. 3 root root 22 Sep 28 10:16 ..
drwx——. 3 ec2-user ec2-user 17 Oct 1 07:18 .ansible
-rw——-. 1 ec2-user ec2-user 73 Oct 1 07:13 .bash_history
-rw-r–r–. 1 ec2-user ec2-user 18 Sep 26 2017 .bash_logout
-rw-r–r–. 1 ec2-user ec2-user 193 Sep 26 2017 .bash_profile
-rw-r–r–. 1 ec2-user ec2-user 231 Sep 26 2017 .bashrc
drwxrw—-. 3 ec2-user ec2-user 19 Oct 3 06:47 .pki
drwx——. 2 ec2-user ec2-user 29 Oct 1 07:12 .ssh
view raw

gistfile1.txt

hosted with ❤ by GitHub

This will display all the files present in our machine with their permissions.

6. Service module:- When we want to ensure the state of a service that is service is running we use the service module.

Command:- ansible test-servers -m service -a 'name=httpd state=started' -become -u ec2-user



This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters


18.223.152.37 | SUCCESS => {
"changed": true,
"name": "httpd",
"state": "started",
"status": {
"ActiveEnterTimestampMonotonic": "0",
"ActiveExitTimestampMonotonic": "0",
"ActiveState": "inactive",
"After": "nss-lookup.target tmp.mount -.mount system.slice network.target basic.target remote-fs.target systemd-journald.socket",
"AllowIsolate": "no",
"AmbientCapabilities": "0",
"AssertResult": "no",
"AssertTimestampMonotonic": "0",
"Before": "shutdown.target",
"BlockIOAccounting": "no",
"BlockIOWeight": "18446744073709551615",
"CPUAccounting": "no",
view raw

gistfile1.txt

hosted with ❤ by GitHub

Apache2 is up on my machine.

7.Debug module:- To print a msg on hosts we use Debug module.

Command:- ansible test-servers -m debug -a 'msg=Hello' -u ec2-user



This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters


18.223.152.37 | SUCCESS => {
"msg": "Hello"
}
18.191.108.32 | SUCCESS => {
"msg": "Hello"
}
18.222.226.59 | SUCCESS => {
"msg": "Hello"
}
view raw

gistfile1.txt

hosted with ❤ by GitHub

Hello, a message is printed on my machine.

8. Template module:- It 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.

Here I have vars in my source machine.



This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters


– name: Use test-servers
hosts: test-servers
become: yes
become_user: root
vars:
source: /home/knoldus/Personal/blogs/Ansible/blog1.txt
destination: /tmp
tasks:
– name: copy local files to remote machines
template:
src: "{{ source }}"
dest: "{{ destination }}"
owner: root
group: root
view raw

gistfile1.txt

hosted with ❤ by GitHub

9. Include module:– When we want to include another playbook in our playbook then we use the Include module.



This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters


– name: Gather hosts
hosts: test-servers
tasks:
– debug:
msg: Hello Ist Play
– name: include a playbook
include: sample.yml
view raw

gistfile1.txt

hosted with ❤ by GitHub

10. User module:- To add a particular user to our module we can use User module



This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters


– name: " Gathering information about hosts"
hosts: test-servers
become: yes
become_user: root
tasks:
– user:
name: Sachin
comment: "Admin"
view raw

gistfile1.txt

hosted with ❤ by GitHub

Here we have added a user named Sachin to our module.

Written by 

Sachin is a software consultant having more than 0.5 years of experience. He loves to learn trending technologies. Sachin is familiar with languages such as Java, Scala and is currently working on DevOps technologies /tools like Kubernetes, Jenkins, Ansible, Docker, Bamboo.