->Introduction
->>What is Ansible?
->>Ansible history
->Basic concepts
->>Inventory
->>Playbook
->>Role
->>Module
->>Plugin
->Diving into Ansible roles
->>Getting started
->>Create a role
->>Roles under the hood
->>How to use roles?
Precise and Complete Requirements? An Elusive Goal
Getting started with Ansible
1. Getting started with Ansible
Alexander Saprykin
Senior Software Engineer
13th October 2018
2. 2
Introduction
What is Ansible?
Ansible history
Diving into Ansible roles
Basic concepts
Inventory
Playbook
Role
Module
Plugin
Agenda
Getting started
Create a role
Roles under the hood
How to use roles?
5. 5
What is inventory?
Inventory
● Defines the infrastructure
● Static inventory - can be sourced from a text file (INI or YAML format)
● Dynamic inventory - generated from a script
● Ansible provides dozens of inventory scripts (e.g. AWS EC2, OpenStack, Docker):
https://github.com/ansible/ansible/tree/devel/contrib/inventory
30. 30
Role
● Self-contained, reusable, complete unit of work
● Decoupled from assumptions made by plays
● Decoupled from inventory
● Encourages collaboration
31. 31
Module
● Called by a task (or used ad-hoc)
● Perform an action on a target host
● Can take direct action, wrap a command line tool, or talk to an API
● Ansible includes a 100’s of modules:
https://github.com/ansible/ansible/tree/devel/lib/ansible/modules
32. 32
Facts
● Provided by setup module
● Returned by modules
● Created using set_fact module
● Use for variable substitution, and conditional checks
33. 33
Plugin
● Augments Ansible core functionality
● Plugin types: action, cache, callback, connection, filter, lookup, shell, strategy,
terminal, test, vars
● Examples:
○ connection: local, ssh, docker, chroot
○ action: copy, fetch, synchronize
● See full list at https://github.com/ansible/ansible/tree/devel/lib/ansible/plugins
36. 36
Playbooks
● Made up of plays
● Plays are opinionated: become, gather_facts, connection, vars, etc.
● Assume a specific inventory
● Target a specific use case
● Generally not reusable
39. 39
Roles
● Decoupled from inventory and plays
● Not Just a set of tasks
● Self-contained, reusable, complete unit of work
40. 40
● Install packages
● Update configuration
● Run tests
● Package software
● Build images
● Orchestrate containers
What you can do with roles?
41. 41
Switch to a role
- name: install and start nginx
hosts: web
become: yes
roles:
- role: install-nginx
packages:
- nginx
- python-pip
- python-devel
- gcc
nginx_test_message: This is a test message
nginx_keepalive_timeout: 115
42. 42
Switch to a role
- name: install and start nginx
hosts: web
become: yes
roles:
- role: install-nginx
packages:
- nginx
- python-pip
- python-devel
- gcc
nginx_test_message: This is a test message
nginx_keepalive_timeout: 115
43. 43
Switch to a role
- name: install and start nginx
hosts: web
become: yes
roles:
- role: install-nginx
packages:
- nginx
- python-pip
- python-devel
- gcc
nginx_test_message: This is a test message
nginx_keepalive_timeout: 115
44. 44
Switch to a role
- name: install and start nginx
hosts: web
become: yes
roles:
- role: install-nginx
packages:
- nginx
- python-pip
- python-devel
- gcc
nginx_test_message: This is a test message
nginx_keepalive_timeout: 115
46. 46
From scratch
Create a role
1. Create a roles directory
2. Create a minimal role directory structure:
mkdir -p ./roles/nginx-install/tasks/
3. Start writing your tasks in main.yml file in tasks directory
47. 47
From template
Create a role
● Ansible Galaxy client tool
ansible-galaxy init nginx-install
● Creates a complete directory structure
● Creates default files
48. 48
From Ansible Galaxy (...or more)
Or just download one
● From Ansible Galaxy - https://galaxy.ansible.com
ansible-galaxy install <namespace>.<role-name>
● From Git
ansible-galaxy install git+https://github.com/acme/nginx-install.git
● ...
49. 49
Where are my roles?
● ANSIBLE_ROLES_PATH
● ansible.cfg
● Provide a colon : separated list of paths
● roles directory next to the playbook
[defaults]
roles_path=/path/to/roles
52. 52
Tasks
● tasks directory
● Entrypoint: tasks/main.yml
● Contains the main list of tasks to be executed by the role
● Tie together handlers, templates, files, variables and defaults
54. 54
● handlers directory
● Entrypoint: handlers/main.yml
● Module indicates when a change has been made
● In response to a change, a notify action can be triggered
● Notify handlers by name…
● … or by topic - new in Ansible 2.2
Handlers
56. - name: start nginx
service: name=nginx state=started enabled=yes
56
Handlers by name
Handler from handlers/main.yml
57. - name: start and enable nginx
service: name=nginx state=started enabled=yes
listen: start nginx
- name: Restart and enable supervisord
service: name=supervisord state=restarted enabled=yes
listen: start nginx
57
Handlers by topic
Handler from handlers/main.yml
58. 58
● files is the base directory for copy and synchronize modules
● Files are copied to the target node
● templates is the base directory for the template module
● Templates contain variables - during execution, the file is transformed, and the result
is copied to the target node
● Ansible uses Jinja2 as a template engine:
https://docs.ansible.com/ansible/latest/user_guide/playbooks_templating.html
Files and templates
63. 63
● defaults/main.yml
○ Defines variables the user can override to change role behavior (e.g.
conditionals, configuration settings)
● variables/main.yml
○ Used by the author to organize the role (e.g. constants, choices)
○ Add additional files to dynamically shape the role
Variables
73. 73
● Play can include roles and tasks
○ Roles are executed first, then tasks
○ For readability, list roles first
● Consider surfacing all defaults in the playbook
○ Make the playbook self-documenting
In a play
74. 74
In a play
hosts: web
name: install and start nginx with wsgi
become: yes
roles:
- role: install-nginx
packages:
- nginx
- python-pip
- python-devel
- gcc
nginx_test_message: This is a test message
nginx_keepalive_timeout: 115
75. 75
● New in Ansible 2.2
● include_role
● Treats the role more like a task
In a task
76. 76
In a play
- name: Run my role
include_role:
name: myrole
- name: Run tasks/other.yml instead of 'main'
include_role:
name: myrole
tasks_from: other
- name: Pass variables to role
include_role:
name: myrole
vars:
rolevar1: 'value from task'
77. 77
In a play
- name: Run my role
include_role:
name: myrole
- name: Run tasks/other.yml instead of 'main'
include_role:
name: myrole
tasks_from: other
- name: Pass variables to role
include_role:
name: myrole
vars:
rolevar1: 'value from task'
78. 78
In a play
- name: Run my role
include_role:
name: myrole
- name: Run tasks/other.yml instead of 'main'
include_role:
name: myrole
tasks_from: other
- name: Pass variables to role
include_role:
name: myrole
vars:
rolevar1: 'value from task'
79. 79
In a play
- name: Run my role
include_role:
name: myrole
- name: Run tasks/other.yml instead of 'main'
include_role:
name: myrole
tasks_from: other
- name: Pass variables to role
include_role:
name: myrole
vars:
rolevar1: 'value from task'
80. 80
In a play
- name: Use role in loop
include_role:
name: myrole
with_items:
- '{{ roleinput1 }}'
- '{{ roleinput2 }}'
- name: Conditional role
include_role:
name: myrole
when: not some_condition
81. 81
In a play
- name: Use role in loop
include_role:
name: myrole
with_items:
- '{{ roleinput1 }}'
- '{{ roleinput2 }}'
- name: Conditional role
include_role:
name: myrole
when: not some_condition