Codementor Events

Ansible Ad-hoc Commands

Published Nov 13, 2017
Ansible Ad-hoc Commands

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 — tasks we just want to do quickly by typing only few commands.

In these situations, Ansible ad-hoc commands are really helpful.

For example, if you have an inventory file with two groups, one group called dbservers and the other group called webservers, like below, if you want to check the status of both the web servers, Server A and Server B, you can do that simply by typing:

ansible webservers -a "pwd"

Sample inventory configuration:

[dbservers]
db1.mercury.com
db2.venus.com

[webservers]
servera.earth.com
serverb.jupyter.com

It will try to execute the pwd statement in the bash of both servers and return the result. If both servers are live, it will display some success result.

How to write an Ansible ad-hoc command?

What you just read above is a simple example of an Ansible ad-hoc command. Now we will move forward and understand its anatomy.

Syntax:-
ansible <hosts> [-m <module_name>] -a <"arguments"> -u <username> [--become]

Hosts: It can be any entry in the inventory file. For specifying all hosts in inventory, use all or '*'. Wild card patterns are also accepted.

module_name: It's an optional parameter. There are hundreds of modules available in Ansible. By default it is command. For example shell, copy, yum, apt, file.

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

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

Become: It's an optional parameter specified when we want to execute operations that need sudo privilege. By default become is false.

If you put a -c option, then Ansible will do a dry run of the command. It will not actually be applied on the nodes.

Frequently used modules

command: Used to execute command in bash and see the result
ansible webservers -m command -a "pwd"
shell: operations like piping and redirects will not work in command. In these situations, you have to use shell module.
ansible webservers -m shell -a 'echo $LD_PATH'
file: Used to create files, directories, set, or change file permissions and ownership etc.
ansible webservers -m file -a 'dest=/home/ubuntu/new.txt mode=600 owner=vishnu group=vishnu'
ansible webservers -m file -a 'dest=/home/ubuntu/new mode=755 state=directory owner=vishnu'
copy: Used for copying files to multiple nodes concurrently
ansible webservers -m copy -a 'src=/home/user/cfg.ini dest=/home/ubuntu /cfg.ini'
yum/apt: For installation and management of applications
ansible webservers -m apt -a 'name=python state=present'
user: To add and delete users
ansible webservers -m user -a 'name=user1 password=somepassword'
ansible webservers -m user -a 'name=user1 state=absent' (to delete user)
service: To manage services like httpd, mysqld etc.
ansible dbservers -m service -a 'name=mysqld state=started'
setup: It's used for gathering facts about the hosts
ansible all -m setup

Hope now you are all set to start using Ansible ad-hoc commands.

-Thanks

Discover and read more posts from Vishnu Ks
get started
post commentsBe the first to share your opinion
Show more replies