/etc/ansible/roles/bird/tasks/bird_post_install.yml
---
# Copyright 2016, Logan Vig <logan2211@gmail.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

- name: Set fact for automatically enabling networks (IPv4)
  set_fact:
    bird_ipv4_enabled: "{{ ansible_default_ipv4 is defined | bool }}"
  when:
    - "bird_ipv4_enabled == 'detect'"
  tags:
    - bird-config
    - bird-init

- name: Set fact for automatically enabling networks (IPv6)
  set_fact:
    bird_ipv6_enabled: >-
      {{ (ansible_default_ipv6 is defined
         and ansible_default_ipv6['scope'] is defined
         and ansible_default_ipv6['scope'] == 'global') | bool }}
  when:
    - "bird_ipv6_enabled == 'detect'"
  tags:
    - bird-config
    - bird-init

- name: Drop bird configuration files
  template:
    src: bird-common.conf.j2
    dest: "{{ item.config_file }}"
    owner: "root"
    group: "root"
    mode: "0644"
  register: bird_results
  when: "item.enabled | bool"
  loop: "{{ bird_configs }}"
  loop_control:
    label: "{{ item.config_file }}"
  tags:
    - bird-config

- name: Restart bird services
  service:
    name: "{{ item.item.service }}"
    state: restarted
  when: "item is changed"
  ignore_errors: "{{ ansible_check_mode | default(false) }}"
  loop: "{{ bird_results.results }}"
  loop_control:
    label: "{{ item.item.service }}"
  tags:
    - upstart-init
    - bird-init

- name: Disable inactive BIRD BGP services and remove from bootup start
  service:
    name: "{{ item.service }}"
    enabled: false
    state: stopped
  when:
    - "not item.enabled | bool"
    - "item.enabled != 'skip'"
  loop: "{{ bird_configs }}"
  loop_control:
    label: "{{ item.service }}"
  ignore_errors: "{{ ansible_check_mode | default(false) }}"
  tags:
    - upstart-init
    - bird-init

- name: Start active BIRD BGP services and add to bootup start
  service:
    name: "{{ item.service }}"
    enabled: true
    state: started
  when:
    - "item.enabled | bool"
    - "item.enabled != 'skip'"
  loop: "{{ bird_configs }}"
  loop_control:
    label: "{{ item.service }}"
  ignore_errors: "{{ ansible_check_mode | default(false) }}"
  tags:
    - upstart-init
    - bird-init