2. ABOUT ME
STL Native, previously worked for Savvis/CenturyLink and
Scottrade
Started contributing to Cobbler in September of 2008, and took
over the project leadership in 2010
Joined Ansible in July, 2013
6. BAD!
$ mkdir -p roles/myrole/{tasks,vars}
$ touch roles/myrole/{tasks,vars}/main.yml
This is a very manual process, and completely unnecessary...
7. A MUCH BETTER WAY ™
$ ansible-galaxy init roles/myrole
Benefits include:
Creates ALL of the directory structure for you
Stubs out some of the YAML files like `meta/main.yml`
11. When applying a tag or conditional statemtent to a role
definition, they are appended to those specified on any tasks
within the role. For example:
# main yaml file
roles:
- { role: foo, tags: 'foo', when: some_var == 'foo' }
# roles/foo/meta/main.yml
dependencies:
- { role: bar }
All tasks defined within 'bar' will also have the above tags and
'when' statement applied to them, in addition to any other tags or
conditionals defined on the task.
17. WHY DOESN'T THIS WORK?
1. Handler names in Ansible must be unique.
2. Included files are always read and parsed at load time, BUT the
conditionals are not evaluated until the task is executed...
3. Result - the last handler defined with the name "wins", and the
wrong handler may be run.
18. THE SOLUTION: SET_FACT
# apache/tasks/redhat.yml
- name: install apache packages
yum: name=httpd state=present
- name: set the name of the service
set_fact: apache_service_name=httpd
# apache/tasks/debian.yml
- name: install apache packages
apt: name=apache2 state=present
- name: set the name of the service
set_fact: apache_service_name=apache2
# apache/handlers/main.yml
- name: restart apache service
service: name={{apache_service_name}} state=restarted
20. We can use set_fact again in the platform-specific includes:
# apache/tasks/redhat.yml
- name: set variables for this OS
set_fact:
apache_service_name: httpd
apache_user: apache
apache_httpd_conf_path: /etc/httpd/conf/httpd.conf
apache_httpd_confd_path: /etc/httpd/conf.d
...
# apache/tasks/debian.yml
- name: set the name of the service
set_fact:
apache_service_name: apache2
apache_user: www-data
apache_httpd_conf_path: /etc/apache2/apache2.conf
apache_httpd_confd_path: /etc/apache2/conf-available
...
21. And in our updated common tasks/main.yml:
# apache/tasks/main.yml
# (continued)
- name: deploy apache configuration
template:
src: httpd.conf.j2
dest: "{{apache_httpd_conf_path}}"
owner: "{{apache_user}}"
mode: "0640"
notify: restart apache service
- name: make sure service is running and enabled
service:
name: "{{apache_service_name}}"
state: running
enabled: yes
23. It is possible to use the include_vars and with_first_found lookup
to include the distro-specific variables file, and to fall back to a
default set of variables:
# apache/tasks/main.yml
- include_vars: "{{ item }}"
with_first_found:
- "{{ ansible_os_family }}.yml"
- "default.yml"
26. When you use the ansible-galaxy CLI command to init a new role,
a sample meta/main.yml is automatically created for you:
---
galaxy_info:
author: your name
description:
company: your company (optional)
license: BSD
min_ansible_version: 1.2
#platforms:
#- name: EL
# versions:
# - all
# - 5
# - 6
# - 7
...
#categories:
#- cloud
...
#- web
dependencies: []
27. LISTING SANE DEPENDENCIES
Dependencies listed in the metadata should only point to other
Galaxy roles.