ANSIBLE 2.0.2.0 – Basic Cheat Sheet – CentOS 6

Ansible,com

Ansible.com

To install Ansible:

# wget http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpminstall epel-release

# yum install epel-release-6-8.noarch.rpm

# yum update

# yum install ansible -y

Add remote hosts:

$ vim /etc/ansible/hosts

192.168.0.1 (your remote server IP) or
hostname.hostname.local (your remote server hostname)

Ping remote hosts: (if using ssh keys for authentication –ask-pass is not required)

$ ansible all -m ping –ask-pass

Ping remote hosts with specific account: (if using ssh keys for authentication –ask-pass is not required)

$ ansible all -m ping –ask-pass -u root

Execute command on remote server shell:

$ ansible all -m shell -a “mkdir /home/ansible/testdirectory” –ask-pass -u root

Remove file from remote server:

$ ansible all -m file -a “dest=/tmp/test.sh state=absent” –ask-pass -u root

Install apache on remote server:

$ ansible all -m yum -a “name=httpd state=latest” -u root

Change file permissions:

$ ansible all -m file -a “dest=/home/ansible/testfile.txt mode=600 owner=ansible group=anisble”

Confirm if package installed but do not update

$ ansible all -m yum -a “name=telnet state=present”

Confirm package is latest version

$ ansible all -m yum -a “name=telnet state=latest”

Confirm package is not installed

$ ansible all -m yum -a “name=telnet state=absent”

Start service

$ ansible all -m service -a “name=httpd state=started”

Restart service

$ ansible all -m service -a “name=httpd state=restarted”

Stop serivce

$ ansible all -m service -a “name=httpd state=stopped”

 

Gathering Facts

$ ansible all -m setup

Distribute Linux flavors in Ansible host file:

$ vim /etc/ansible/hosts

[RHEL]
192.168.1.1
rhel.redhat.test
[SUSE]
192.168.1.2
suse.opensuse.test

Only send instructions on RHEL hosts:

$ ansible RHEL  -m shell -a “mkdir /home/ansible/testfolder” 

Ansible use YaML for Automation and known as Playbook in Ansible

create a playbook i.e an .yaml file eg: telnetPlaybook.yaml


– hosts: SUSE
remote_user: root
tasks:
– zypper: name=telnet state=latest
– hosts: RHEL
remote_user: root
tasks:
– yum: name=telnet state=latest

Run playbook with Ansible: (-f switch allow the execution to be performed on multiple hosts parallel)

$ ansible-playbook telnetPlaybook.yaml -f 10

Ansible Modules (-m): These are few but there is huge list.

command – execute commands and this is default module of Ansible.
copy – copy files
shell – use remote shell to execute
file – file operations
ping – ping remote hosts
yum – Redhat package manager
git – use GIT
user – user creation, manipulation
service – manage services

If you find it interesting, spread the word 🙂