Ansible Tutorial: Introduction to simple Ansible commands
In our earlier Ansible tutorial, we discussed the installation & configuration of Ansible. Now in this ansible tutorial, we will learn some simple ansible commands that we will use to manage our infrastructure. Consider this as an Ansible commands cheatsheet.
So let us start by looking at the syntax of a simple ansible commands,
$ ansible <group> -m <module> -a <arguments>
Here, we can also use a single host or all in place of <group> & <arguments> are optional to provide. Now let’s look at some basic commands to use with ansible,
Ansible Commands CheatSheet
Check the connectivity of hosts
We have used this command in our previous tutorial also. The command to check the connectivity of hosts is
$ ansible <group> -m ping
Rebooting hosts
$ ansible <group> -a “/sbin/reboot”
Checking host’s system 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 | less
Secondly, 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/dan dest=/tmp/home”
Managing users
So to manage the users on the connected hosts, we use a module named ‘user’ & comamnds to use it are as follows,
Creating a new user
$ ansible <group> -m user -a “name=testuser password=<encrypted password>”
Deleting a user
$ ansible <group> -m user -a “name=testuser state=absent”
Note:- To create an encrypted password, use the ‘mkpasswd –method=sha-512’ command.
Changing permissions & ownership
So for changing ownership of files of connected hosts, we use a module named ‘file’ & commands used are
Changing permission of a file
$ ansible <group> -m file -a “dest=/home/dan/file1.txt mode=777”
Changing the ownership of a file
$ ansible <group> -m file -a “dest=/home/dan/file1.txt mode=777 owner=dan group=dan”
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,
Make sure the package is installed & update it
$ ansible <group> -m yum -a “name=ntp state=latest”
Make sure the package is installed & don’t update it
$ ansible <group> -m yum -a “name=ntp state=present”
Make sure the package is at a specific version
$ ansible <group> -m yum -a “name= ntp-1.8 state=present”
Make sure the package is not installed
$ ansible <group> -m yum -a “name=ntp state=absent”
Managing services
So to manage services with ansible, we use a 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”
These were our some important commands for Ansible Commands Cheatsheet. Pleas feel free to send in any questions or queries using the comment box below.
We are giving you exclusive deals to try Linux Servers for free with 100$ credit, check these links to claim your 100$,
DigitalOcean — 100$ free credit & Linode — 100$ free credit
Check some Exclusive Deals, HERE.
Also, check out DevOps Book You should read section.