You are on page 1of 5

Ansible documentation

=========================

Insatallion of ansible in ubuntu


=================================
1.apt-get update -y
2.apt-get install software-properties-common
3.apt-add-repository ppa:ansible/ansible
4. apt-get update
5.apt-get install ansible
6.ansible --version
7.python --version
note:python is mandatory in all the clients snd server because ansible is developed
in python language
8.now move to /etc/ansible/.This is the home directory of ansible.In this directory
we see the following files
i)ansible.cnf ___ This is the ansible configuration file.
ii)hosts ___ This is the host inventory file.In ths we can configure our
clients
iii)roles
9.Now open the hosts inventory file
cd /etc/ansible
ls
vi hosts
[dev] //group name//
ansible_server ansible_connection=local //this is to configure the server as
client//
[prod]
ansible_amazon ansible_host=ip of the clients //this is the configuration of
clients//
save and exit (:wq!)
10.Now generate the ssh key in ansible server.Through ssh connection establish
between the server and the clients
11.ssh-keygen is the command to generate ssh key in server
12.Now we can see id_rsa and id_rsa.pub keys in /root/.ssh

#cd .ssh/
#ssh-keygen
id_rsa id_rsa.pub

In clients
============
python need to install in nodes
13.vi /etc/ssh/sshd_config open this file and do the following changes
Permitrootlogin yes
passwordauthentication yes
:wq!
14.service sshd restart
15.We need to crete a password for the clients
16.passwd root
type ur passwd
17.service sshd restart

In server
=========
16.execute the foolowing command
ssh-copy-i root@ip of clients or ssh-copy-id (client id)
#ssh (clientid) ---control enters into the client and enter exit to come out of
node
To test the connection
Ansible all -m ping (response comes from node as pong)

17.now connection established between the server and the clients

===================================================================================
=====================
18.Now we need to write the playbooks and apply to the nodes
19.mkdir playbooks
20.cd playbooks
21.vi file.yml
---
- hosts: dev test
tasks:
- name: create directory on tmp dir for clients
file: path=/tmp/mouni state=directory
- name: create file on mouni dir for clients
file: path=/tmp/mouni/hanu state=touch
:wq!

#ansible-playbook file.yml ---- to run playbook file.yml

22.ansible -m ping all


23.Now the mouni directory is created in /tmp directory and in mouni directory hanu
file is ceated in dev and test clients
24.In this way we can apply the playbooks

Ansible ad-hoc commands (by using this commands we cannot write a playbook, by
using single command we create a file,directory etc..)
===========================================================
25.ansible all -m file -a "dest=/tmp/a.txt state=touch mode=600" //cmd to create a
file using ad-hoc cmd//
26.ansible all -m file -a "dest=/tmp/mouni/suni state=directory" //cmd to create a
directory//
27.ansible dev -m yum -a "pkg=git state=installed" //instation of package in dev
group//
ansible_server | SUCCESS => {
"cache_update_time": 1514874149, //We will see this msg//
"cache_updated": false,
"changed": false
}
28.ansible test -m copy -a "src=/root/devops.txt dest=/root" //copy the file from
test group//
29.vi ansible.conf
30.In this we need to uncomment the log-path = /var/log/ansible.log //this the
ansible log file path//
31.when we execute this we can see the logs on this file
Ansible when command
=====================
32.cd playbooks
33.vi when.yml
---
- hosts: all
tasks:
- name: create file for redhat family
file: path=/tmp/chef1 state=touch mode=0777
when: ansible_os_family == "RedHat"
ignore_errors: true
- name: create directory for debian family
file: path=/tmp/jenkins1 state=directory
when: ansible_os_family == "Debian"
ignore_errors: true
:wq!
34.In redhat client go to /tmp/ directory we can see chef1 directory and in debian
family we can see jankins1 directory
Ansible loops and conditions
=============================
35.using this loop we are creating the multiple users

---
- hosts: all
tasks:
- name: add several users
user:
name: "{{ item.name }}"
state: present
groups: "{{ item.groups }}"
with_items:
- { name: 'mounika', groups: 'root' }
- { name: 'mounika1', groups: 'root' }
:wq!
36.Now we the two user are created.Go to vi /etc/passwd
here we can see the users which we are created
Ansible vault
=============
By using this we can secure our playbook in the form of encryption
36.root@ansible-server:/etc/ansible/playbooks# ansible-vault create test.yml
New Vault password:
Confirm New Vault password:

vi test.yml
37.we will get this encrpted file
$ANSIBLE_VAULT;1.1;AES256
34346331323232653032666463373335623665663133613764653763336163396265343437326236
3933343133323363613337353333663764663537336235310a326239643732633935366265646332
61623632653731373036353239643435373838373338306339343964663166343033306632623063
3063636538343731650a366263373531306430643064633039336663633435346436313233343238
63636633663133336230636638383166646637663661353761663761316266663736313765393661
31623162393339326433643230633163306133383465613664313236353065666361333732633861
61306466396334313534316634376663393634646264383765616632646530633762353432313436
30366162316335666463616265366230343664323339303062396464643236383363633435376633
30323565383934663762313337383966346133356161306261363734633431366235633062373334
6234303832623833353831646465396137353466663831396466

38.root@ansible-server:/etc/ansible/playbooks# ansible-vault view test.yml


//using this command we can see the encrypted file//
Vault password: //it will promt u for the password//

---
- hosts:
tasks:
- name: fetching devops.txt from clients
fetch: src=/root/devops.txt dest=/etc/ansible

39.Encrypte the existing playbook we use this command


# ansible-vault encrypt when.yml
New Vault password:
Confirm New Vault password:
Encryption successful
40.To edit or modifie our encrypted playbook we this command
# ansible-vault edit test.yml
Vault password: //it will promt for passwd//
41.Decrypt existing encrypted playbook we this command
# ansible-vault decrypt test.yml
Vault password:
Decryption successful
# cat test.yml //using this command we can see ur file//

---
- hosts:
tasks:
- name: fetching devops.txt from clients
fetch: src=/root/devops.txt dest=/etc/ansible

Ansible role
=============
Roles are ways of automatically loading certain var files,tasks,and handlers based
on a known file structure.Grouping content by roles also allows easy sharing of
roles with other users.
Roles are just automation around 'include' directives as describe
above

42.Using this commands we can create roles in ansible home directory


#cd /etc/ansible
#ls
#cd roles
#ls
#ansible-galaxy init web
#ls
#cd web
#ls
#cd files
#vi index.html
<html><body><h1>welcome to ansible world</h1></body></html>
:wq!
#cd tasks
#ls
#vi main.yml
---
- name: installing apache2 for ubuntu
apt: pkg=apache2 state=installed
- name: remove existing index.html file
file: path=/var/www/index.html state=absent
- name: deploying webapp into html dir for clients
copy: src=index.html dest=/var/www/html
- name: starting the service
service: name=apache2 state=started
:wq!
#cd web
#vi web.yml
---
- hosts: dev
roles:
- wed
:wq!
#ansible-playbook web.yml
43.now index.html file display in the browser
note:open http and https in aws inbound rules.Take the ip adress of ubuntu client
and index.html file should display

You might also like