Ansible Ad-hoc commands

Ansible Ad-hoc commands





Ad-hoc commands are commands which can be run individually to perform quick functions.

These ad-hoc commands are not used for configuration management and development because these commands are of one-time usage.

The ansible ad-hoc commands use /usr/bin/ansible command line tool to automate a single task.

                           


Ad hoc commands Syntax


Syntax:

#ansible <hosts> -a <"arguments"> -u <username> [--become]

Ex:

#ansible demo -a “ls”

Explanation

Hosts: It can be an entry in the inventory file. For specifying all hosts in the inventory, use all or "*".

Arguments: We should pass values that are required by the module. It can change according to the module used.

Username: It specifies the user account in which Ansible can execute commands.

Become: It's an optional parameter specified when we want to run operations that need sudo privilege. By default, it becomes false.


Ad-hoc commands

To check ls on demo group of hosts :

[ansible@ip]#ansible demo -a “ls”


To create files on demo group of hosts :

[ansible@ip]#ansible demo -a “touch file1”


To create files on all group of hosts :

[ansible@ip]#ansible all -a “touch file2”


To check list of hidden file with detailed demo group of hosts :

[ansible@ip]#ansible demo -a “ls -al”


To install httpd on demo group of hosts :

[ansible@ip]#ansible demo -a “sudo yum install httpd –y”


To install httpd on demo group with sudo permission of hosts :

[ansible@ip]#ansible demo -ba “yum install httpd –y”


To uninstall httpd on demo group with sudo permission of hosts :

[ansible@ip]#ansible demo -ba “yum remove httpd –y”






Share:

0 comments

Please leave your comments...... Thanks