Skip to content
This repository was archived by the owner on Oct 27, 2025. It is now read-only.

Commit 109fd60

Browse files
authored
Merge pull request #3 from MartinVerges/master
pull from master
2 parents ca1d717 + b493ca3 commit 109fd60

File tree

8 files changed

+96
-5
lines changed

8 files changed

+96
-5
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,30 @@ and routed updated.
230230
roles:
231231
- role: network
232232

233+
9) This role can also optionally add network interfaces to firewalld zones. The
234+
core firewalld module (http://docs.ansible.com/ansible/latest/firewalld_module.html)
235+
can perform the same function, so if you make use of both modules then your
236+
playbooks may not be idempotent. Consider this case, where only the firewalld
237+
module is used:
238+
239+
* network_interface role runs; with no firewalld_zone host var set then any
240+
ZONE line will be removed from ifcfg-*
241+
* firewalld module runs; adds a ZONE line to ifcfg-*
242+
* On the next playbook run, the network_interface role runs and removes the
243+
ZONE line again, and so the cycle repeats.
244+
245+
In order for this role to manage firewalld zones, the system must be running a
246+
RHEL based distribution, and using NetworkManager to manage the network
247+
interfaces. If those criteria are met, the following example shows how to add
248+
the eth0 interface to the public firewalld zone:
249+
250+
- device: eth0
251+
bootproto: static
252+
address: 192.168.10.18
253+
netmask: 255.255.255.0
254+
gateway: 192.168.10.1
255+
firewalld_zone: public
256+
233257
Note: Ansible needs network connectivity throughout the playbook process, you
234258
may need to have a control interface that you do *not* modify using this
235259
method while changeing IP Addresses so that Ansible has a stable connection

meta/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
galaxy_info:
3-
author: "Benno Joy, Martin Verges"
3+
author: "Benno Joy, Martin Verges, Luke Short"
44
company: "AnsibleWorks, First Colo GmbH"
55
license: "Simplified BSD License"
66
min_ansible_version: 1.9

tasks/main.yml

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,52 @@
5858
- include: restartscript.yml
5959
when: network_allow_service_restart and ansible_os_family == 'Debian'
6060

61-
- name: Restart on RedHat Systems
62-
service: name=network state=restarted
61+
- name: Checking if the "network" service is enabled
62+
service: name=network enabled=yes
63+
check_mode: yes
64+
register: network_service
65+
ignore_errors: true
66+
when: network_allow_service_restart and ansible_os_family == 'RedHat'
67+
68+
- set_fact:
69+
network_service_enabled: "{{ not network_service.failed
70+
and not network_service.changed }}"
6371
when: network_allow_service_restart and ansible_os_family == 'RedHat'
72+
73+
- name: Checking if the "NetworkManager" service is enabled
74+
service: name=NetworkManager enabled=yes
75+
check_mode: yes
76+
register: NetworkManager_service
77+
ignore_errors: true
78+
when: network_allow_service_restart and ansible_os_family == 'RedHat'
79+
80+
- set_fact:
81+
NetworkManager_service_enabled: "{{ not NetworkManager_service.failed
82+
and not NetworkManager_service.changed }}"
83+
when: network_allow_service_restart and ansible_os_family == 'RedHat'
84+
85+
- name: Restart the "network" service on Red Hat systems
86+
service: name=network state=restarted
87+
when: >
88+
(network_allow_service_restart
89+
and ansible_os_family == 'RedHat'
90+
and network_service_enabled)
91+
and (ether_result | changed
92+
or bond_port_result | changed
93+
or bond_result | changed
94+
or vlan_result | changed
95+
or bridge_result | changed
96+
or bridge_port_result | changed)
97+
98+
- name: Restart the "NetworkManager" service on Red Hat systems
99+
service: name=network state=restarted
100+
when: >
101+
(network_allow_service_restart
102+
and ansible_os_family == 'RedHat'
103+
and NetworkManager_service_enabled) and
104+
(ether_result | changed or
105+
bond_port_result | changed or
106+
bond_result | changed or
107+
vlan_result | changed or
108+
bridge_result | changed or
109+
bridge_port_result | changed)

tasks/redhat.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,11 @@
2424
template: src=route_{{ ansible_os_family }}.j2 dest={{ net_path }}/route-{{ item.device }}
2525
with_items: "{{ network_bridge_interfaces }}"
2626
when: network_bridge_interfaces is defined and item.route is defined
27+
28+
- name: Cleanup gateway dev that does not set to the one we want
29+
lineinfile: dest=/etc/sysconfig/network regexp="^GATEWAYDEV=(?!{{ gateway_dev }})" state=absent
30+
when: gateway_dev is defined
31+
32+
- name: Explicitly set gateway dev
33+
lineinfile: dest=/etc/sysconfig/network line="GATEWAYDEV={{ gateway_dev }}"
34+
when: gateway_dev is defined

templates/Debian_resolvconf.j2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
dns-nameservers {{ item.dns_nameservers|join(' ') }}
33
{% endif %}
44
{% if item.dns_search is defined %}
5-
dns-search {{ item.dns-search }}
5+
dns-search {{ item.dns_search }}
66
{% endif %}
77
{% if item.dns_domain is defined %}
8-
dns-domain {{ item.dns-domain }}
8+
dns-domain {{ item.dns_domain }}
99
{% endif %}
1010

templates/bond_RedHat.j2

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ GATEWAY={{ item.gateway }}
1616
{% endif %}
1717
{% include "RedHat_bond_options.j2" %}
1818
{% endif %}
19+
1920
{% if item.dns_nameservers is defined %}
2021
{% for dns_nameserver in item.dns_nameservers %}
2122
DNS{{ loop.index }}={{ dns_nameserver }}
@@ -53,3 +54,7 @@ BONDING_MASTER={{ item.bonding_master }}
5354
{% if item.bridge is defined %}
5455
BRIDGE={{ item.bridge }}
5556
{% endif %}
57+
58+
{% if item.firewalld_zone is defined %}
59+
ZONE={{ item.firewalld_zone }}
60+
{% endif %}

templates/bridge_RedHat.j2

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,7 @@ DEFROUTE={{ item.defroute | bool | ternary("yes", "no") }}
6767
{% if item.mtu is defined %}
6868
MTU={{ item.mtu }}
6969
{% endif %}
70+
71+
{% if item.firewalld_zone is defined %}
72+
ZONE={{ item.firewalld_zone }}
73+
{% endif %}

templates/ethernet_RedHat.j2

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,7 @@ DEFROUTE={{ item.defroute | bool | ternary("yes", "no") }}
6262
{% if item.mtu is defined %}
6363
MTU={{ item.mtu }}
6464
{% endif %}
65+
66+
{% if item.firewalld_zone is defined %}
67+
ZONE={{ item.firewalld_zone }}
68+
{% endif %}

0 commit comments

Comments
 (0)