Chapter 10: Automating Linux Administration Tasks
Exercise: Managing network configuration
servera has a second network interface, eth1, that is not configured yet. You will give it a static address with the network system role, keeping the setting in group_vars, and then read the result back from the facts.
The project ~/system-network has an ansible.cfg, an inventory with servera in webservers, a playbook get-eth1.yml that shows the facts for eth1, and the system roles collection archive.
Managing network configuration
- Configure a network interface with the network system role.
- Read network facts.
Prerequisites: Complete control-node setup and the preceding lessons in this chapter. Run commands as student on workstation unless a step names another machine.
From the project directory, run the relevant home-lab check. For an exercise with several checkpoints, grade each state before changing or removing it:
lab grade system-networkThe grader reads project files and host state. A passing report covers the listed checks; also perform the task’s independent connection, repeat-run, and reboot checks where requested.
Change the dummy interface address to another documentation address, then verify the old address is gone.
Record the published result first. The grader checks the original requirements, so a changed name or value may intentionally fail those checks.
Add a persistent secondary connection on servera with playbook.yml, without interrupting its management connection.
- Use the network system role, with the connection values stored in
group_vars/webservers/network.yml. - In the classroom, use
eth1at172.25.250.30/24. At home, use the dummy connection namedeth1at192.0.2.30/24. get-eth1.ymlmust report the resulting interface facts.- Verify the address, active connection, and continued SSH access, including after an explicit reboot.
The home-lab VMs have one network card, which Ansible uses. Instead of a second card, eth1 is a dummy interface, created by the same role from type: dummy, with an address from 192.0.2.0/24, a range reserved for documentation. Everything else is the same.
Install the collection
[student@workstation ~]$ cd ~/system-network
[student@workstation system-network]$ ansible-galaxy collection install \
> ./redhat-rhel_system_roles-1.120.5.tar.gz -p collections
...output omitted...
redhat.rhel_system_roles:1.120.5 was installed successfullyWrite the playbook
---
- name: NIC Configuration
hosts: webservers
roles:
- redhat.rhel_system_roles.networkFind the variable in the documentation
The role's README.md has a section for each kind of setting. Look for "Setting the IP configuration":
[student@workstation system-network]$ less \
> collections/ansible_collections/redhat/rhel_system_roles/roles/network/README.mdIts example shows a network_connections entry with an ip dictionary, whose address is a list of addresses with their prefix.
Put the setting in group_vars
[student@workstation system-network]$ mkdir -pv group_vars/webservers
mkdir: created directory 'group_vars'
mkdir: created directory 'group_vars/webservers'---
network_connections:
- name: eth1
type: ethernet
ip:
address:
- 172.25.250.30/24---
network_connections:
- name: eth1
type: dummy
interface_name: eth1
ip:
address:
- 192.0.2.30/24Run it
[student@workstation system-network]$ ansible-navigator run -m stdout playbook.yml
...output omitted...
TASK [redhat.rhel_system_roles.network : Print network provider] ***************
ok: [servera.lab.example.com] => {
"msg": "Using network provider: nm"
}
...output omitted...
TASK [redhat.rhel_system_roles.network : Configure networking connection profiles] ***
changed: [servera.lab.example.com]
...output omitted...
ok: [servera.lab.example.com] => {
"__network_connections_result.stderr_lines": [
"[002] <info> #0, state:None persistent_state:present, 'eth1': add connection eth1, 08b92940-ef4a-49ad-a309-e4f05eae64af"
]
}
...output omitted...
TASK [redhat.rhel_system_roles.network : Re-test connectivity] *****************
ok: [servera.lab.example.com]
...output omitted...The role uses NetworkManager (nm), added a connection profile called eth1, and finally checked that it can still reach the host.
Read it back from the facts
---
- name: Obtain network info for webservers
hosts: webservers
tasks:
- name: Display eth1 info
ansible.builtin.debug:
var: ansible_facts['eth1']['ipv4'][student@workstation system-network]$ ansible-navigator run -m stdout get-eth1.yml
...output omitted...
TASK [Display eth1 info] *******************************************************
ok: [servera.lab.example.com] => {
"ansible_facts['eth1']['ipv4']": {
"address": "192.0.2.30",
"broadcast": "192.0.2.255",
"netmask": "255.255.255.0",
"network": "192.0.2.0",
"prefix": "24"
}
}
...output omitted...In a classroom, the address is 172.25.250.30.
Finish
Check your work with lab grade system-network, then put the exercise away with lab finish system-network.