Ansible using ad-hoc commands

Ansible using ad-hoc commands

An Ansible using Ad-hoc commands are commands that we use only for quick purposes that we don’t want to save for later, such as checking the status of a server or copying a file to the server. This is a good place to start to understand the basics of what Ansible can do prior to learning the playbooks language – ad-hoc commands can also be used to do quick things that you might not necessarily want to write a full playbook for.

Generally speaking, the true power of Ansible lies in playbooks. Why would you use ad-hoc tasks versus playbooks?

For instance, if you wanted to power off all of your lab for Diwali vacation, you could execute a quick one-liner in Ansible without writing a playbook.

How to write an Ansible ad-hoc command?

Ad hoc commands are the easiest way to explore Ansible. They have the following structure:

Syntax:-

ansible <hosts> [-m <module_name>] -a <"arguments"> -u <username> [--become]
  • <hosts> : It can be any entry in the inventory file..
  • -m <module> : It’s an optional parameter. There are hundreds of modules available in Ansible.
  • -a <arguments> : We should pass values that are required by the module.
  • -u <username> : It specifies the user account in which Ansible can execute commands.
  • <Become> : It’s an optional parameter specified when we want to execute operations that need sudo privilege. By default become is false.

Ansible Using Ad-Hoc Commands

Now before running the task, you should make sure passwordless ssh connection has been setup between ansible control machine and the remote machines. For info on how to setup that you can refer

URL :  How to setup passwordless SSH login in Linux

One of the most simple commands used in Ansible is the ping module. It can be used to check the SSH connection between the Ansible control machine and the target machines. This is not ICMP ping.

]$ ansible servers -m ping
ansible using ad-hoc Commands

ansible using ad-hoc Commands

Sometimes you required to check the uptime of many servers and you check it manually. Ansible will help you on this, using –a options and use uptime command and mentioned the server list (use /etc/ansible/hosts as an inventory), You can see all the server uptime on the screen in single command.

]$ ansible all -a uptime
ansible using ad-hoc commands

ansible using ad-hoc commands

We need to install basic rpm on all the servers, it’s very painful task to login all servers and install the rpm but single ad-hoc command can install the rpm on all servers in ansible. I am using ansible “YUM” module to install the rpm. For more ansible module refer the url ANSIBLE MODULE 

I have used –become option to run with root because rpm installation required root.

]$ ansible webservers -m yum -a "name=telnet state=present" --become
ansible using ad-hoc commands

ansible using ad-hoc commands

You can also use ansible “COPY” module to copy the files from 1 destination to another. It’s very simple task to use copy module.

]$ ansible webservers –m copy –a “src=/etc/hosts dest=/tmp/hosts”

Client : Verify the file copy or not on remote server.

If you want to change permission as well as ownership of the file then use ansible “FILE” module. I am using –become option because chown required root privileges.

]$ ansible webservers –m file –a “dest=/tmp/hosts1” chmod=777 owner=linux1” --become

Client : You can verify on remote server that permission and owner changed or not.

We need to reboot all of the hosts inside of that servers host group, we could initiate a reboot by calling the reboot binary directly from Ansible. You need to use –become because reboot required root privileges. :

]$ ansible development -a "/sbin/reboot" –become

If you are run the reboot using normal user it’s give the error.

 

Thanks for reading the post 🙂 . Please share and leave your comment if any.

Tagged . Bookmark the permalink.

7 Responses to Ansible using ad-hoc commands

  1. Pingback: cialis without a prescription

  2. Pingback: real viagra 100mg pills sale

  3. Pingback: ciproxine 500 mg antibiotique

  4. Pingback: chloroquin

  5. Pingback: croatia viagra 100mg

  6. Pingback: buy careprost from canada

  7. Pingback: cialis usa