Ansible adhoc commands



Adhoc commands are commands which we use only for quick purposes, which we don't want  to save for later. In situations like checking the status of a server or copying a file to the server we just want to do it quickly by typing only few commands. In such situations Ansible adhoc commands are really helpful. 
For example if you have an inventory file with 2 groups, one group called dbservers and one group called webservers like below,

Sample inventory configuration.

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

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

If you want to check the status of both the web servers, servera and serverb. You can do that simply by typing

ansible webservers -a "pwd"

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

How to write an Ansible adhoc command?

What you just read above is a simple example for Ansible adhoc 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 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 module available in Ansible. By default it is command. For example shell, copy, yum, apt, file.
  • arguments : We should pass values which are required by the module . It may change according to the module used.
  • username : It specifies the user account in which ansible can execute command. User account in which it do ssh.
  • become : It's an optional parameter specified when we want to execute operations which needs 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 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 such 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 file 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 adhoc commands.

-Thanks



Comments

Popular Posts