Ansible is an automation tool created for the sole
purpose of handling redundant that would otherwise slow down your development
cycle. The automation tool is used in IT services such as application
deployment, management, provisioning, and inter-service orchestration. In times
of quickly scaling IT industries and their ever-increasing complexity of work,
Ansible is like Moses created to shepherd the hapless sheep of through the
valley of IT development.
Please refer this article to understand in detail about
Ansible.
In this article, we will be learning some of the most
useful Ansible Commands.
Purpose
|
Commands
|
To Set Up SSH Command
|
sudo apt-get install
openssh-server
|
To Generate SSH Key
|
ssh-keygen
|
To Copy the SSH Key on the Hosts
|
ssh-copy-id hostname
|
To Check the SSH Connection
|
ssh <nodeName>
|
To Add Ansible repository
|
sudo apt-add-repository
ppa:ansible/ansible
|
To Run the update command
|
sudo apt-get update
|
To Install Ansible package
|
sudo apt-get install ansible
|
To Check Ansible Version
|
ansible –version
|
To set up SSH agent
|
$ ssh-agent bash
$ ssh-add ~/.ssh/id_rsa
|
To use SSH with a password
instead of keys
|
ansible europe -a
"/sbin/reboot" -f 20
|
To run /usr/bin/ansible from a
user account
|
ansible europe -a
"/usr/bin/foo" -u username
|
To run commands through privilege
escalation and not through user account
|
ansible europe -a
"/usr/bin/foo" -u username --become [--ask-become-pass]
|
To become a user, other than root
by using --become-user
|
$ ansible europe -a
"/usr/bin/foo" -u username --become --become-user otheruser
[--ask-become-pass]
|
To Transfer a file directly to many servers
|
ansible europe -m copy -a
"src=/etc/hosts dest=/tmp/hosts"
|
To change the ownership and
permissions on files
|
ansible webservers -m file -a
"dest=/srv/foo/a.txt mode=600"
ansible webservers -m file -a
"dest=/srv/foo/b.txt mode=600 owner=example group=example"
|
To create directories
|
ansible webservers -m file -a
"dest=/path/to/c mode=755 owner=example group=example state=directory"
|
To delete directories
(recursively) and delete files
|
ansible webservers -m file -a
"dest=/path/to/c state=absent"
|
To ensure that a package is
installed, but doesn’t get updated
|
ansible webservers -m apt -a
"name=acme state=present"
|
To ensure that a package is
installed to a specific version
|
ansible webservers -m apt -a
"name=acme-1.5 state=present"
|
To ensure that a package is not
installed
|
ansible webservers -m apt -a
"name=acme state=absent"
|
To ensure a service is started on
all web servers
|
ansible webservers -m service -a
"name=httpd state=started"
|
To restart a service on all web
servers
|
ansible webservers -m service -a
"name=httpd state=restarted"
|
To ensure a service is stopped
|
ansible webservers -m service -a
"name=httpd state=stopped
|