restart service for a single computer using a loop when file copied

7 views
Skip to first unread message

Chris

unread,
Sep 5, 2018, 2:23:36 PM9/5/18
to Ansible Project
Good day,

I have a list that I loop over to copy files. I want to restart a service only if that file has been changed on the remote system. What is the best method to restart a service on only on a system if the file was copied to the system.

For instance:
defaults/main.yml

copy_files:
  - name: macsec
    all_systems_copy_src: "files/etc/modprobe.d/macsec.conf"
    all_systems_copy_dest: "/etc/modprobe.d/macsec.conf"
    all_systems_copy_owner: "0"
    all_systems_copy_group: "0"
    all_systems_copy_mode: "0644"

  - name: rsyslog
    all_systems_copy_src: "files/etc/rsyslog/syslog.conf"
    all_systems_copy_dest: "/etc/rsyslog.d/syslog.conf"
    all_systems_copy_owner: "0"
    all_systems_copy_group: "0"
    all_systems_copy_mode: "0644"

tasks/copy_files/main.yml

- name: "Copy files"
  copy:
    src: "{{ item.all_systems_copy_src }}"
    dest: "{{ item.all_systems_copy_dest }}"
    owner: "{{ item.all_systems_copy_owner }}"
    group: "{{ item.all_systems_copy_group }}"
    mode: "{{ item.all_systems_copy_mode }}"
  with_items:
    - "{{ copy_files }}"

If the system copies a new syslog file restart syslog.

I know there are handlers and notify, but I only want to restart on a specific system if it gets updated.

Chris

Kai Stian Olstad

unread,
Sep 5, 2018, 3:48:54 PM9/5/18
to ansible...@googlegroups.com
On 05.09.2018 20:23, Chris wrote:
> Good day,
>
> I have a list that I loop over to copy files. I want to restart a
> service
> only if that file has been changed on the remote system. What is the
> best
> method to restart a service on only on a system if the file was copied
> to
> the system.

Here you say system and system.

> If the system copies a new syslog file restart syslog.
>
> I know there are handlers and notify, but I only want to restart on a
> specific system if it gets updated.

And system again, so i guess this is system is the same host and notify
is perfect in that scenario.

--
Kai Stian Olstad

Chris

unread,
Sep 5, 2018, 4:12:29 PM9/5/18
to Ansible Project
Apologies if that was not clear.

On my ansible server I have files that I want to copy to remote servers. I only want to restart a service if a specific file was copied and not just any other file. My example shows two files, but I only want to restart the syslog service if the syslog file is copied. Additionally I don't want to restart the syslog service on all of the inventoried servers just the ones with the updated file.

I hope that makes my question more clear.

Chris

Jonathan Lozada De La Matta

unread,
Sep 5, 2018, 4:19:49 PM9/5/18
to ansible...@googlegroups.com

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/6d8870fb-3154-4965-93ff-b8300902a401%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--

Jonathan lozada de la matta

AUTOMATION PRACTICE



 

Kai Stian Olstad

unread,
Sep 5, 2018, 4:32:11 PM9/5/18
to ansible...@googlegroups.com
On 05.09.2018 22:12, Chris wrote:
> Apologies if that was not clear.
>
> On my ansible server I have files that I want to copy to remote
> servers. I
> only want to restart a service if a specific file was copied and not
> just
> any other file. My example shows two files, but I only want to restart
> the
> syslog service if the syslog file is copied. Additionally I don't want
> to
> restart the syslog service on all of the inventoried servers just the
> ones
> with the updated file.

That's exactly what notify/handlers does, it only notify if it's a
change.
So if you add a key to your list called notify then you can have notify:
'{{ item.notify | default(omit) }}' on the task.
So any entry in the list that don't have notify key will just omit
notify entirely.

--
Kai Stian Olstad

Chris

unread,
Sep 5, 2018, 5:32:55 PM9/5/18
to Ansible Project
Thank you, this is the answer I was searching for with my question.

I will add a notify key and then use a handler off of the key.
Reply all
Reply to author
Forward
0 new messages