pyinfra: how to know when an operation causes a change

339 Views Asked by At

Sometimes, you need to run more commands if one command causes a change on the remote system. Good examples would be:

  • You update a systemd service file. If the file was actually changed, then you need to restart the service.
  • You update the configuration for a service (like say /etc/dhcp/dhcpd.conf). If that file was changed, you need to restart the service.

Is there a way to do this with files.put? Ideally you could write code like:

changed = files.put(src='files/dhcpd.conf', dest='/etc/dhcp/dhcpd.conf')
if changed:
    systemd.service(service='isc-dhcp-server', running=True, restarted=True)
1

There are 1 best solutions below

0
yggdrasil On

In pyinfra, every operation returns an object that has a changed property that does what you want:

dhcpconfig = file.put(…)
if dhcpconfig.changed:
    systemd.service(…)