Introduction

My existing Ansible playbooks for deploying new VMs into my GeekLab have kept clear of network configuration as that was either managed by DHCP or left as a manual task. I've now finaly taken the time to figure out a way to do this through Ansible-based automation more easily. This updates the Ubuntu VM deployer playbooks in Deployment - Ansible Playbooks and with an update to the associated bash script in Deployment - Bash scripts. This was a satisfying exercise as it proved the extensibility concept I'd build into these and appears to be working quite nicely.

I'm still keeping my master spreadsheet of IP address as that's that's easier to read than trawling though the Ansible Inventory and also keeps a separation between intended design and operation implemenation.

Why Static IP Addresses?

Managing addresses is one fo the big challenges of operating an Internet Protocol (IP) network. In an ideal situation you let most hosts sort themselves out using the Dynamic Address Resolution Protocol (DHCP) and things just work - providing all the bits play together nicely - and you don't have to worry too much. But even on the simplest network you'll need at least one static IP addresses just for the default gateway which is typically also used for a DHCP server. And unsurprisingly my GeekLab network is not the simplest.

I have the following hosts on each lab network that each need a static IP address allocated:

  • Default gateway
  • Samba 4 Domain Controller & DHCP Server
  • Additional Samba 4 Domain Controller & DHCP Server

All of the other hosts use DHCP, either through a reservation so they get the same address each time or from a general pool where they get the next available address each time they connect. I also have some applications subnets where all the hosts have static IPs but that's through choice.

For each of these I've been editing the the network config after deployment, but it should be possible to do this with my deployment automation. The driver for doing this is the upcoming replacement of the Samba DC's with new instances running on Ubuntu 24.04 LTS as the current ones are based on Ubuntu 20.04 LTS which will be going end-of-life in a few months. While I could do the rebuilds in a semi-automated way I figured it would be an interesting exercise to see how much I could get my deployer to do.

Note: At the moment I'm concerned entirely with "ye olde traditional" IP addresses, also known as IP version 4 or IPv4 for short. There is also a IP version 6 (a.k.a. IPv6) that while similar in some respects does this very differently when it comes to addressing. I've been playing with IPv6 for some time but for now I'm going to ignore it.

Assigning Static IP using Ansible

Using DHCP to automatically assign addresses to hosts is nice and easy once you've got your DHCP server configured. Even allocating a static IP to a host during deployment isn't a big deal as it's just an additional configuration file with some parameters in it. But there's a gotcha and it's name is Domain Name Server or DNS. Yes, I know, it's always bl**dy DNS...

While all of the configuration parameters need to be applied to each host individually, some are specific to the host and others are common to all hosts on that particular network. This means I can put a lot of the configuration info into the groups that correspond to each network so they will be inherited for the hosts assigned to them, which saves a bit of typing.

DHCP servers allocate IP addresses to hosts. DNS provides a mapping from hostname to IP address. The two are separate services, but I have scripted my DHCP servers (which use DNSmasq) to update my DNS servers (which are Samba 4 in DC mode) with the address allocated to each host so a request for dvu-u24test1.lab21.example.com will just work with me having to update anything manually.

Using static addresses managed via an Ansible Inventory means I now need to manually update DNS after each host is built. Which kinda defeats the point of automation so I'm also going to need a way to update the DNS servers from my deployer.

So the process becomes:

  1. Add a static address to the host in the Ansible Inventory while the host is being defined
  2. Run a playbook to update DNS with the assigned address and target hostname during host deployment
  3. Run a task to apply the static address and related parameters to the host during deployment

The first step is just extra info to be added to the inventory, the other two take care of provisioning the static address during build. Should be easy huh?

Inventory Updates

Untill now I've been using DHCP or manual config for managing host addresses so everything has either been in the DHCP server config or a LibreOffice spreadsheet. For hosts that I want to deploy with static IPs I need to copy that info into the Ansible Inventory, which means introducing some new variables.

This is my first cut at doing this so I'm not claiming these are the best or most sensible, just that they work for a version 1. All the network parameters start with network_ and if they're going to be specific to IPv4 or IPv6 then I've put a 4 or 6 into the name too. I can get clever later if I wish.

 VariableScopePurpose
network_address4 Host IPv4 address to be assigned to the VM
network_gateway4 Network IPv4 address of the default gateway, or destination of the default route if you want to use the new thinking
network_address4_mask Network Subnet mask for the IPv4 network in CIDR notation without the leading /.
network_nameserver4_1 Network IPv4 address of the primary DNS host to use
network_nameserver4_2 Network IPv4 address of a secondary DNS host to use
network_search Network list of domain names to search if a request is for the short name only
network_type Host Flag to indicate if host(s) should use static IP addresses
network_dns_update Network Tool to use for updating DNS when a host is built with a static IP address
ad_domain_primary Network Target host to use for DNS updates.  This should ideally be replaced with a lookup from the group holding the DC hosts and picking one of them instead.
ad_domain Network The AD domain, which is also the DNS zone, where we want to add the allocated address to.

 

While these could all be specified at Host level too, most of them will apply to the entire network. An example inventory now looks something like this, with s4dc-2.test.example.com being a new host to be deployed with a static IP address.

--- # Ansible Inventory
# Test Networks
# 6 Feb 2025

test:
  vars:
    config_sambatool_dc: tvu-s4dc-1.test.example.com

    network_gateway4: 192.168.451.1
    network_ntp: 192.168.451.1
    network_address4_mask: 24
    network_nameserver4_1: 192.168.451.64
    network_nameserver4_2: 192.168.451.65
    network_dns_update: samba-tool
    network_search: test.example.com

  children:

    sambadc_test:
      vars:
        ad_domain: test.example.com
        ad_domain_primary: s4dc-1.test.example.com
        network_type: static

      hosts:

        s4dc-2.test.example.com:
          network_address4: 192.168.451.65

And yes, 192.168.451.65 is an invalid address, just in case anyone does an unthinking copy/paste. I've also stripped out the other parameters in the interests of clarity. I normally add parameters used for the apcupsd UPS daemon at network level, and there's also info on the deployment template and ESXi network name too.

Add Static Address to DNS

For hosts I'm going to deploy onto a network where a Samba 4 DC provides DNS services we can use an Ansible Playbook to perform this update. This needs to happen before the host reboots under it's real name so I'm going to trigger this at the very beginning of the deploy process during the currently unused infrastructure prepare phase to allow time for the update to propagate to all the domain controllers.

This actually proved to be the trickiest part to build since we need to trigger actions on a Samba host that's part of the domain which rules out using both the host we're building as it doesn't actually exist in reality at this stage, and also the Ansible Controller since that's not a member of any domain.

For hosts that are to be deployed into a domain that is not controlled by a Samba DC that's a different story so I'm leaving that as manual configuration with a warning message later in the build process. For example, I have some hosts that rely on the Vigor Router I use for internet activity to also provide internal DNS and while I suspect it's possible to remotely configure the Vigor I'm leaving that on the todo list for now.

New Playbook: ubuntu24-07.yaml

The trick to making this playbook work is although we set hosts: {{ target }} to be the new host we want to build in order to gain access the inventory variables we set gather_facts: no so that Ansible doesn't try to connect to it a the start of the playbook. Which is good as our target host doesn't exist yet.

There's only one task in this play and and we use a delegate_to: statement to connect to our chosen domain controller and then execute a samba-tool dns command to create our DNS record using the encrypted credentials held in the ansible vault vault-newhost.yaml.

--- # Ansible Playbook

# New Ubuntu 24.04 host setup
# Stage 0: Prepare landsscape
#
# Updated: 1 Feb 25
#
# This playbook triggers a DNS update on the DC if the host is set to static and
# DNS update method is set to samba-tool. It might also be better as a completely stand-alone playbook.
#
# This playbook runs in the name of the target but delegates all tasks to others since
# the target is not actually ready for connections just yet.
#

- name: Complete OVF deployment
  hosts: "{{ target }}"
  gather_facts: no

  vars_files:
   - vars/vault-newhost.yaml
   - vars/config-newhost.yaml

tasks:
- name: Update DNS using samba-tool on an existing DC
  when: network_type == "static" and network_dns_update == "samba-tool"
  delegate_to: "{{ ad_domain_primary }}"
  ignore_errors: true
  ansible.builtin.command:
    argv:
    - "samba-tool"
    - dns
    - add
    - "{{ ad_domain_primary }}"
    - "{{ ad_domain }}"
    - "{{ inventory_hostname_short }}"
    - A
    - "{{ network_address4 }}"
    - -U
    - "{{ config_ad_admin_user }}"
    - --password
    - "{{ config_ad_admin_cred }}"

# Password also not secure, need better method.

...

The task will only be executed if our target host has a network_type of static and network_dns_update is set to samba-tool. For other DNS servers I would create similar tasks with a different value of network_dns_update to allow them to execute instead.

Using a password on the command line is not as secure as it could be. It would probably better to try and use the expect module so it will prompt for the password but this requires pexpect to be installed on the target host and I've had some reliability issues using it on another playbook so I'm not entirely convinced right now. Another option is to use a local installation on samba-tool on the Ansible controller but then we need to open up additional firewall ports between the controller and the target for the DCE protocol which needs more investigation.

My earlier attemtps to make this work were executed later in the build process with Ansbile connected to the target host as usual. Interestingly, executing the Update DNS task with delegate_to caused Ansible to try and connect from the target host to the DC host, rather than directly from the Ansible Control to the DC host. This of course failed as the required key was not on the target host - and never will be.

Modify ubuntu24-deploy.sh

We also need to modify the deployer script ubuntu24-deploy.sh to call our new playbook as the first actions to perform. This will use the admin user and SSH key as we need to connect to the existing Samba 4 DC.

[...]

#######################################
# Main execution
#

RunPlaybookSSH ubuntu24-07.yaml $TARGET_ADMIN_SSH_USER $TARGET_ADMIN_SSH_KEY

DeployLock

[...]

 

Configure the static IPv4 Address on the Host

We need to add two bits of configuration to apply a static IP address to the host:

  1. A Netplan configuration file with the address and all the other settings that are normally supplied using DHCP
  2. An Ansible Task to apply the template

All the required settings are now in the inventory so we can easily use a j2 template to create the config.

Netplan Template

Ubuntu hosts use netplan to specify network settings which is then interpreted by the specified netplan backend into whatever detailed configuration is needed by other Linux components.

60-static.yaml

# Static address for Samba server etc
network:
   ethernets:
     ens192:
       dhcp4: false
       dhcp6: false
       addresses:
       - {{ network_address4 }}/{{ network_address4_mask }}
#     - [network_address6]
#     gatewayX replaced by routes to default via XXX
       routes:
       - to: default
         via: {{ network_gateway4 }}
#     - to: default_ipv6
#     via: "network_gateway6"
      nameservers:
         addresses:
         - {{ network_nameserver4_1 }}
#       - [network_nameserver6_1]
        search:
        - {{ network_search }}
   version: 2

Even though I'm not handing IPv6 right now I've left placeholders for future use when I'm ready to enable it again. Although to be honest I'll probably manually update exiting hosts individually rather than create a one-off playbook. IPv6 redeployment is a future thing.

Address Assignment Task Block

With the template defined the next step is to apply it by adding the following block to the ubuntu24-15.yaml playbook after the name: Rename host task.

# If setting static IP then all of this block needs to run
  - name: Set a static IPv4 address
  when: network_type == "static"
  block:

  - name: Deploy 60-static netplan config
  become: true
  ansible.builtin.template:
    src: 60-static.yaml.j2
    dest: /etc/netplan/60-static.yaml
    owner: root
    group: root
    mode: '0640'
    backup: yes
  notify:
  - Reboot host

# Do NOT do a netplan apply otherwise the address changes immediately. Which will be bad.

  - name: Requst DNS update
  when: network_dns_update != "samba-tool"
  ansible.builtin.pause:
    prompt: "Please add new VM to DNS before continuing otherwise the next play will probably fail. [Enter] when ready"

# End of "Set a static IPv4 addresss" block

This first checks if network_type is set to static and if so it deploys the Netplan 60-static.yaml file and triggers a reboot at the end of the playbook so it takes effect. If we did not automatically update DNS with the static IP we're assigning then we need to hold execution of the playbook at this point until we've manually DNS otherwise the next playbook will fail as it won't find the host. Unless you've done some other hackery on the control node but we won't speak of such deviousness here.

Result

Adding support for static IP addresses has been on my to-do list for a while. The immediate need was the upcoming rotation of the Samba 4 DC's which need to be rebuilt on Ubuntu 24.04 and they must have static IPs to keep both Samba and the DHCP server happy. I also have internal email and Nextcloud servers that also need rebuilding onto Ubuntu 24.04 and while they could use DHCP with reservations, as permanent application servers using static addresses is one less thing to go wrong.

I will go back and update the articles affected by this change at some point. I've already started deploying the new DCs using this enhancment and I'd rather get the new Samba DC deployer written up first.