Ansible ad-hoc Commands

Ansible ad-hoc Commands
Techiio-author
Written by Nilima PaulJanuary 15, 2022
7 min read
Ansible
1 VIEWS 1 LIKES 0 DISLIKES SHARE
1 LIKES 0 DISLIKES 1 VIEWS SHARE
Techiio-author
Nilima Paul

Technology Security Analyst

We will know in this article, what's the Ansible ad-hoc Commands.

Introduction to Ansible ad-hoc Commands

Ansible specially appointed orders are the orders that we can straightforwardly run from the terminal on at least one oversaw customer. It is the least complex and the speediest method for running orders on any customer. It is generally used to test the orders or orders that we don't utilize more than once. It is a joke e order that doesn't need composing a playbook, for instance checking the association of overseen hubs with the Ansible expert server, overseeing documents, administrations, clients, and so forth It shows the straightforwardness and force of Ansible notwithstanding, we can run each module in turn.

Syntax:

$ansible [pattern] -m  [module] -a “[module options]”
  • pattern: where we define managed nodes or groups in our inventory.
  • module: where we define which module of Ansible we want to execute against the pattern.
  • module options: where we pass the options that are required for that module to work and it is optional.

Example of Ansible ad-hoc Commands

Given below is the example mentioned:

Code:

$ansible ansible_client.lab.com -m ping

Explanation:

In the above command, we used ‘ping’ module to test connection between master server and client named ‘ansible_client.lab.com’.

Ansible ad-hoc Commands

Given below are some ansible ad-hoc commands that are used in day to day management of our infrastructure:

1. If we want to collect information of a specific host or group of host.

Example #1

Code:

$ansible webservers –m setup

Explanation:

In the above example, we used ‘setup’ module to get the details about the nodes in the webservers group. We get a lot of data as an output, however, if we are interested in specific fact, we can use ‘filter’ option as below.

Example #2

Code:

$ansible webservers -m setup -a "filter=ansible_nodename"

Explanation:

In the above example, we have filtered out hostname of the server using ‘ansible_nodename’.

2. We can manage packages like installing and uninstalling a package or checking the status of the package on the nodes.

Example #1:

Code:

$ansibletest_group -b -m yum -a "name=nginx state=latest"

Explanation:

In the above example, we have installed ‘nginx’ package to the client node. The ‘-b’ option is used to run the command as root as it requires sudo privilege. We use ‘state=absent’ in the ad-hoc command to uninstall any package as below.

Example #2:

Code:

$ansibletest_group -b -m yum -a "name=nginx state=absent"

Explanation:

In the above example, we can see changed is equal to true. It means ‘nginx’ package has been uninstalled successfully.

3. We can manage files using Ansible ad-hoc commands, for example, creating new files, removing existing files and modifying it etc. We use ‘file’ module to manage files.

Example:

Code:

$ansibletest_group -m file -a "path=/root/my_testfile state=touch”

Explanation:

In the above example, it creates a file named ‘my_testfile’ in the ‘/root’ folder. If we want to get the details of any file, just give the path as below.

Code:

$ ansibletest_group -m file -a "path=/root/my_testfile"

We can also change the mode, owner, and group of the file, if we want as below.

Code:

$ansibletest_group -m file -a "path=/root/my_testfile mode=0400"

Explanation:

In the above example, we have started the ‘httpd’ service using the ‘service’ module on the client node. Just login to the client node to verify that the service is running.

6. We can also reboot our servers using Ansible ad-hoc command as below.

Example:

Code:

$ansible webservers -i inventory.ini -b -a "/sbin/reboot" -f 10

Explanation:

In the above example, we can see that the client node is getting rebooted as soon as we run the command however, getting an error because the host becomes unreachable when it reboots.

7. We can execute shell command on the client nodes using the ‘shell’ module. We need to pay attention to shell quoting rules to avoid any error.

Example:

Code:

$ansible webservers -m shell -a ‘df -h’

Conclusion

Ansible specially appointed Commands are exceptionally valuable when we need to run any order rapidly from the terminal without composing any playbook. We can send the result to a record utilizing redirection and can pipe the result to use by another order like less, more, grep, and so forth It is extremely simple and easy to utilize in any case, it isn't helpful while composing complex undertakings.

Ansible
AnsibleadhocCommands
Python
1 VIEWS 1 LIKES 0 DISLIKES SHARE
1 LIKES 0 DISLIKES 1 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-author
Nilima Paul
Technology Security Analyst
Techiio-followerTechiio-followerTechiio-follower
201 Blog Posts
0 Discussion Threads
Trending Technologies
15
Software40
DevOps46
Frontend Development24
Backend Development20
Server Administration17
Linux Administration26
Data Center24
Sentry24
Terraform23
Ansible83
Docker70
Penetration Testing16
Kubernetes21
NGINX20
JenkinsX17
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance