The Advert-Hoc command is the one-liner ansible command that performs one process on the goal host. It lets you execute easy one-line process in opposition to one or group of hosts outlined on the stock file configuration. An Advert-Hoc command will solely have two parameters, the group of a bunch that you simply wish to carry out the duty and the Ansible module to run.
The Advert-Hoc command offers you extra benefit for exploring ansible itself. You’ll be able to carry out duties with out making a playbook first, equivalent to rebooting servers, managing providers, enhancing the road configuration, copy a file to just one host, set up just one package deal.
On this tutorial, I’ll present the fundamental utilization of the Ansible Advert-Hoc command. I might be utilizing the Advert-Hoc command to carry out easy duties which may be wanted in your every day foundation as a system administrator.
Stipulations
For this information, we might be utilizing two Ubuntu 18.04 LTS servers, Bionic Beaver. The ansible server will get the hostname ‘ansible-node’ with the IP handle 10.5.5.20 and the Provision server with the hostname ‘provision’ and the IP handle 10.5.5.21.
What we’ll do?
- Primary Advert-Hoc Command
- File Switch
- Replace and Improve
- Handle Package deal
- Handle Companies
- Checking System
Primary Advert-Hoc Command Utilization
Firstly, we’ll study the fundamental utilization of Ansible Advert-Hoc for managing servers. We’ll study the Ansible Advert-Hoc fundamental command, utilizing Advert-Hoc command with ssh password authentication, the privilege escalation, and utilizing Advert-Hoc command in opposition to a bunch of hosts.
1. Primary Command
The essential command of ansible ad-hoc in opposition to ‘all’ hosts on the stock file and utilizing the ‘ping’ module.
ansible all -m ping
- The primary parameter ‘all’ for all hosts on the stock file.
- The second parameter contained in the ‘-m’ possibility for the module, working the ping module.
Now you’re going to get the end result as beneath.
An Advert-Hoc command in opposition to the provisioning server has been ‘SUCCESS’ with none modifications made on the server and we get the results of the ‘ping’ module from the provisioning server ‘pong’.
2. Filter Hosts Group and Single Host
Now you need to use the Advert-Hoc command in opposition to a bunch of hosts which are already outlined on the stock file. You should use your customized stock file or utilizing the default stock file ‘/and so on/ansible/hosts’.
Under is an instance to run the ad-hoc command in opposition to the group of hosts known as ‘hakase-testing’ which are already outlined on the default stock configuration file.
ansible hakase-testing -m setup -a "filter=ansible_distribution*"
In the event you’re utilizing the customized stock file, add the ‘-i’ possibility adopted the stock file title.
ansible hakase-testing -i hosts -m setup -a "filter=ansible_distribution*"
You’ll get the identical end result.
Now if you wish to run in opposition to single host on the stock configuration, you need to use the title of the host equivalent to beneath.
ansible provision -m setup -a "filter=ansible_distribution*"
And the ad-hoc command will run on the ‘provision’ server solely.
3. Utilizing SSH Password
Now we’ll carry out an Advert-Hoc command utilizing the prompted ssh password authentication. And to be able to do that, you should set up the extra package deal known as ‘sshpass’ on the ‘ansible-node’.
Set up sshpass package deal utilizing the apt command beneath.
sudo apt set up sshpass -y
Now run the ad-hoc command and add the ‘–ask-pass’ choice to the tip.
ansible hakase-testing -m ping --ask-pass
And you can be requested the ‘SSH Password’ for the server.
Sort your ssh password and the ad-hoc command might be run in opposition to the server.
4. Privilege Escalation
The ansible gives options for the privilege escalation in opposition to servers. If you wish to run the ad-hoc command as a non-root consumer, you need to use the ‘–become’ choice to get the basis privileges and the ‘-Okay’ choice to immediate the password.
Run the ad-hoc command ‘fdisk -l’ as a consumer ‘hakase’ with the privilege possibility ‘–become’ and the ‘-Okay’ to immediate the ‘SUDO Password’.
ansible hakase-testing -m shell -a 'fdisk -l' -u hakase --become -Okay
Under is the end result.
File Switch
Now we will use the Advert-Hoc command for File Switch to and from the server. We will switch a file to the provisioning server with the ‘copy’ module, and obtain file from the server utilizing ‘fetch’ module.
1. Add File to Host
For this instance, we will run the ad-hoc command and utilizing the ‘copy’ module to add the sudoers configuration for consumer hakase to the ‘/and so on/sudoers.d’ listing on the group ‘hakase-testing’.
Run the ad-hoc command beneath.
ansible hakase-testing -m copy -a 'src=/dwelling/hakase/hakase-sudo.conf dest=/and so on/sudoers.d/hakase proprietor=root mode=0644' -u hakase --become -Okay
Now you can be requested the ‘SUDO Password’ for the hakase consumer. Sort the password and you’re going to get the end result as beneath.
The file has been uploaded to the ‘dest’ vacation spot listing, and also you get the ‘modified’ end result as ‘true’.
2. Obtain File from the Host
Now we will use an ad-hoc command with the ‘fetch’ module for downloading the file from the provisioning server to the native ‘ansible-node’ server.
Obtain the configuration file ‘/and so on/sudoers.d/hakase’ from the ‘provision’ server to the native listing known as ‘backup’.
ansible provision -m fetch -a 'src=/and so on/sudoers.d/hakase dest=/dwelling/hakase/backup/hakase-sudoers flat=sure'
And you’re going to get the file known as ‘hakase-sudoers’ on the ‘backup’ listing.
Step 3 – Replace Repository and Improve Packages
To replace and improve the repository of Ubuntu servers, we will use the ad-hoc command with the apt module.
Replace repository on the group hakase-testing.
ansible hakase-testing -m apt -a 'update_cache=sure' --become
Now replace repositories and improve all packages to the most recent model utilizing the ‘improve=dist’ possibility.
ansible hakase-testing -m apt -a 'improve=dist update_cache=sure' --become
Await all packages is being upgraded.
Handle Packages
That is very helpful whenever you’re attempting to construct and debug your personal playbook. As a result of generally you want an extra package deal to be put in on the system. So, this ad-hoc command offers you a straightforward strategy to set up that package deal with out login to every server.
1. Set up Package deal
Set up a single package deal utilizing the ad-hoc command with the apt module as beneath.
ansible hakase-testing -m apt -a 'title=nginx state=newest' --become
2. Take away Package deal
Take away the package deal and purge all configuration associated to the package deal.
ansible hakase-testing -m apt -a 'title=nginx state=absent purge=sure' --become
3. Autoremove
The instance beneath is eradicating the nginx package deal and purge all configuration associated after which take away all unused packages on the system.
ansible hakase-testing -m apt -a 'title=nginx state=absent purge=sure autoremove=sure' --become
Handle Companies
On this step, we will use the service module on the ad-hoc command for managing the system service on the provisioning server.
1. Begin Companies
Begin the nginx service and add it to the boot time.
ansible hakase-testing -m service -a 'title=nginx state=began enabled=sure' --become
You’ll get ‘modified’ and ‘enabled’ end result as ‘true’.
2. Restart Service
If you wish to restart the service, you need to use the next command.
ansible hakase-testing -m service -a 'title=nginx state=restarted' --become
The nginx service has been restarted.
3. Cease a Service
To cease the service, change the ‘state’ worth to ‘stopped’.
ansible hakase-testing -m service -a 'title=nginx state=stopped' --become
The nginx service on the servers ‘hakase-testing’ has been stopped.
Checking the System
Now we will use the ‘shell’ module contained in the ad-hoc command. And we’ll do easy system monitoring utilizing easy Linux command by way of the Ansible ad-hoc.
Firstly, set up the ‘sysstat’ package deal to all servers utilizing the ad-hoc command beneath.
ansible hakase-testing -m apt -a 'title=sysstat state=newest' --become
Await the ‘sysstat’ package deal set up.
As soon as it is full, you are able to verify all servers.
1. Disk Out there
Test the disk out there on the basis partition utilizing the fdisk command.
ansible hakase-testing -m shell -a 'df -h /dev/sda2' --become
Change the ‘/dev/sda2’ with your personal path.
2. RAM Reminiscence Utilization
Now verify the RAM Reminiscence utilization on all servers utilizing the ‘free -m’ command.
ansible hakase-testing -m shell -a 'free -m' --become
And you can be proven the end result as beneath.
3. CPU Utilization
Checking the CPU utilization of all servers utilizing the mpstat command.
ansible hakase-testing -m shell -a 'mpstat -P ALL' --become
The mpstat command is a part of the ‘sysstat’ package deal.
4. Open Ports
Checking the open ports on all system utilizing the netstat by way of the ad-hoc command.
ansible hakase-testing -m shell -a 'netstat -plntu' --become
5. Uptime
Now verify the uptime of every server.
ansible hakase-testing -m shell -a 'uptime' --become
Comments