How to execute ansible Ad-Hoc tasks in linux system with practical example



Execute Ansible Ad-Hoc Commands |The simplest way to kniw about ansible basic by executing Ad- Hoc tasks. Ad-Hoc tasks are very needful when we are going to do common  tasks like changing permission of file, check the connectivity and so on.

You can not create file using Ad-Hoc command. This is one time job.
To start Ad-Hoc command at first we need to install latest version of Ansible.To install latest version of ansible please go though the installation procedure of Ansible.

Before start to execute ansible command we need to gathering the details of remote hosts and which command to be execute.


Create inventory file in ansible

All details of all remote machines  are stored in the inventory file. By default the inventory  file is located at /etc/ansible/hosts. We can put the hostname of systems  and ip address of systems on which the command will be executed.
In this tutorial you have used  one host named host01.

put its hostname in an inventory file call our inventory myhosts, with
one group called group1.
echo “[group1]” > myhosts
Then add the host to the group:
echo “host01 ansible_ssh_user=root” >> myhosts


How to execute Ad-Hoc command 


We are going to execute date command .We have to convert the command into ansible tasks.
To run an ad-hoc task we need to execute the below command. 

For example, to check the current date and time of the remote host:

ansible group1 -i myhosts -m command -a date

·         When we run above command ansible executes the command and give the output.


To do this activity we need to establish passwordless connectivity between ansible control machine and remote machines. For more details on how to setup passwordless login you can refer

https://techrideradmin.blogspot.com/2018/09/ssh-password-less-login.html

Output:

ansible group1 -i myhosts -m command -a date

Ad-Hoc command
Ad-Hoc command

If this article is helpful to know about Execute Ansible Ad-Hoc Tasks please share this article.

SHARE

Admin

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

1 comments: