๐Ÿ“— Ansible playbooks and roles for building an idempotent, interconnected and scalable infrastructure
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ansible-playbooks/all.yml

73 lines
3.0 KiB

2 years ago
- hosts: all
gather_facts: no
serial: "{{ hosts_per_batch | d(1) | int }}"
strategy: "{{ hosts_strategy | d('linear') }}"
tasks:
# TODO: multiple roles
- block:
- name: get primary role
set_fact:
host_primary_role: "{%- if primary_role is defined -%}{{ primary_role }}\
{%- elif hostvars[inventory_hostname]['primary_role'] is defined -%}{{ hostvars[inventory_hostname]['primary_role'] }}\
{%- else -%}{{ inventory_hostname }}\
{%- endif -%}"
2 years ago
- name: check if role info file exists
stat:
path: "{{ (playbook_dir, 'roles', host_primary_role, 'tasks', 'info.yml') | path_join }}"
delegate_to: localhost
register: result
no_log: yes
2 years ago
- name: get role info
include_role:
name: "{{ host_primary_role }}"
tasks_from: info.yml
apply:
no_log: yes
when: result.stat.exists
2 years ago
when: no_primary_role | d(false) == false
2 years ago
- name: build role dependency list and stage info
2 years ago
set_fact:
role_deps: "{{ (([] if no_primary_role | d(false) == true else ([{ 'stage': 6, 'role': host_primary_role }] if role_dependency is not defined else [role_dependency])) +
([role_dependency_common] if (role_dependency_no_common | d(false) == false) else []) +
([{ 'stage': 1, 'role': 'container' }] if 'containers' in group_names else []) +
([{ 'stage': 3, 'role': 'postgres', 'tasks_from': 'integrate.yml' }] if role_use_database | d(false) == true else [])
) | flatten(levels=1) | sort(attribute='stage') }}"
2 years ago
selected_stages: "{%- if stage is defined and ((stage | string) is search(',')) -%}{{ stage | string | split(',') | list | map('int') | list }}\
{%- elif (stage is not defined) or ((stage | int) == 0) -%}{{ [1,2,3,4,5,6,7,8,9] }}\
{%- else -%}{{ [stage | int] }}\
{%- endif -%}"
no_log: yes
- name: show deployment info
debug:
msg: "deploying role {{ host_primary_role | d('(no role)') }}\" on host \"{{ inventory_hostname }}\", {{
2 years ago
(('stages ' if (selected_stages | length > 1) else 'stage ') ~ (selected_stages | join(', ')))
if ([1,2,3,4,5,6,7,8,9] | symmetric_difference(selected_stages) | list | length > 0) else 'all stages' }}\n\n{{
'dependencies:\n' ~ (role_deps | map(attribute='stage') | list | zip(
role_deps | map(attribute='role') | list
) | map('join', ': ') | list | join('\n'))
}}"
2 years ago
- name: run pre_tasks
include_tasks: tasks/pre_tasks.yml
- name: run stages
include_tasks: tasks/includes/stage.yml
loop: "{{ selected_stages }}"
loop_control:
loop_var: this_stage
- name: show deployment info
debug:
msg: "ok: deployment completed on host \"{{ inventory_hostname }}\""