Ansible -1
Ansible -1
Ansible -1
What is Ansible?
Ansible is an open source IT Configuration Management, Deployment &
Orchestration tool. It aims to provide large productivity gains to a wide variety of
automation challenges. This tool is very simple to use yet powerful enough to automate
complex multi-tier IT application environments
Puppet – It has multi-master architecture, if the active master goes down, the
other master takes the active master place.
Ansible – It runs with a single active node, called the Primary instance. If
primary goes down, there is a Secondary instance to take its place.
Salt stack – It can have multiple masters configured. If one master is down,
agents connect with the other master in the list. Therefore it has multiple
masters to configure salt minions.
Ease of Setup
Ansible – It has only master running on the server machine, but no agents
running on the client machine.
Salt stack – Here Server is called as salt master and clients are called as
salt minions which run as agents in the client machine.
Configuration Language
Chef – Chef uses Ruby Domain Specific Language (Ruby DSL). It has a steep
Learning Curve and its developer oriented.
Puppet
Puppet uses its own puppet Domain Specific Language (Puppet DSL). It is not
very easy to learn and its system administrator oriented .
Ansible
Ansible uses YAML i.e yet another mark-up Language (Python). It is quite easy
to learn and its administrator oriented. Python is inbuilt into most Unix and
Linux deployments nowadays, so setting the tool up and running is quicker.
Salt stack
Salt stack also uses YAML (Python). It is again easy to learn and administrator
oriented.
Machine should be
Chef – Chef Server works only on Linux/Unix but Chef Client and Workstation
can be on windows as well.
Puppet – Puppet Master works only on Linux/Unix but Puppet Agent also works
on windows.
Ansible – Ansible supports windows machines as well but the Ansible server has
to be on Linux/Unix machine.
Salt stack – Salt Master works only on Linux/Unix but Salt minions can work on
windows as well.
Chef - push
Puppet-pull
Ansible-push
Salt stack-push & pull
Ansible topics
Ad hoc command
Play books
Roles
Vault
Ad hoc command is used to install a single task
Play books is used to install and doing multiple task using YAML lag
Roles is dividing a complex playbooks into a structural format
Vault is used to hide the your secret files
Step 4: In putty ssh session enable allow agent forwarding option- Otherwise
while connecting to node instance you will get permission denied error
Logon to ec2-user
Step 5: Ansible adhoc command: Practice the command below with ec2-user
and not with root user
Step 6: Create one text file for e.g. slaves.txt and add node instance private IP
Example:
[Web]
IP 1
IP 2
Step 7: Now you need ansible.cfg file and u can get it from
Using this Url
https://raw.githubusercontent.com/ansible/ansible/devel/examples/ansible.cfg
Vi ansible.cfg
Step 8:
ansible all -i slaves.txt -m ping
ansible web -i slaves.txt -m ping
Step 9:
Ansible web –i slaves.txt –m yum –a “name=httpd state=present” –b
Step 10:
Ansible web –i slaves.txt –m service –a “name=httpd state=started” –b
Step 11:
Transferring the file to many servers/machines
ansible all -i slaves.txt -m copy -a "src=./slaves.txt dest=/tmp/slaves.txt"
Setp 12:
Deleting whole directory and files
Ansible abc -m file -a "dest = /path/user1/new state = absent"
Step 14:
User: To add and delete users
ansible webservers -m user -a 'name=nishanth password=admin123' -b
ansible webservers -m user -a 'name=nishanth state=absent' (to delete
user) -b
Task: use Ubuntu machine and without using -i
Step 15:
Deploy your webapp straight from git:
ansible webservers -m git -a "repo=https://foo.example.org/repo.git
dest=/srv/myapp version=HEAD"
ansible webservers -m service -a "name=httpd state=restarted"
ansible all -i slaves.txt --list-host
ansible <specific ip> -i slaves.txt -a "uname -a"
ansible all -i slaves.txt -a "uname -a" -u ec2-user
ansible all -i slaves.txt -a "uname -a" -u ec2-user –b
ansible jenkins -i slaves.txt -a "grep -i JENKINS_PORT
/etc/sysconfig/jenkins" -b
Dynamic Inventory:
Step 1:
You can find sample python script for dynamic inventory in this URL
https://raw.githubusercontent.com/ansible/ansible/devel/contrib/inventory/ec2.py
chmod 777 ec2-py
Check ls –lrt
Step 2:
Using IAM full admin access
Install boto in control machine to work with dynamic inventory with the
below command.
Pip install boto
Step 3:
Create IAM role in AWS with full administrative access and attach role to
your control machine
Step 4:
Dynamic Inventory example
ansible all -i ec2.py -a "uname -a"
Playbooks
Step 1:
Install visual studio code
https://code.visualstudio.com/docs/?dv=win64user
Step 2:
Install ansible plugins in visual studio code which will make our job easy
Step 3:
playbook file should be with .yaml format
vi filename.yaml
To run
ansible-playbook –I slaves.txt filename.yaml
Task 1:
- hosts: all
remote_user: ec2-user
become: yes
tasks:
- name: install httpd server
package:
name: httpd
state: present
- template:
src: /home/ec2-user/index.html.j2
dest: /var/www/html/index.html
To run this file
Task 2:
Create the (vi file name.yaml)
- hosts: all
remote_user: ec2-user
become: yes
tasks:
- name: im going to install apache
yum:
name: "{{ item }}"
state: present
with_items:
- mysql
- php
- unzip
Run this (ansible-playbook -i slaves.txt filename.yaml)
Task 3:
- hosts: web
remote_user: ec2-user
become: yes
tasks:
- name: Add jenkins repo install
yum_repository:
name: jenkins
description: jenkins YUM repo
baseurl: https://pkg.jenkins.io/redhat-stable
gpgkey: https://pkg.jenkins.io/redhat-stable/jenkins.io.key
- name: jenkins java install
yum:
name: "{{ item }}"
state: present
loop:
- java
- jenkins
- name: jenkins start
service:
name: jenkins
state: started
- hosts: web
remote_user: ec2-user
become: yes
vars:
port:8000
tasks:
- name: Add jenkins repo install
yum_repository:
name: jenkins
description: jenkins YUM repo
baseurl: https://pkg.jenkins.io/redhat-stable
gpgkey: https://pkg.jenkins.io/redhat-stable/jenkins.io.key
- name: jenkins java install
yum:
name: "{{ item }}"
state: present
loop:
- java
- jenkins
- name: going to change port
lineinfile:
path: /etc/sysconfig/jenkins
regexp: '^jeankins-port='
line: "jenkins-port-{{port}}"
notify:
-restart jenkins
- name: jenkins start
service:
name: jenkins
state: started
handlers:
- name: restart jenkins
service:
name: jenkins
state:restarted
Task 4:
- hosts: jenkins
remote_user: ec2-user
become: yes
vars:
jenkins_port: 9006
tasks:
- name: jenkins installation from yum
yum_repository:
name: jenkins
description: jenkins
baseurl: http://pkg.jenkins.io/redhat
gpgkey: https://jenkins-ci.org/redhat/jenkins-ci.org.key
Roles:
Step 1:
Sudo yum install tree –y
Cd task/
Using vi main,yml
Check it current dictory
pwd
Using cd
come back path
vi role.yml