3

I have an Ansible playbook that I use to configure new Linux VMs. I was recently building a new VM with Ubuntu 22.04. The playbook will write some configuration files related to networking and whatnot, and then the final step of the process is to use Ansibles ansible.builtin.package to install all package updates.

- name: "Install updates" become: true ansible.builtin.package: upgrade: "dist" register: res_pkg_updates notify: "reboot system" tags: [ never, updates ] 

My VM was created from a template I made a few months ago, so the Ubuntu OS had a few packages out of date, which isn't unexpected. The problem is that one of the packages must support or provide the networking functionality. So when the package modules starts the VM on the path of installing updates, the networking daemon is restarted, and the VM gets the new IP that was configured earlier in my playbook. This causes the Ansible task to hang, waiting for a reconnection to a machine that is now at a different IP.


I want to know how to configure my ansible.builtin.package task to install package updates but not restart any services, especially networking.

2
  • On Ubuntu, use the module apt and set policy_rc_d=101. (upgrade: "dist" should perform a distribution upgrade! In this case, you have to reboot the system. But, this is not what you describe. You say: "had a few packages out of date"). Anyway, take a look at the example how to reboot the system on your own if really needed. Commented Jan 26, 2024 at 3:31
  • 1
    This sounds like an XY problem. You actually need the host to not end up on a different IP, which can be solved by either configuring a static address on the host itself, or by setting up the DHCP server it’s getting it’s IP address from to always give it the same IP. Commented Jan 26, 2024 at 12:32

2 Answers 2

3

Separate the IP address change from the package transaction and the rest of the play.

First update packages and do whatever other things.

As the last things in this play, update the IP address for this host, but do not bring the interface up on the new IP yet. Maybe by changing a config file. Schedule a reboot of this host a little in the future, such as with ansible.posix.at module. And finally, update DNS so Ansible inventory is aware of the IP change. End of play. Later the host reboots itself, completing the software update and IP change.

A future play can start with a ansible.builtin.wait_for_connection as a convenient way to retry if not up and reachable yet.

ansible.builtin.reboot module in theory can do the reboot, wait for the host to come back, and continue. But it has no good way of rebooting one IP address and come back as another.

3

A maybe different approach I would use a static ip for the VM, instead of dynamically let it get assigned. Not restarting the network daemon might cause other problems when running the playbook

Or you can do this

- name: Update package cache and install updates ansible.builtin.package: name: "*" state: latest update_cache: yes force: no become: yes 

force: no prevents the package manager from forcing updates or restarting services

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.