AnsibleWorkshopWA PDF
AnsibleWorkshopWA PDF
AnsibleWorkshopWA PDF
0
Introduction to Ansible training
Marco Berube
sr. Cloud Solution Architect
Michael Lessard
Senior Solutions Architect
Martin Sauvé
Senior Solutions Architect
AGENDA
Ansible Training
1 Introduction to Ansible
+ DEMO 4 Ansible variables
+ LAB
2 Ansible commands
+ LAB 5 Ansible roles
+ LAB
3 Ansible playbooks
+ LAB 6 Ansible tower
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
Provisioning
Configuration management
Application deployments
Rolling upgrades - CD
Security and Compliance
Orchestration
7 RHUG Ansible Workshop
BENEFITS
Why is Ansible popular?
★ Modules (Tools)
★ Tasks
★ Inventory
★ Plays
★ Playbook (Plan)
# INSTALL ANSIBLE
yum install ansible
[all:vars]
ansible_ssh_user=centos
[web]
web1 ansible_ssh_host=centos2
[admin]
ansible ansible_ssh_host=centos1
Objectives
Using Ansible commands, complete the following tasks:
1. Test Ansible connection to all your hosts using ping module
2. Install EPEL repo on all your hosts
3. Install HTTPD only on your web hosts
4. Change SELINUX to permissive mode
Modules documentation:
http://docs.ansible.com/ansible/list_of_all_modules.html
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 }}
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
handlers:
- name: Restart Apache
service: name=httpd state=restarted
tasks:
- debug: var=result
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'
You can apply a special type of conditional that if true will cause an error to be
thrown.
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"
Objectives
Using an Ansible playbook:
1. Change SELINUX to permissive mode on all your hosts
2. Install HTTPD on your web hosts only
3. Start and Enable HTTPD service on web hosts only if a new httpd
package is installed.
4. Copy an motd file saying “Welcome to my server!” to all your hosts
5. Copy an “hello world” index.html file to your web hosts in
/var/www/html
6. Modify the sshd.conf to set PermitRootLogin at no
tasks:
- name: Configure selinux to {{ selinux }}
selinux:
policy: targeted
state: "{{ selinux }}"
tasks:
- name: Install Apache
yum: name=httpd state=present
notify: Restart Apache
handlers:
- name: Restart Apache
service: name=httpd state=restarted enabled=yes
- name: RestartSSH
Service: name=sshd state=restarted enambles=yes
---
- name: Lab2 - All server setup
hosts: all
become: yes
tasks:
- name: Configure selinux to {{ selinux }}
selinux:
policy: targeted
state: "{{ selinux }}"
...
★ hostvars[inventory_hostname]
★ hostvars[<any_hostname>]
{{ 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]
Documentation:
http://docs.ansible.com/ansible/template_module.html
Ansible uses Jinja2. 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 %}
vars:
var1: {{ foo }} <<< ERROR!
var2: “{{ bar }}”
var3: Echoing {{ foo }} here is fine
- set_fact:
apache_version: ”{{ result.stdout }}"
Objectives
Modify you lab2 playbook to add the following:
1. Convert your MOTD file in a template saying : “Welcome to
<hostname>!”
2. Install facter to all your hosts using an ansible command
3. Convert your index.html file into a template to output the following
information:
Web Servers
lab1 192.168.3.52 - free memory: 337.43 MB
lab2 192.168.3.53 - free memory: 346.82 MB
---
- name: debug
hosts: all
tasks:
tasks:
- name: Configure selinux to permissive
selinux:
policy: targeted
state: permissive
tasks:
- name: Install Apache
yum: name=httpd state=present
notify: Restart Apache
handlers:
- name: Restart Apache
service: name=httpd state=restarted enabled=yes
index.html.j2
Web Servers<br>
{% for server in groups.web %}
{{ server }} {{ hostvars[server].ansible_default_ipv4.address }} - free memory: {{ hostvars[server].facter_memoryfree
}}<br>
{% endfor %}
❏ tasks
❏ files
❏ scripts
❏ templates
❏ variables
➔ install packages
➔ copying files
➔ starting deamons
roles
└── myapp
├── defaults
├── files
├── handlers
├── meta
├── tasks
├── templates
└── vars
69 RHUG Ansible Workshop
ROLES
Create folder structure automatically
---
- 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'" }
pre_tasks:
- command:lb_rm.sh {{ inventory_hostname }}
delegate_to: lb
roles:
- myapp
post_tasks:
- command: mon_add.sh {{ inventory_hostname }}
delegate_to: nagios
Objectives
1. Create 3 roles: common, apache and haproxy
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
c. “haproxy” should be applied to your “lb” group
3. Your index.html should return the web server name.
4. selinux state should be a set as a variable in group_vars “all”
http://people.redhat.com/mlessard/qc/haproxy.tar.gz
https://github.com/masauve/ansible-labs
plus.google.com/+RedHat facebook.com/redhatinc
linkedin.com/company/red-hat twitter.com/RedHatNews
youtube.com/user/RedHatVideos
FIXING VIM FOR YAML EDITION
$ vim
:PlugInstall
---
language: python
python: "2.7"
# Install ansible
addons:
apt:
packages:
- python-pip
install:
# Install ansible
- pip install ansible
script:
# Basic role syntax check
- ansible-playbook tests/test.yml -i tests/inventory --syntax-check
notifications:
webhooks: https://galaxy.ansible.com/api/v1/notifications/