I have a patch locally that enables pre and post processing for roles.
The directory names are pre_tasks and post_tasks, of course. The flow is:
- hosts: targets
roles:
- role-1
- role-2
pre_tasks:
- name: one
tasks:
- name: two
post_tasks:
- name: three
and evaluates as:
role-1:pre_tasks
role-2:pre_tasks
play:pre_tasks
role-1:tasks
role-2:tasks
play:tasks
role-1:post_tasks
role-2:post_tasks
play:post-tasks
I use it to have a role nginx-frontend, then then wants to know about
ofbiz-backend and php-backend, and other hosts that then have those
roles. I then have the backend role notify a handler, that then updates
/etc/hosts with the address found in $hostvars.
I'm wondering if this makes sense to anyone else, and if it would be
wanted? I haven't yet modified docsite, because I'm asking here first.
The following examples show a complex backend registration system; I hit
some snags with ordering, so I added the pre/post support for roles,
which should allow the main-line tasks to then query for the list of
backend mappings. I haven't yet modified my backend role(shown below)
with this new feature.
== examples:playbook.yml
- hosts: targets
accelerate: true
gather_facts: true
roles:
- etckeeper
- { role: nginx-frontend, when: "inventory_hostname in
(groups['frontend'] | default([]))" }
- { role: php-backend, when: "inventory_hostname in
(groups['php-backend'] | default([]))" }
- { role: mysql-backend, when: "inventory_hostname in
(groups['mysql-backend'] | default([]))" }
- { role: postgresql-backend, when: "inventory_hostname in
(groups['postgresql-backend'] | default([]))" }
- { role: ofbiz-backend, when: "inventory_hostname in
(groups['ofbiz-backend'] | default([]))" }
- { role: image-processor, when: "inventory_hostname in
(groups['image-processor'] | default([]))" }
== examples: nginx-frontend/meta.yml
dependencies:
- role: backend
tags: ['nginx']
args:
role: nginx-frontend
roles: ['ofbiz-backend', 'php-backend']
== examples: php-backend/meta.yml
dependencies:
- role: backend
tags: ['php']
args:
role: php-backend
roles: ['mysql-backend']
== examples: backend/tasks.yml
- set_fact:
host_roles: "{{host_roles is defined and
(host_roles.append(args.role) or host_roles) or [args.role]}}"
when: args.roles is defined
- set_fact:
wanted_roles: "{{wanted_roles is defined and
(wanted_roles.extend(args.roles) or wanted_roles) or args.roles}}"
notify:
- roles-scan {{ args.role }}
== examples: backend/handlers.yml
- name: roles-scan {{ args.role }}
when: item[1] in wanted_roles
lineinfile:
dest=/etc/hosts
regexp='^\S+\s+.*?{{item[1]}}.universe'
line='{{item[0].ansible_eth0.ipv4.address}} {{item[1]}}.universe'
notify:
- etckeeper commit
register: roles_scan_result
with_subelements:
- hostvars.values() | selectattr('host_roles', 'defined') | list
- 'host_roles'
==
--
You received this message because you are subscribed to the Google Groups "Ansible
Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to ansible-project+unsubscr...@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.