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

**URL:** <https://forum.ansible.com/t/restart-service-for-a-single-computer-using-a-loop-when-file-copied/28508>\
**Category:** Ansible Project\
**Created:** [September 5, 2018, 6:23pm UTC](https://forum.ansible.com/t/restart-service-for-a-single-computer-using-a-loop-when-file-copied/28508 "2018-09-05T18:23:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris7](https://avatars.discourse-cdn.com/v4/letter/c/439d5e/32.png) [@Chris7](https://forum.ansible.com/u/Chris7)\
**Post date:** [September 5, 2018, 6:23pm UTC](https://forum.ansible.com/t/restart-service-for-a-single-computer-using-a-loop-when-file-copied/28508/1 "2018-09-05T18:23:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [September 5, 2018, 7:48pm UTC](https://forum.ansible.com/t/restart-service-for-a-single-computer-using-a-loop-when-file-copied/28508/2 "2018-09-05T19:48:44Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![Chris7](https://avatars.discourse-cdn.com/v4/letter/c/439d5e/32.png) [@Chris7](https://forum.ansible.com/u/Chris7)\
**Post date:** [September 5, 2018, 8:12pm UTC](https://forum.ansible.com/t/restart-service-for-a-single-computer-using-a-loop-when-file-copied/28508/3 "2018-09-05T20:12:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Jonathan\_Lozada\_De\_L](https://avatars.discourse-cdn.com/v4/letter/j/eb8c5e/32.png) [@Jonathan\_Lozada\_De\_L](https://forum.ansible.com/u/Jonathan_Lozada_De_L)\
**Post date:** [September 5, 2018, 8:19pm UTC](https://forum.ansible.com/t/restart-service-for-a-single-computer-using-a-loop-when-file-copied/28508/4 "2018-09-05T20:19:29Z")

</div>

use handlers [https://docs.ansible.com/ansible/2.6/user\_guide/playbooks\_intro.html#handlers-running-operations-on-change](https://docs.ansible.com/ansible/2.6/user_guide/playbooks_intro.html#handlers-running-operations-on-change)

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [September 5, 2018, 8:32pm UTC](https://forum.ansible.com/t/restart-service-for-a-single-computer-using-a-loop-when-file-copied/28508/5 "2018-09-05T20:32:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Chris7](https://avatars.discourse-cdn.com/v4/letter/c/439d5e/32.png) [@Chris7](https://forum.ansible.com/u/Chris7)\
**Post date:** [September 5, 2018, 9:32pm UTC](https://forum.ansible.com/t/restart-service-for-a-single-computer-using-a-loop-when-file-copied/28508/6 "2018-09-05T21:32:55Z")

</div>

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.
