Ansible-Ad-hoc | Modules | Host patterns

KUSHAGRA BANSAL
Nerd For Tech
Published in
4 min readJun 4, 2021

Prerequisite:

node2 [172–21–32–33] node1 [172–31–42–50] server [172.31.38.62]

Host Patterns =>

Here, we check the numbers of nodes connected to our server in any pattern.

Commands:

ansible all — lists-hosts
ansible — lists-hosts
ansible [0] — lists-hosts

where
[0] means first node
[1] means second node
[-1] means last node
[-2]
means second last node
1:4] means second node to fifth node

Ad-hoc | Modules | playbook in Ansible

Ad-hoc commands are simply Linux commands. Due to no idempotency, it executes the file again and again. Here, idempotency means the execution is done every time either it is already executed or not i.e. no overwrites of file.
Module means single work done by a single command while the collection of modules execute are known as playbooks.

Ad-hoc:
They can be run individually to perform quick functions.
These ad-hoc commands are not used for configuration management and deployment because commands are one-time usage.
The Ansible ad-hoc commands use /usr/bin/ansible the command-line tool to automate a single task.

Commands:

ansible developers -a “sudo yum install httpd -y”

ansible developers -a “sudo yum remove httpd -y”
OR
ansible developers -ba “yum remove httpd -y”

Ansible Modules:

Ansible ships with several modules (called ‘module library’) that can be executed directly on remote hosts or through ‘playbooks’.
Also, the library of modules can reside on any machine and no servers, daemon, or database required.
Ansible modules are stored in an inventory file and the default location is /etc/ansible/hosts
Idempotency can be achieve by modules.

Commands of Ansible modules:

ansible -b -m yum -a “pkg = httpd state = present”
// to uninstall state = absent, to update state=latest, to start the service state=started

If run this command again:

ansible developers -b -m user -a “name=kush”
// create a user by name kush

ansible -b -m user -a “name=kush state=absent”
// to delete the user

ansible -b -m copy -a “src=file.txt dest=/tmp”
// copying the file from server to nodes present in group

Idopotency can be achieved by module i.e setup module

ansible developers -m setup
// to check all the present configurations of the node

ansible developers -m setup -a “filter= ipv4” // To check configurations related to ipv4

--

--