Skip to content

Instantly share code, notes, and snippets.

@frenata
Last active April 1, 2022 20:09
Show Gist options
  • Save frenata/f91acd4c0c4dc463a937e6da7b6f629a to your computer and use it in GitHub Desktop.
Save frenata/f91acd4c0c4dc463a937e6da7b6f629a to your computer and use it in GitHub Desktop.
conditionally restart systemd unit files via ansible, two styles
tasks:
- name: specify a systemd unit file
become: true
copy:
src: foo.service
dest: /etc/systemd/system
notify: restart foo
- name: enable the systemd unit file
become: true
systemd:
name: foo
enabled: yes
daemon_reload: yes
state: started
handlers:
- name: restart foo
become: true
systemd:
name: foo
daemon_reload: yes
state: restarted
tasks:
- name: specify a systemd unit file
become: true
copy:
src: foo.service
dest: /etc/systemd/system
register: foo # record the results of this task
- name: enable the systemd unit file
become: true
systemd:
name: foo
enabled: yes
daemon_reload: yes
state: "{{'restarted' if foo.changed else 'started'}}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment