Technology Security Analyst
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.
$ansible [pattern] -m [module] -a “[module options]”
Given below is the example mentioned:
$ansible ansible_client.lab.com -m ping
In the above command, we used ‘ping’ module to test connection between master server and client named ‘ansible_client.lab.com’.
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.
$ansible webservers –m setup
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.
$ansible webservers -m setup -a "filter=ansible_nodename"
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.
$ansibletest_group -b -m yum -a "name=nginx state=latest"
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.
$ansibletest_group -b -m yum -a "name=nginx state=absent"
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.
$ansibletest_group -m file -a "path=/root/my_testfile state=touch”
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.
$ 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.
$ansibletest_group -m file -a "path=/root/my_testfile mode=0400"
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.
$ansible webservers -i inventory.ini -b -a "/sbin/reboot" -f 10
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.
$ansible webservers -m shell -a ‘df -h’
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.
Subscribe to get latest updates