Ansible
Ansible
Ansible
Eric Beaudoin
Technical Account Manager
THIS IS A FREE INTRODUCTION TRAINING
PROVIDED BY RED RED HAT
1 Introduction to Ansible
4 Ansible variables
+ LAB
2 Ansible commands
+ LAB 5 Ansible roles
+ LAB
3 Ansible playbooks
+ LAB 6 Ansible Tower
5
Intro to Ansible
Michael DeHaan (creator cobbler and func) “ Ansible owes much of it's origins to
time I spent at Red Hat’s Emerging
Technologies group, which was an
https://www.ansible.com/blog/2013/12/08/the-origins-of-ansible
R&D unit under Red Hat's CTO ”
- Michael DeHaan
7 CONFIDENTIAL
BENEFITS
Why is Ansible popular?
★ Playbook (Plan)
★ Plays
★ Tasks
★ Modules (Tools)
★ Inventory
# CENTOS
# ENABLE EPEL REPO
yum install epel-release
# RHEL
# ENABLE EXTRAS REPO
subscription-manager repos --enable rhel-7-server-extras-rpms
Ou
subscription-manager repos --enable=rhel-7-server-ansible-2-rpms
# INSTALL ANSIBLE
yum install ansible
– Wikipedia
18 Ansible Workshop
ANSIBLE COMMANDS
INVENTORY
Use the default one (/etc/ansible/hosts) or create an inventory file
[all:vars]
ansible_ssh_user=centos
[web]
web1 ansible_ssh_host=centos2
[admin]
ansible ansible_ssh_host=centos1
[defaults]
inventory=/home/centos/ansible/inventory
Objectives
Using Ansible commands, complete the following tasks:
1. Test Ansible connection to all your hosts using ping module
2. Install HTTPD only on your web hosts
3. Change SELINUX to permissive mode (all hosts)
Modules documentation:
http://docs.ansible.com/ansible/list_of_all_modules.html
---
- name: This is a Play
hosts: web
remote_user: centos
become: yes
gather_facts: no
vars:
state: present
tasks:
- name: Install Apache
yum: name=httpd state={{ state }}
http://docs.ansible.com/ansible/setup_module.html
tasks:
- name: Install Apache
yum: name=httpd state={{ state }}
**** When a variable is used as the first element to start a value, quotes are mandatory.
tasks:
- name: Install Apache and PHP
yum: name={{ item }} state={{ state }}
with_items:
- httpd
- php
➔ with_nested
➔ with_dict
➔ with_fileglob
➔ with_together
➔ with_sequence
➔ until
➔ with_random_choice
➔ with_first_found
➔ with_indexed_items
➔ with_lines
http://docs.ansible.com/ansible/playbooks_loops.html
tasks:
- yum: name={{ item }} state=installed
with_items:
- httpd
- memcached
notify: Restart Apache
- template: src=templates/web.conf.j2
dest=/etc/httpd/conf.d/web.conf
notify: Restart Apache
handlers:
- name: Restart Apache
service: name=httpd state=restarted
tasks:
tasks:
- name: install Apache
yum: name=httpd state=installed
when: ansible_os_family == "RedHat"
tasks:
- block:
- yum: name={{ item }} state=installed
with_items:
- httpd
- memcached
- template: src=templates/web.conf.j2 dest=/etc/httpd/conf.d/web.conf
- service: name=bar state=started enabled=True
when: ansible_distribution == 'CentOS'
By default, Ansible stop on errors. Add the ingore_error parameter to skip potential errors.
tasks:
- block:
- debug: msg='i execute normally'
- command: /bin/false
- debug: msg='i never execute, cause ERROR!'
rescue:
- debug: msg='I caught an error'
- command: /bin/false
- debug: msg='I also never execute :-('
always:
- debug: msg="this always executes"
★ hostvars[inventory_hostname]
Show all ansible facts
Specific variable for specific host
{{ hostvars['test.example.com']['ansible_distribution'] }}
★ group_names
is a list (array) of all the groups the current host is in
★ groups
is a list of all the groups (and hosts) in the inventory.
- name: debug
hosts: all
tasks:
- name: Show hostvars[inventory_hostname]
debug: var=hostvars[inventory_hostname]
vars:
var1: {{ foo }} <<< ERROR!
var2: “{{ bar }}”
var3: Echoing {{ foo }} here is fine
Documentation:
http://docs.ansible.com/ansible/template_module.html
Jinja2 is a modern and designer-friendly templating language for Python, modelled after
Django’s templates and used by Ansible.
Highly recommend reading about Jinja2 to understand how templates are built.
{{ variable }}
web1 10.0.1.1
web2 10.0.1.2
web3 10.0.1.3
{% if ansible_processor_cores >= 2 %}
-smp enable
{% else %}
-smp disable
{% endif %}
{% set my_var='this-is-a-test' %}
{{ my_var | replace('-', '_') }}
this_is_a_test
server1
server2
server3
# md5sum of a filename
{{ filename | md5 }}
# Comparisons
{{ ansible_distribution_version | version_compare('12.04', '>=') }}
# Default if undefined
{{ user_input | default(‘Hello World') }}
{% if variable is defined %}
{% if variable is none %}
{% if variable is even %}
{% if variable is string %}
{% if variable is sequence %}
- set_fact:
apache_version: ”{{ result.stdout }}"
Objectives
Copy and modify you lab2 playbook to add the following:
1. Use the debug.yml (see next slide) file to explore all the ansible facts
2. Convert your MOTD file in a template saying : “Welcome to
<hostname>!”
3. Install facter on all your hosts then re-execute the debug.yml. You
should see a bunch of new variables (facter_)
4. Convert your index.html file into a Jinja2 template to output the
following information:
Web Servers
centos1 192.168.3.52 - free memory: 337.43 MB
---
- name: debug
hosts: all
tasks:
❏ tasks
❏ files
❏ scripts
❏ templates
❏ variables
➔ install packages
➔ copying files
➔ starting deamons
roles
└── myapp
├── defaults
├── files
├── handlers
├── tasks
├── templates
└── vars
---
- hosts: webservers
roles:
- common
- webservers
---
- hosts: webservers
roles:
- common
- { role: myapp, dir: '/opt/a', port: 5000 }
- { role: myapp, dir: '/opt/b', port: 5001 }
---
- hosts: webservers
roles:
- { role: foo, when: "ansible_os_family == 'RedHat'" }
---
- hosts: webservers
serial: 1
pre_tasks:
- command:lb_rm.sh {{ inventory_hostname }}
delegate_to: lb
- command: mon_rm.sh {{ inventory_hostname }}
delegate_to: nagios
roles:
- myapp
post_tasks:
- command: mon_add.sh {{ inventory_hostname }}
delegate_to: nagios
- command: lb_add.sh {{ inventory_hostname }}
delegate_to: lb
75 Ansible Workshop
ANSIBLE GALAXY
Objectives
1. Create 2 roles: common and apache
2. Create a playbook to apply those roles.
a. “common” should be applied to all servers
b. “apache” should be applied to your “web” group
3. Put the jinja2 templates in the appropriate folder.
plus.google.com/+RedHat facebook.com/redhatinc
linkedin.com/company/red-hat twitter.com/RedHatNews
youtube.com/user/RedHatVideos
EXTRA STUFF
FIXING VIM FOR YAML EDITION
$ vim
:PlugInstall
https://www.youtube.com/watch?v=wbhdJE7DM-A
DEMO STARTS AT 10:24
[defaults]
gathering = smart
fact_caching = jsonfile
fact_caching_connection = /path/to/cachedir
fact_caching_timeout = 86400
[defaults]
stdout_callback = debug