Ansible CLI commands


In this tutorial we will discuss Ansible CLI commands to run ad-hoc commands on remote machine. Below is the basic syntax for ansible CLI command.

ansible <hosts-group> -m <module> -a <arguments>

We can use single host or all in place of <group> and <arguments> are optional to provide as per module which we are using.

Check connectivity of hosts
We will use this command to check connectivity to hosts

ansible  <group> -m ping

if you haven’t configure password less configuration between remote machine and control machine then we may need to pass few args to provide passwords in cli commands.

ansible <group> -m ping –k –k –u <username>
-k  argument will be used to ask password and another -k is for sudo password in runtime.
-u argument will be used to give username

If you want to run the ansible module on group of hosts from custom hosts file then you can follow below command.

ansible –i hosts <group_name> -m <module_name>  -a <arguments>

Checking host system’s information
Ansible collects the system’s information for all the hosts connected to it. To display the information of hosts, run
ansible <group> -m setup

To check a particular info from the collected information by passing an argument.
ansible <group> -m setup -a “filter=ansible_distribution”

Transferring files
For transferring files, we use a module ‘copy’ & complete command that is used is

ansible <group> -m copy -a “src=/home/ansible dest=/tmp/home”

Managing users
So to manage the users on the connected hosts, we use a module named ‘user’ & commands to use it are as follows,

Creating a new user
ansible <group> -m user -a “name=ansible password=<encrypted password>”

Deleting a user
ansible <group> -m user -a “name=ansible state=absent”

Changing permissions & ownership
So for changing ownership of files of connected hosts, we use module named ‘file’ & commands used are

Changing permission of a file
ansible <group> -m file -a “dest=/home/ansible/file1.txt mode=777”

Changing ownership of a file
ansible <group> -m file -a “dest=/home/ansible/file1.txt mode=777 owner=ansible group=ansible”

Managing Packages
So, we can manage the packages installed on all the hosts connected to ansible by using ‘yum’ & ‘apt’ modules & the complete commands used are
Check if package is installed & update it
ansible <group> -m yum -a “name=httpd state=latest”

Check if package is at a specific version
ansible <group> -m yum -a “name= httpd-1.8 state=present”

Check if package is not installed
ansible <group> -m yum -a “name= httpd state=absent”

Managing services
So to manage services with ansible, we use modules ‘service’ & complete commands that are used are,

Starting a service
ansible <group> -m service -a “name=httpd state=started”

Stopping a service
ansible <group> -m service -a “name=httpd state=stopped”

Restarting a service
ansible <group> -m service -a “name=httpd state=restarted”



<< Previous                                                                                                                                 Next >>


No comments:

Post a Comment