KEMBAR78
Basics of Ansible - Sahil Davawala | PPTX
 DevOps - a clipped compound of "development" and "operations"
 Automate the software integration, testing, deployment, and infrastructure
changes.
 Improve automation and measurement of system metrics.
 The best powerful automation tool which can help us in achieving everything is
ANSIBLE.
 Basics of Ansible
 Usage of Ansible
 Ansible without playbook i.e. through Adhoc commands
 Ansible with playbook
 Ansible modules
 Installation of a package on multiple instances followed by a Demo.
It is an IT automation tool which can configure systems, deploy
software, and orchestrate more advanced IT tasks such as
continuous deployments or zero downtime rolling updates.
 Ansible is an agent-less and uses a PUSH(SSH) approach.
 It is developed by RedHat.
 It is an OpenSource application.
 Platforms supported are Linux & Windows.
 Linux for control machine & Windows for managed nodes.
 Latest version of ansible is 2.3
Reference URL : https://github.com/ansible/ansible/releases
 Major companies using Ansible are Atlassian, CISCO, EA Sports, NASA, RedHat,
Twitter and many more.
Control Machine Requirements
 Currently Ansible can be run from any machine with Python 2.6 or 2.7 installed
Managed Node Requirements(Target Host)
 On the managed nodes, you need a way to communicate, which is normally SSH.
You also need Python 2.6 or later installed for the same.
Chef/Puppet Ansible
• Needs to be installed on Agents.
• Agents pull changes from a master.
• Communication channel used is their own
(usually not SSH)
• Complicated
setup/architecture/installation
• Complicated orchestration.
• Chef uses Ruby in backend and pure Ruby
DSL for configuration.
• Puppet using Ruby in backend and uses
Puppet DSL for configuration.
• Need not be installed on Agents.
• Pushes the changes to Agents whenever
required.
• Uses SSH
• Easy installation and architecture
• Simplified orchestration.
• Ansible backed is on Python.
• Uses YAML as configuration files.
 Installing Ansible on CentOS :
 To get Ansible for CentOS 7, first ensure that the CentOS 7 EPEL repository is
installed:
sudo yum install epel-release
sudo yum update
 Once the repository is installed, install Ansible with yum:
sudo yum install ansible
 Installing Ansible on Ubuntu :
sudo apt-get install software-properties-common
sudo apt-add-repository ppa:ansible/ansible
sudo apt-get update
sudo apt-get install ansible
 Run command : ansible –version
 Example output :
ansible 2.3.1.0
config file = /etc/ansible/ansible.cfg
configured module search path = Default w/o overrides
NOTE : If anyone wants to execute the playbook on a remote machine
then it is mandatory to have Python libraries installed on that remote machine.
 Certain settings in Ansible are adjustable via a configuration file. This
configuration file is know as ANSIBLE CONFIG
 Ansible allows configuration of settings via environment variables. If these
environment variables are set, they will override any setting loaded from the
configuration file.
 Here is the order in which configuration file will be processed.
 We use YAML because it is easier for humans to read and write than other common data formats like XML or JSON.
 All YAML files (regardless of their association with Ansible or not) can optionally begin with --- and end with ... This is part of the YAML
format and indicates the start and end of a document.
 All members of a list are lines beginning at the same indentation level starting with a "- " (a dash and a space):
Example :
---
# A list of tasty fruits
fruits:
- Apple
- Orange
- Strawberry
- Mango
…
 A dictionary is represented in a simple key: value form (the colon must be followed by a space):
Example :
# An employee record
martin:
name: Martin D'vloper
job: Developer
skill: Elite
 Ansible works against multiple systems in our infrastructure at a particular point
of time.
 It does this by selecting portions of systems listed in Ansible’s inventory which
defaults to being saved at the location : /etc/ansible/hosts.
 You can specify a different inventory file using the -i <path> option on the
command line
 Example : ansible-playbook -i playbook.yml
 It can be used in both commands & playbooks.
 Types of inventories includes basic list, shell script, python script, advanced script
like ec2,etc.
 To ping multiple servers at a time(which are existing in inventory file) :
 ansible –m ping all
 For creating a new user and manipulation of existing user accounts :
 ansible all -m user -a "name=foo password=<crypted password here>”
 Ensure a service is started on all webservers:
 ansible webservers -m service -a "name=httpd state=started"
 Ansible adhoc commands have the limitation in complex scenarios so to overcome
this and make the automation easy & robust, playbooks were introduced.
 Playbooks can be used to manage configurations and deployments to remote
machines.
 Here is the example how to use a playbook.
 ansible-playbook first.yml –e name=webservers1
 apt module
 yum module
 package module
 shell module
 command module
 user module
 hostname module
 The apt module manages the apt packages.
Example :
- name: Remove "foo" package.
apt:
name: foo
state: absent
- name: Install the package "foo"
apt:
name: foo
state: present
 Installs, upgrade, removes, and lists packages and groups with the yum package
manager.
Example :
- name: install the latest version of Apache
yum:
name: httpd
state: latest
 Installs, upgrade and removes packages using the underlying OS package
manager.
Example :
- name: install the latest version of ntpdate
package:
name: ntpdate
state: latest
 The shell module takes the command name followed by a list of space-delimited
arguments. It is almost exactly like the command module but runs the command
through a shell (/bin/sh) on the remote node.
Example :
- name: Executing a Command Using Shell Module
shell: ls -lrt > temp.txt
The above command lists all the files in the current folder and writes that to the file i.e.
temp.txt.
 The command module takes the command name followed by a list of space-
delimited arguments.
 The given command will be executed on all selected nodes. It will not be processed
through the shell, so variables like $HOME and operations
like "<", ">", "|", ";" and "&" will not work.
Example :
- name: Executing a command using command module.
command: cat hello.txt
The above command displays the content of the file hello.txt
 Manage user accounts and user attributes.
Example :
- user:
name: johnd
comment: "John Doe"
uid: 1040
group: admin
 Set system’s hostname, supports most OSs/Distributions, including those using
systemd.
 Note, this module does NOT modify /etc/hosts. You need to modify it yourself using
other modules like template or replace.
Example :
- hostname:
name: web01
 Ansible command using a direct agent.
 Ansible command using an Inventory group.
 Ansible playbook for direct host (agent)
 Ansible playbook split into multiple tasks.
 Ansible playbook installing a service on multiple hosts.
Basics of Ansible - Sahil Davawala

Basics of Ansible - Sahil Davawala

  • 2.
     DevOps -a clipped compound of "development" and "operations"  Automate the software integration, testing, deployment, and infrastructure changes.  Improve automation and measurement of system metrics.  The best powerful automation tool which can help us in achieving everything is ANSIBLE.
  • 4.
     Basics ofAnsible  Usage of Ansible  Ansible without playbook i.e. through Adhoc commands  Ansible with playbook  Ansible modules  Installation of a package on multiple instances followed by a Demo.
  • 5.
    It is anIT automation tool which can configure systems, deploy software, and orchestrate more advanced IT tasks such as continuous deployments or zero downtime rolling updates.
  • 6.
     Ansible isan agent-less and uses a PUSH(SSH) approach.  It is developed by RedHat.  It is an OpenSource application.  Platforms supported are Linux & Windows.  Linux for control machine & Windows for managed nodes.  Latest version of ansible is 2.3 Reference URL : https://github.com/ansible/ansible/releases  Major companies using Ansible are Atlassian, CISCO, EA Sports, NASA, RedHat, Twitter and many more.
  • 7.
    Control Machine Requirements Currently Ansible can be run from any machine with Python 2.6 or 2.7 installed Managed Node Requirements(Target Host)  On the managed nodes, you need a way to communicate, which is normally SSH. You also need Python 2.6 or later installed for the same.
  • 9.
    Chef/Puppet Ansible • Needsto be installed on Agents. • Agents pull changes from a master. • Communication channel used is their own (usually not SSH) • Complicated setup/architecture/installation • Complicated orchestration. • Chef uses Ruby in backend and pure Ruby DSL for configuration. • Puppet using Ruby in backend and uses Puppet DSL for configuration. • Need not be installed on Agents. • Pushes the changes to Agents whenever required. • Uses SSH • Easy installation and architecture • Simplified orchestration. • Ansible backed is on Python. • Uses YAML as configuration files.
  • 10.
     Installing Ansibleon CentOS :  To get Ansible for CentOS 7, first ensure that the CentOS 7 EPEL repository is installed: sudo yum install epel-release sudo yum update  Once the repository is installed, install Ansible with yum: sudo yum install ansible  Installing Ansible on Ubuntu : sudo apt-get install software-properties-common sudo apt-add-repository ppa:ansible/ansible sudo apt-get update sudo apt-get install ansible
  • 11.
     Run command: ansible –version  Example output : ansible 2.3.1.0 config file = /etc/ansible/ansible.cfg configured module search path = Default w/o overrides NOTE : If anyone wants to execute the playbook on a remote machine then it is mandatory to have Python libraries installed on that remote machine.
  • 12.
     Certain settingsin Ansible are adjustable via a configuration file. This configuration file is know as ANSIBLE CONFIG  Ansible allows configuration of settings via environment variables. If these environment variables are set, they will override any setting loaded from the configuration file.  Here is the order in which configuration file will be processed.
  • 13.
     We useYAML because it is easier for humans to read and write than other common data formats like XML or JSON.  All YAML files (regardless of their association with Ansible or not) can optionally begin with --- and end with ... This is part of the YAML format and indicates the start and end of a document.  All members of a list are lines beginning at the same indentation level starting with a "- " (a dash and a space): Example : --- # A list of tasty fruits fruits: - Apple - Orange - Strawberry - Mango …  A dictionary is represented in a simple key: value form (the colon must be followed by a space): Example : # An employee record martin: name: Martin D'vloper job: Developer skill: Elite
  • 15.
     Ansible worksagainst multiple systems in our infrastructure at a particular point of time.  It does this by selecting portions of systems listed in Ansible’s inventory which defaults to being saved at the location : /etc/ansible/hosts.  You can specify a different inventory file using the -i <path> option on the command line  Example : ansible-playbook -i playbook.yml  It can be used in both commands & playbooks.  Types of inventories includes basic list, shell script, python script, advanced script like ec2,etc.
  • 17.
     To pingmultiple servers at a time(which are existing in inventory file) :  ansible –m ping all  For creating a new user and manipulation of existing user accounts :  ansible all -m user -a "name=foo password=<crypted password here>”  Ensure a service is started on all webservers:  ansible webservers -m service -a "name=httpd state=started"
  • 18.
     Ansible adhoccommands have the limitation in complex scenarios so to overcome this and make the automation easy & robust, playbooks were introduced.  Playbooks can be used to manage configurations and deployments to remote machines.  Here is the example how to use a playbook.  ansible-playbook first.yml –e name=webservers1
  • 20.
     apt module yum module  package module  shell module  command module  user module  hostname module
  • 21.
     The aptmodule manages the apt packages. Example : - name: Remove "foo" package. apt: name: foo state: absent - name: Install the package "foo" apt: name: foo state: present
  • 22.
     Installs, upgrade,removes, and lists packages and groups with the yum package manager. Example : - name: install the latest version of Apache yum: name: httpd state: latest
  • 23.
     Installs, upgradeand removes packages using the underlying OS package manager. Example : - name: install the latest version of ntpdate package: name: ntpdate state: latest
  • 24.
     The shellmodule takes the command name followed by a list of space-delimited arguments. It is almost exactly like the command module but runs the command through a shell (/bin/sh) on the remote node. Example : - name: Executing a Command Using Shell Module shell: ls -lrt > temp.txt The above command lists all the files in the current folder and writes that to the file i.e. temp.txt.
  • 25.
     The commandmodule takes the command name followed by a list of space- delimited arguments.  The given command will be executed on all selected nodes. It will not be processed through the shell, so variables like $HOME and operations like "<", ">", "|", ";" and "&" will not work. Example : - name: Executing a command using command module. command: cat hello.txt The above command displays the content of the file hello.txt
  • 26.
     Manage useraccounts and user attributes. Example : - user: name: johnd comment: "John Doe" uid: 1040 group: admin
  • 27.
     Set system’shostname, supports most OSs/Distributions, including those using systemd.  Note, this module does NOT modify /etc/hosts. You need to modify it yourself using other modules like template or replace. Example : - hostname: name: web01
  • 29.
     Ansible commandusing a direct agent.  Ansible command using an Inventory group.  Ansible playbook for direct host (agent)  Ansible playbook split into multiple tasks.  Ansible playbook installing a service on multiple hosts.

Editor's Notes

  • #3 DevOps (a clipped compound of "development" and "operations") is a software development and delivery process that emphasizes communication and collaboration between product management, software development, and operations professionals.  It seeks to automate the process of software integration, testing, deployment, and infrastructure changes by establishing a culture and environment where building, testing, and releasing software can happen rapidly, frequently, and more reliably.
  • #7 And ofcourse Crest Data Systems is one of the major companies.
  • #8 For Windows : Need pywinrm on control machine(Linux) Need PowerShell on the target machine(Windows)
  • #9 Change Management - Provisioning - Automation - Orchestration - It basically means the automated arrangement, coordination, and management of complex computer systems, and services.
  • #10 https://www.ansible.com/blog/orchestration-you-keep-using-that-word
  • #19 Playbooks - Playbooks are Ansible’s configuration, deployment, and orchestration language. Playbooks are designed to be human-readable and are developed in a basic text language.
  • #21 And many more…. Ansible is famous because of N number of modules support they provide.
  • #24 RPM family(Linux,CentOS) : Httpd Debian family(Ubuntu) : Apache Reference URL(using package module on multiple OS) : https://serverfault.com/questions/587727/how-to-unify-package-installation-tasks-in-ansible
  • #30 Commands : ansible -m ping all ansible-playbook first.yml -e name=webservers1