Browse Source

networkd-to-ifconfig: add variables for configuration by variables

pull/100/head
6 changed files with 79 additions and 21 deletions
  1. +1
    -0
      CHANGELOG.md
  2. +9
    -0
      networkd-to-ifconfig/defaults/main.yml
  3. +30
    -18
      networkd-to-ifconfig/tasks/main.yml
  4. +13
    -0
      networkd-to-ifconfig/tasks/set_facts_from_ansible.yml
  5. +18
    -0
      networkd-to-ifconfig/tasks/set_facts_from_systemd.yml
  6. +8
    -3
      networkd-to-ifconfig/templates/interfaces.j2

+ 1
- 0
CHANGELOG.md View File

@@ -35,6 +35,7 @@ The **patch** part changes incrementally at each release.
* mysql-oracle: backport tasks from mysql role
* mysql: activate binary logs by specifying log_bin path
* mysql: specify a custom server_id
* networkd-to-ifconfig: add variables for configuration by variables
* packweb-apache: Deploy opcache.php to give some insights on PHP's opcache status
* php: variable to install the mysqlnd module instead of the default mysql module
* postgresql : variable to install PostGIS (default: `False`)


+ 9
- 0
networkd-to-ifconfig/defaults/main.yml View File

@@ -0,0 +1,9 @@
---

force_update_eni_file: False
eni_interface_name: Null
eni_ipv4_address: Null
eni_ipv4_gateway: Null
eni_ipv6_address: Null
eni_ipv6_gateway: Null
ipv6_gateway_mask: "xxxx.xxxx.xxxx.xxFF:FF:FF:FF:FF"

+ 30
- 18
networkd-to-ifconfig/tasks/main.yml View File

@@ -7,26 +7,38 @@

- debug:
msg: A /etc/network/interfaces file already exists, nothing is done.
when: interfaces_file.stat.exists
when: interfaces_file.stat.exists and not force_update_eni_file

- block:
- name: "Parse addresses"
shell: "grep Address= /etc/systemd/network/50-default.network | cut -d'=' -f2"
register: network_address_grep
check_mode: no
- name: "Parse gateways"
shell: "grep Gateway= /etc/systemd/network/50-default.network | cut -d'=' -f2"
register: network_gateway_grep
check_mode: no

- name: Prepare variables
- name: "Look for systemd network config"
stat:
path: /etc/systemd/network/50-default.network
register: systemd_network_file

- name: Set interface name
set_fact:
eni_interface_name: "{{ ansible_default_ipv4.interface }}"
eni_ipv4_address: "{{ network_address_grep.stdout_lines | ipv4 | first }}"
eni_ipv4_gateway: "{{ network_gateway_grep.stdout_lines | ipv4 | first }}"
eni_ipv6_address: "{{ network_address_grep.stdout_lines | ipv6 | first }}"
eni_ipv6_gateway: "{{ network_gateway_grep.stdout_lines | ipv6 | first }}"

- include: set_facts_from_systemd.yml
when: systemd_network_file.stat.exists

- include: set_facts_from_ansible.yml
when: not systemd_network_file.stat.exists

- name: Check config (IPv4)
assert:
that:
- eni_ipv4_address | ipv4
- eni_ipv4_gateway | ipv4
msg: "IPv4 configuration is invalid"

- name: Check config (IPV6)
assert:
that:
- eni_ipv6_address | ipv6
- eni_ipv6_gateway | ipv6
msg: "IPv6 configuration is invalid"
when: eni_ipv6_address or eni_ipv6_gateway

- name: "A new /etc/network/interfaces is generated"
template:
@@ -48,7 +60,7 @@
enabled: True
state: restarted
ignore_errors: True
- debug:
msg: You should verify your configuration, then reboot the server.
when: not interfaces_file.stat.exists
when: force_update_eni_file or not interfaces_file.stat.exists

+ 13
- 0
networkd-to-ifconfig/tasks/set_facts_from_ansible.yml View File

@@ -0,0 +1,13 @@
---

- name: Prepare variables (IPv4)
set_fact:
eni_ipv4_address: "{{ ansible_default_ipv4.address | ipv4 }}"
eni_ipv4_gateway: "{{ ansible_default_ipv4.gateway | ipv4 }}"
when: ansible_default_ipv4

- name: Prepare variables (IPv6)
set_fact:
eni_ipv6_address: "{{ ansible_default_ipv6.address | ipv6 | first }}"
eni_ipv6_gateway: "{{ ansible_default_ipv6.gateway | ipv6 | first }}"
when: ansible_default_ipv6

+ 18
- 0
networkd-to-ifconfig/tasks/set_facts_from_systemd.yml View File

@@ -0,0 +1,18 @@
---

- name: "Parse addresses"
shell: "grep Address= /etc/systemd/network/50-default.network | cut -d'=' -f2"
register: network_address_grep
check_mode: no

- name: "Parse gateways"
shell: "grep Gateway= /etc/systemd/network/50-default.network | cut -d'=' -f2"
register: network_gateway_grep
check_mode: no

- name: Prepare variables
set_fact:
eni_ipv4_address: "{{ network_address_grep.stdout_lines | ipv4 | first }}"
eni_ipv4_gateway: "{{ network_gateway_grep.stdout_lines | ipv4 | first }}"
eni_ipv6_address: "{{ network_address_grep.stdout_lines | ipv6 | first }}"
eni_ipv6_gateway: "{{ network_gateway_grep.stdout_lines | ipv6 | first }}"

+ 8
- 3
networkd-to-ifconfig/templates/interfaces.j2 View File

@@ -10,8 +10,13 @@ iface {{ eni_interface_name }} inet static
address {{ eni_ipv4_address }}
gateway {{ eni_ipv4_gateway }}

{% if eni_ipv6_address and eni_ipv6_gateway %}
iface {{ eni_interface_name }} inet6 static
address {{ eni_ipv6_address }}
gateway {{ eni_ipv6_gateway }}
post-up /sbin/ip -6 route add {{ eni_ipv6_gateway }} dev {{ eni_interface_name }}
post-up /sbin/ip -6 route add default via IPV6
netmask 128

post-up /sbin/ip -f inet6 route add {{ eni_ipv6_gateway }} dev {{ eni_interface_name }}
post-up /sbin/ip -f inet6 route add default via {{ eni_ipv6_address }}
pre-down /sbin/ip -f inet6 route del {{ eni_ipv6_gateway }} dev {{ eni_interface_name }}
pre-down /sbin/ip -f inet6 route del default via {{ eni_ipv6_gateway }}
{% endif %}

Loading…
Cancel
Save