aboutsummaryrefslogtreecommitdiff
path: root/ansible/roles/ovn-db/tasks/lookup_cluster.yml
blob: 86b4aa13afbab2158c439f402bf2b0018a1fd30a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
---
- name: Checking for any existing OVN DB container volumes
  become: true
  kolla_container_facts:
    container_engine: "{{ kolla_container_engine }}"
    action: get_volumes
    name:
      - ovn_nb_db
      - ovn_sb_db
  register: ovn_db_container_volume_facts

- name: Divide hosts by their OVN NB volume availability
  group_by:
    key: "ovn-nb-db_had_volume_{{ ovn_db_container_volume_facts.volumes['ovn_nb_db'] is defined }}"
  changed_when: false

- name: Divide hosts by their OVN SB volume availability
  group_by:
    key: "ovn-sb-db_had_volume_{{ ovn_db_container_volume_facts.volumes['ovn_sb_db'] is defined }}"
  changed_when: false

- name: Establish whether the OVN NB cluster has already existed
  set_fact:
    ovn_nb_db_cluster_exists: "{{ groups['ovn-nb-db' + '_had_volume_True'] is defined }}"

- name: Establish whether the OVN SB cluster has already existed
  set_fact:
    ovn_sb_db_cluster_exists: "{{ groups['ovn-sb-db' + '_had_volume_True'] is defined }}"

- name: OVN NB checks
  block:

    - name: Check if running on all OVN NB DB hosts
      fail:
        msg: >
          Some hosts need database bootstrapping, but not all OVN NB DB hosts
          ({{ groups['ovn-nb-db'] | join(', ') }}) are in the target list
          ({{ groups['ovn-nb-db'] | difference(ansible_play_batch) | list | join(', ') }}).
          Stopping as it may be unsafe to proceed. Please run without --limit
          or --serial to bootstrap these hosts.
      when:
        - ovn_nb_db_cluster_exists
        - groups['ovn-nb-db'] | difference(ansible_play_batch) | list | length > 0

    - name: Check OVN NB service port liveness
      wait_for:
        host: "{{ api_interface_address }}"
        port: "{{ ovn_nb_db_port }}"
        connect_timeout: 1
        timeout: 10
      register: check_ovn_nb_db_port_liveness
      ignore_errors: yes

    - name: Divide hosts by their OVN NB service port liveness
      group_by:
        key: "ovn-nb-db_port_alive_{{ check_ovn_nb_db_port_liveness is success }}"
      changed_when: false

    - name: Get OVN NB database information
      command: >
        {{ kolla_container_engine }} exec ovn_nb_db ovsdb-client query unix:/run/ovn/ovnnb_db.sock
        "[\"_Server\",{\"table\":\"Database\",\"where\":[[\"name\",\"==\", \"OVN_Northbound\"]],\"op\":\"select\"}]"
      become: true
      when: check_ovn_nb_db_port_liveness is success
      changed_when: false
      register: ovn_nb_db_info

    - name: Divide hosts by their OVN NB leader/follower role
      group_by:
        key: "ovn-nb-db_{{ 'leader' if (ovn_nb_db_info.stdout | from_json).0.rows.0.leader else 'follower' }}"
      when: check_ovn_nb_db_port_liveness is success
      changed_when: false

    - name: Fail on existing OVN NB cluster with no leader
      fail:
        msg: OVN NB cluster exists but there is no leader - please check cluster status
      when:
        - groups['ovn-nb-db_leader'] is not defined and groups['ovn-nb-db_follower'] is defined

  any_errors_fatal: true
  when: inventory_hostname in groups.get('ovn-nb-db_had_volume_True', '')

- name: OVN SB checks
  block:

    - name: Check if running on all OVN SB DB hosts
      fail:
        msg: >
          Some hosts need database bootstrapping, but not all OVN SB DB hosts
          ({{ groups['ovn-sb-db'] | join(', ') }}) are in the target list
          ({{ groups['ovn-sb-db'] | difference(ansible_play_batch) | list | join(', ') }}).
          Stopping as it may be unsafe to proceed. Please run without --limit
          or --serial to bootstrap these hosts.
      when:
        - ovn_sb_db_cluster_exists
        - groups['ovn-sb-db'] | difference(ansible_play_batch) | list | length > 0

    - name: Check OVN SB service port liveness
      wait_for:
        host: "{{ api_interface_address }}"
        port: "{{ ovn_sb_db_port }}"
        connect_timeout: 1
        timeout: 10
      register: check_ovn_sb_db_port_liveness
      ignore_errors: yes

    - name: Divide hosts by their OVN SB service port liveness
      group_by:
        key: "ovn-sb-db_port_alive_{{ check_ovn_sb_db_port_liveness is success }}"
      changed_when: false

    - name: Get OVN SB database information
      command: >
        {{ kolla_container_engine }} exec ovn_sb_db ovsdb-client query unix:/run/ovn/ovnsb_db.sock
        "[\"_Server\",{\"table\":\"Database\",\"where\":[[\"name\",\"==\", \"OVN_Southbound\"]],\"op\":\"select\"}]"
      become: true
      when: check_ovn_sb_db_port_liveness is success
      changed_when: false
      register: ovn_sb_db_info

    - name: Divide hosts by their OVN SB leader/follower role
      group_by:
        key: "ovn-sb-db_{{ 'leader' if (ovn_sb_db_info.stdout | from_json).0.rows.0.leader else 'follower' }}"
      when: check_ovn_sb_db_port_liveness is success
      changed_when: false

    - name: Fail on existing OVN SB cluster with no leader
      fail:
        msg: OVN SB cluster exists but there is no leader - please check cluster status.
      when:
        - groups['ovn-sb-db_leader'] is not defined and groups['ovn-sb-db_follower'] is defined

  any_errors_fatal: true
  when: inventory_hostname in groups.get('ovn-sb-db_had_volume_True', '')