├── vars ├── main.yml └── RedHat.yml ├── handlers └── main.yml ├── ansible.cfg ├── defaults └── main.yml ├── templates ├── bond_slave.j2 ├── bridge_port.j2 ├── bridge.j2 ├── ethernet.j2 └── bond.j2 ├── meta └── main.yml ├── README.md ├── tasks └── main.yml └── test.yml /vars/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # vars file for ansible-role-network 3 | -------------------------------------------------------------------------------- /handlers/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # handlers file for ansible-role-network 3 | -------------------------------------------------------------------------------- /vars/RedHat.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # vars file for RedHat 3 | 4 | network_config_path: "/etc/sysconfig/network-scripts" 5 | -------------------------------------------------------------------------------- /ansible.cfg: -------------------------------------------------------------------------------- 1 | [defaults] 2 | roles_path = ../ 3 | host_key_checking = False 4 | ansible_managed = Ansible managed: modified on %Y-%m-%d %H:%M:%S by {uid} on {host} 5 | -------------------------------------------------------------------------------- /defaults/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # defaults file for ansible-role-network 3 | 4 | network_ethernet_interfaces: [] 5 | network_bridge_interfaces: [] 6 | network_bond_interfaces: [] 7 | -------------------------------------------------------------------------------- /templates/bond_slave.j2: -------------------------------------------------------------------------------- 1 | # {{ ansible_managed }} 2 | 3 | DEVICE={{ item.1 }} 4 | BOOTPROTO=none 5 | MASTER={{ item.0.device }} 6 | ONBOOT=yes 7 | SLAVE=yes 8 | USERCTL=no 9 | NM_CONTROLLED=no 10 | -------------------------------------------------------------------------------- /meta/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | galaxy_info: 3 | author: z 4 | description: Ansible role for configure network on RedHat/CentOS. 5 | company: 6 | license: license (BSD, MIT) 7 | min_ansible_version: 1.6 8 | platforms: 9 | - name: EL 10 | versions: 11 | - 6 12 | - 7 13 | categories: 14 | - networking 15 | - system 16 | dependencies: [] 17 | -------------------------------------------------------------------------------- /templates/bridge_port.j2: -------------------------------------------------------------------------------- 1 | # {{ ansible_managed }} 2 | 3 | DEVICE={{ item.1 }} 4 | TYPE=Ethernet 5 | BOOTPROTO=none 6 | ONBOOT=yes 7 | USERCTL=no 8 | NM_CONTROLLED=no 9 | {% if item.0.vlan is defined %} 10 | VLAN=yes 11 | {% endif %} 12 | BRIDGE={{ item.0.device }} 13 | {% if item.0.bond_mode is defined %} 14 | BONDING_OPTS="mode={{ item.0.bond_mode }} miimon={{ item.0.bond_miimon|default(100) }}" 15 | {% endif %} 16 | -------------------------------------------------------------------------------- /templates/bridge.j2: -------------------------------------------------------------------------------- 1 | # {{ ansible_managed }} 2 | 3 | {% if item.bootproto == 'static' %} 4 | DEVICE={{ item.device }} 5 | TYPE=Bridge 6 | ONBOOT=yes 7 | BOOTPROTO=none 8 | USERCTL=no 9 | NM_CONTROLLED=no 10 | {% if item.stp is defined %} 11 | STP=yes 12 | DELAY=5 13 | {% endif %} 14 | 15 | {% if item.address is defined %} 16 | IPADDR={{ item.address }} 17 | NETMASK={{ item.netmask | default("255.255.255.0") }} 18 | {% endif %} 19 | {% if item.gateway is defined %} 20 | GATEWAY={{ item.gateway }} 21 | {% endif %} 22 | {% endif %} 23 | 24 | {% if item.bootproto == 'dhcp' %} 25 | DEVICE={{ item.device }} 26 | TYPE=Bridge 27 | ONBOOT=yes 28 | BOOTPROTO=dhcp 29 | USERCTL=no 30 | NM_CONTROLLED=no 31 | {% if item.stp is defined %} 32 | STP=yes 33 | DELAY=5 34 | {% endif %} 35 | {% endif %} 36 | -------------------------------------------------------------------------------- /templates/ethernet.j2: -------------------------------------------------------------------------------- 1 | # {{ ansible_managed }} 2 | 3 | {% if item.bootproto == 'static' %} 4 | NAME={{ item.device }} 5 | DEVICE={{ item.device }} 6 | TYPE=Ethernet 7 | ONBOOT=yes 8 | BOOTPROTO=none 9 | USERCTL=no 10 | NM_CONTROLLED=no 11 | {% if item.vlan is defined %} 12 | VLAN=yes 13 | {% endif %} 14 | {% if item.bridge is defined %} 15 | BRIDGE={{ item.bridge }} 16 | {% endif %} 17 | 18 | {% if item.address is defined %} 19 | IPADDR={{ item.address }} 20 | NETMASK={{ item.netmask | default("255.255.255.0") }} 21 | {% endif %} 22 | {% if item.gateway is defined %} 23 | GATEWAY={{ item.gateway }} 24 | {% endif %} 25 | {% endif %} 26 | 27 | {% if item.bootproto == 'dhcp' %} 28 | NAME={{ item.device }} 29 | DEVICE={{ item.device }} 30 | TYPE=Ethernet 31 | ONBOOT=yes 32 | BOOTPROTO=dhcp 33 | USERCTL=no 34 | NM_CONTROLLED=no 35 | {% if item.vlan is defined %} 36 | VLAN=yes 37 | {% endif %} 38 | {% if item.bridge is defined %} 39 | BRIDGE={{ item.bridge }} 40 | {% endif %} 41 | {% endif %} 42 | -------------------------------------------------------------------------------- /templates/bond.j2: -------------------------------------------------------------------------------- 1 | # {{ ansible_managed }} 2 | 3 | {% if item.bootproto == 'static' %} 4 | DEVICE={{ item.device }} 5 | TYPE=Ethernet 6 | ONBOOT=yes 7 | BOOTPROTO=none 8 | USERCTL=no 9 | NM_CONTROLLED=no 10 | {% if item.vlan is defined %} 11 | VLAN=yes 12 | {% endif %} 13 | {% if item.bridge is defined %} 14 | BRIDGE={{ item.bridge }} 15 | {% endif %} 16 | 17 | {% if item.address is defined %} 18 | IPADDR={{ item.address }} 19 | NETMASK={{ item.netmask | default("255.255.255.0") }} 20 | {% endif %} 21 | {% if item.gateway is defined %} 22 | GATEWAY={{ item.gateway }} 23 | {% endif %} 24 | BONDING_OPTS="mode={{ item.bond_mode }} miimon={{ item.bond_miimon|default(100) }}" 25 | {% endif %} 26 | 27 | {% if item.bootproto == 'dhcp' %} 28 | DEVICE={{ item.device }} 29 | TYPE=Ethernet 30 | ONBOOT=yes 31 | BOOTPROTO=dhcp 32 | USERCTL=no 33 | NM_CONTROLLED=no 34 | {% if item.vlan is defined %} 35 | VLAN=yes 36 | {% endif %} 37 | {% if item.bridge is defined %} 38 | BRIDGE={{ item.bridge }} 39 | {% endif %} 40 | BONDING_OPTS="mode={{ item.bond_mode }} miimon={{ item.bond_miimon|default(100) }}" 41 | {% endif %} 42 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Ansible Role: network 2 | 3 | This roles enables users to configure various network components in their servers. The role can be used to configure 4 | 5 | - Ethernet Interfaces 6 | 7 | - Bridge Interfaces 8 | 9 | - Bonding interfaces 10 | 11 | 12 | ## Requirements 13 | 14 | none 15 | 16 | ## Role Variables 17 | 18 | 19 | `defaults/main.yml:` 20 | 21 | ``` 22 | network_ethernet_interfaces: [] #The list of ethernet interfaces to be added to the sytem 23 | network_bond_interfaces: [] #The list of bond interfaces to be added to the sytem 24 | ``` 25 | 26 | Note: The values for the list are listed in the `test.yml`. 27 | 28 | ## Examples 29 | 30 | All the above examples show how to configure a single host, The below example shows how to define your network configurations 31 | for all your machines. 32 | 33 | - Assuming your Inventory looks as follows: 34 | 35 | ``` 36 | /etc/ansible/hosts 37 | 38 | [servers] 39 | servers01 40 | servers02 41 | 42 | ``` 43 | 44 | - Describe your network configuration for each host in hostvars 45 | 46 | ``` 47 | host_vars/servers01 48 | ------------------- 49 | 50 | 51 | network_ether_interfaces: 52 | - device: eth1 53 | bootproto: static 54 | address: 192.168.10.18 55 | netmask: 255.255.255.0 56 | gateway: 192.168.10.1 57 | 58 | network_bond_interfaces: 59 | - device: bond0 60 | bootproto: dhcp 61 | bond_mode: 4 62 | bond_miimon: 100 63 | bond_slaves: [eth2, eth3] 64 | 65 | 66 | host_vars/servers02 67 | ------------------- 68 | 69 | network_ether_interfaces: 70 | - device: eth0 71 | bootproto: static 72 | address: 192.168.10.19 73 | netmask: 255.255.255.0 74 | gateway: 192.168.10.1 75 | 76 | ``` 77 | 78 | ## Example Playbook 79 | 80 | see `test.yml` 81 | 82 | ## Author Information 83 | 84 | z 85 | -------------------------------------------------------------------------------- /tasks/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # tasks file for ansible-role-network 3 | 4 | # Include variables and define needed variables. 5 | - name: Include OS-specific variables. 6 | include_vars: "{{ ansible_os_family }}.yml" 7 | 8 | # Ethernet 9 | - name: Create the network configuration file for ethernet devices. 10 | template: src=ethernet.j2 dest={{ network_config_path }}/ifcfg-{{ item.device }} 11 | with_items: "{{ network_ethernet_interfaces }}" 12 | when: network_ethernet_interfaces is defined 13 | register: ethernet_result 14 | 15 | # Bridge 16 | - name: Create the network configuration file for bridge devices. 17 | template: src=bridge.j2 dest={{ network_config_path }}/ifcfg-{{ item.device }} 18 | with_items: "{{ network_bridge_interfaces }}" 19 | when: network_bridge_interfaces is defined 20 | register: bridge_result 21 | 22 | - name: Create the network configuration file for port on the bridge devices. 23 | template: src=bridge_port.j2 dest={{ network_config_path }}/ifcfg-{{ item.1 }} 24 | with_subelements: 25 | - "{{ network_bridge_interfaces }}" 26 | - ports 27 | when: network_bridge_interfaces is defined 28 | 29 | # Bonding 30 | - name: Create the network configuration file for bond devices. 31 | template: src=bond.j2 dest={{ network_config_path }}/ifcfg-{{ item.device }} 32 | with_items: "{{ network_bond_interfaces }}" 33 | when: network_bond_interfaces is defined 34 | register: bond_result 35 | 36 | - name: Make sure the bonding module is loaded 37 | modprobe: name=bonding state=present 38 | when: bond_result|changed 39 | 40 | - name: Create the network configuration file for slave in the bond devices. 41 | template: src=bond_slave.j2 dest={{ network_config_path }}/ifcfg-{{ item.1 }} 42 | with_subelements: 43 | - "{{ network_bond_interfaces }}" 44 | - bond_slaves 45 | when: network_bond_interfaces is defined 46 | 47 | # Restart Network 48 | - name: Restart Networking. 49 | service: name=network state=restarted 50 | -------------------------------------------------------------------------------- /test.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # test files for ansible-role-network 3 | 4 | ## Configure eth1 and eth2 of a host with static ip and a dhcp ip. 5 | 6 | - hosts: servers01 7 | roles: 8 | - role: ansible-role-network 9 | network_ethernet_interfaces: 10 | - device: eth0 11 | bootproto: static 12 | address: "{{ admin_ip }}" 13 | netmask: 255.255.255.0 14 | gateway: "{{ admin_ip_gateway }}" 15 | - device: eth1 16 | bootproto: dhcp 17 | 18 | ## Configure a bridge interface with multiple nics added to the bridge. 19 | 20 | - hosts: servers01 21 | roles: 22 | - role: ansible-role-network 23 | network_bridge_interfaces: 24 | - device: br1 25 | address: 192.168.10.10 26 | netmask: 255.255.255.0 27 | gateway: 192.168.10.254 28 | bootproto: static 29 | stp: "on" 30 | vlan: "yes" 31 | ports: [eth2] 32 | 33 | ## Configure a bond interface with an Active-Backup slave configuration. 34 | 35 | - hosts: servers01 36 | roles: 37 | - role: ansible-role-network 38 | network_bond_interfaces: 39 | - device: bond0 40 | bootproto: static 41 | vlan: yes 42 | address: 192.168.10.10 43 | netmask: 255.255.255.0 44 | gateway: 192.168.10.254 45 | bond_mode: 1 46 | bond_miimon: 100 47 | bond_slaves: [eth3, eth4] 48 | 49 | ## Configure a bonding interface with 802.3ad as the bonding mode and address is got via dhcp 50 | 51 | - hosts: servers01 52 | roles: 53 | - role: ansible-role-network 54 | network_bond_interfaces: 55 | - device: bond1 56 | bootproto: dhcp 57 | vlan: yes 58 | bond_mode: 4 59 | bond_miimon: 100 60 | bond_slaves: [eth5, eth6] 61 | 62 | ## Configure a bond interface with an Active-Backup slave configuration and bridge to br2. 63 | 64 | - hosts: servers01 65 | roles: 66 | - role: ansible-role-network 67 | network_bond_interfaces: 68 | - device: bond2 69 | bootproto: static 70 | vlan: yes 71 | bond_mode: 1 72 | bond_miimon: 100 73 | bond_slaves: [eth7, eth8] 74 | bridge: br2 75 | 76 | network_bridge_interfaces: 77 | - device: br2 78 | bootproto: static 79 | address: 192.168.20.20 80 | netmask: 255.255.255.0 81 | gateway: 192.168.20.254 82 | stp: "on" 83 | vlan: "yes" 84 | ports: [bond2] 85 | bond_mode: 1 86 | bond_miimon: 100 87 | 88 | ## Configure a bond with sub interface with an Active-Backup slave configuration. 89 | 90 | - hosts: servers01 91 | roles: 92 | - role: ansible-role-network 93 | network_bond_interfaces: 94 | - device: bond3 95 | bootproto: static 96 | vlan: yes 97 | bond_mode: 1 98 | bond_miimon: 100 99 | bond_slaves: [eth9, eth10] 100 | - device: bond3.1001 101 | bootproto: static 102 | vlan: yes 103 | address: 192.168.30.10 104 | netmask: 255.255.255.0 105 | gateway: 192.168.10.254 106 | bond_mode: 1 107 | bond_miimon: 100 108 | bond_slaves: [] 109 | --------------------------------------------------------------------------------