ANSIBLE INTRODUCTION
Marcin Klimus, Cisco CMS Engineer
[email protected] What is Ansible?
Why use Ansible?
Comparison with others configuration management tools
New features in Ansible 2.0
Set up test environment
Target machines requirements
Installing Ansible
Ansible configuration hierarchy
Ansible hosts inventory file
Ansible first run
How it works?
Ad-hoc commands
INDEX
1. Provisioning – act of getting a server or fresh instance to work (on
AWS or OpenStack cloud)
2. Configuration Management – install the software, manage users on
the machine, check settings
3. Deployment – setup an applications or custom software on server
or group of servers
4. Orchestration – managing the large number of servers, their
dependencies, HA of applications, replication between data
centers,
5. Part of Continuous Delivery/Deployment – as additional tool for
automation of the cycle of delivery (with Jenkins used for building
applications)
WHAT IS ANSIBLE?
Super-simple to use (requires only Python and SSH on your target
machines) comparing to Puppet or Chef
Sensible Design – saving a lot of pain avoiding mistakes
Very efficient vs manual changes and shell scripting
Extremely fast growth, a lot of new features
More than just configuration management tool
WHY USE ANSIBLE?
Task blocks (like try/except/finally in Python)
Dynamic Includes (loops allowed, run AFTER you inventory data is
fetched)
Execution Strategies (‘free’ allows hosts to rock an roll without
waiting for others) (previous default is now called ‘linear’; i.e. one
host after the other)
New Modules: expect, find, iptables and package modules
NEW FEATURES IN ANSIBLE 2.0
Import provided virtual machine (minimal Centos 7.4)
Change hostname for each server (hostnamectl set-
hostname <dest_hostname>)
Add machines to /etc/hosts file
Check connectivity for i in {1..4}; do ping -c 1
slave$i; done
SET UP TEST ENVIRONMENT
Generate the ssh keys on master (ssh-keygen)
Share keys across VMs: for i in {1..4}; do ssh-copy-id
slave$i; done
Check password less connectivity: for i in {1..4}; do ssh
slave$i pwd; done
SET UP TEST ENVIRONMENT C.D.
Python 2.7
OpenSSH server running
Connect via SSH and accept the host key –accept-hostkey
TARGET MACHINE REQUIREMENTS
From the official repository using package manager (yum
install Ansible)
Latest releases by PIP
http://docs.ansible.com/ansible/latest/installation_guide/intro_ins
tallation.html#basics-what-will-be-installed
INSTALLING ANSIBLE
Downloading PIP:curl https://bootstrap.pypa.io/get-pip.py -o
get-pip.py
Installing PIP: python get-pip.py
Upgrade virtualenv: yum remove python-virtualenv-1.10.1-
4.el7.noarch ; pip install virtualenv
Create virtualenv: virtualenv vir_ansible/
Swtich to virtualenv: . /root/vir_ansible/bin/activate
Installing: pip install ansible
INSTALLING ANSIBLE FROM PIP
Main Ansible configuration file: ansible.cfg
Search path, in proper Unix style:
1. Ansible_Config (environment variable)
2. ansible.cfg (current directory)
3. .ansible.cfg (in users’s home)
4. /etc/ansible/ansible.cfg (default settings + docs)
Ansible glossary for all options:
http://docs.ansible.com/ansible/latest/reference_appendices/gl
ossary.html
ANSIBLE CONFIGURATION HIERARCHY
Simple grouping of hosts
INI file syntax
Create a simple inventory file in Ansible projects directory
[allservers]
slave1
slave2
slave3
192.168.30.7
Overwrite ansible.cfg: ansible-playbook –i <path_to_inventory>
ANSIBLE HOSTS INVENTORY FILE
Copy basics files to our location: cp /etc/ansible/hosts
/etc/ansible/ansible.cfg /root/ansible_training
Edit line in ansible.cfg:
inventory=/root/ansible_training/hosts
Edit /root/ansible_training/hosts (add two groups)
Check connectivity from ansible: ansible -m ping appservers
Start with simple command: ansible appservers -m command -a
"yum update"
ANSIBLE FIRST RUN
Deploy next server (or remove ~/.ssh/known_hosts)
Start with simple command: ansible appservers -m command -a
"yum update"
Change host check option:
vim ansible.cfg
[defaults]
host_key_checking = False
ANSIBLE FIRST RUN C.D.
HOW IT WORKS?
Running one-off tasks with Ansible
Automating fragile bash scripts or ‘for’ loops in the shell
A use case often not covered (or bad hacked) with other
automation/config managmenet frameworks
A few examples of simple tasks that don’t fit well into the
‘playbook’ format
AD-HOC COMMANDS
Syntax: ansible <group/machine> -m <module> -a <args> (-k for
ask-pass)
ansible 192.168.30.4 –m ping
ansible allservers –a “free –m”
ansible allservers –m command –a “free –m”
ansible dbservers -m package -a "name=openssh-server
state=installed“
ansible allservers -m package -a "name=ksh state=installed“
ansible allservers -m package -a "name=ksh state=installed" -
m raw
ansible allservers -m package -a "name=ksh state=absent"
ansible allservers –a “yum update”
ansible allservers -m service -a "name=sshd state=restarted"
AD-HOC COMMANDS EXAMPLES