summaryrefslogtreecommitdiffstats
path: root/roles/ands_network/tasks/nm_configure_connection.yml
blob: 9354fbfd836001f84bff1226a930f2eb839fbb4b (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
- name: "detect nm connection corresponding to interface '{{ bridge | default(iface) }}'"
  shell: "nmcli d show {{ iface | quote }} | grep CONNECTION | cut -d ':' -f 2- | sed -E -e 's/^[[:space:]]+//' | grep '^[[:alpha:]]'"
  register: conres
  failed_when: false
  changed_when: false


- name: "check if the requested ip '{{ cidr }}' is present on the interface '{{ biface }}'"
  set_fact:
    ip_present: "{{ cidr | ipaddr('address') in ips }}"
  vars:
    biface:  "{{ bridge | default(iface) }}"
    eth:     "{{ hostvars[inventory_hostname]['ansible_' + biface] | default({}) }}"
    ipv4:    "{{ eth['ipv4'] | default({}) }}"
    q:       "{{ eth | json_query('ipv4_secondaries[*].address') }}"
    sec:     "{{ ((q == ands_none) or (q == '')) | ternary([], q) }}"
    ips:     "{{ sec  | union([ipv4.address]) }}"
  when: 
    - conres.rc == 0
#    - eth.ipv4 is defined

- name: "destroy connection '{{ conres.stdout }}' if ip does not match"
  command: "nmcli connection delete {{ conres.stdout | quote }}"
  register: delres
  when: 
    - conres.rc == 0
    - force | default(false)
    - not (alias | default(false))
    - not ip_present
    
- name: "create bridge '{{ bridge }}' with cidr '{{ cidr }}'"
  command: "nmcli connection add type bridge ifname {{ bridge | quote }} con-name {{ name }} ip4 {{ cidr }}"
  when: 
    - bridge is defined
    - (conres.rc != 0) or (not (delres | skipped))
    - (conres.rc != 0) or (not (alias | default(false)))
  
- name: "connect bridge '{{ bridge }}' to interface '{{ iface }}'"
  command: "nmcli connection add type bridge-slave ifname {{ iface | quote }} master {{ bridge | quote }}"
  when: 
    - bridge is defined
    - (conres.rc != 0) or (not (delres | skipped))
    - (conres.rc != 0) or (not (alias | default(false)))
    
- name: "configure network interface '{{ iface }}' to '{{ cidr }}'"
  command: "nmcli connection add type infiniband ifname {{ iface | quote }} con-name {{ name }} ip4 {{ cidr }}"
  when: 
    - bridge is not defined
    - (conres.rc != 0) or (not (delres | skipped))
    - (conres.rc != 0) or (not (alias | default(false)))

- name: "add ip alias '{{ cidr }}' to connection '{{ conres.stdout }}' using interface '{{ iface }}'"
  command: "nmcli connection modify {{ conres.stdout | quote }} +ipv4.address {{ cidr }}"
  register: alres
  when: 
    - alias | default(false)
    - conres.rc == 0
    - not ip_present
    
- name: "start connection {{ cname  }}"
  command: "nmcli connection up {{ cname | quote }}"
  register: alres
  vars:
    cname: "{{ (conres.stdout == '') | ternary(name, conres.stdout) }}"
  when: 
    - not(alres | skipped)
    - alres | succeeded
    - not ip_present