http://linuxtechlab.com/ansible-tutorial-simple-commands
In our earlier Ansible tutorial, we discussed the installation & configuration of Ansible.
Now in this ansible tutorial, we will learn some basic examples of
ansible commands that we will use to manage our infrastructure. So let
us start by looking at the syntax of a complete ansible command,
$ ansible -m -a
Here, we can also use a single host or all in place of &
are optional to provide. Now let’s look at some basic
commands to use with ansible,
$ ansible -m ping
-a “/sbin/reboot”
$ ansible -m setup | less
Secondly, to check a particular info from the collected information by passing an argument,
$ ansible -m setup -a “filter=ansible_distribution”
$ ansible -m copy -a “src=/home/dan dest=/tmp/home”
-m user -a “name=testuser password=”
-m user -a “name=testuser state=absent”
Note:- To create an encrypted password, use the ‘mkpasswd –method=sha-512’ command.
-m file -a “dest=/home/dan/file1.txt mode=777”
-m file -a “dest=/home/dan/file1.txt mode=777 owner=dan group=dan”
-m yum -a “name=ntp state=latest”
-m yum -a “name=ntp state=present”
-m yum -a “name= ntp-1.8 state=present”
-m yum -a “name=ntp state=absent”
-m service -a “name=httpd state=started”
-m service -a “name=httpd state=stopped”
-m service -a “name=httpd state=restarted”
So this completes our tutorial of some simple, one line commands that can be used with ansible. Also, for our future tutorials, we will learn to create plays & playbooks that help us manage our hosts more easliy & efficiently.
Here, we can also use a single host or all in place of
Check connectivity of hosts
We have used this command in our previous tutorial also. The command to check connectivity of hosts is$ ansible
Rebooting hosts
$ ansible
R
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
Secondly, to check a particular info from the collected information by passing an argument,
$ ansible
Transfering files
For transferring files we use a module ‘copy’ & complete command that is used is$ ansible
Manging 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
$ ansibleDeleting a user
$ ansibleNote:- 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 module named ‘file’ & commands used areChanging permission of a file
$ ansibleChanging ownership of a file
$ ansibleManaging Packages
So, we can manage the packages installed on all the hosts connected to ansible by using ‘yum’ & ‘apt’ modules & the complete commands used areCheck if package is installed & update it
$ ansibleCheck if package is installed & don’t update it
$ ansibleCheck if package is at a specific version
$ ansibleCheck if package is not installed
$ ansibleManaging services
So to manage services with ansible, we use a modules ‘service’ & complete commands that are used are,Starting a service
$ ansibleStopping a service
$ ansibleRestarting a service
$ ansibleSo this completes our tutorial of some simple, one line commands that can be used with ansible. Also, for our future tutorials, we will learn to create plays & playbooks that help us manage our hosts more easliy & efficiently.
No comments:
Post a Comment