---
# tasks file for host
- name: Add the OS specific variables
include_vars: "{{ ansible_os_family }}.yml"
- name: Install /etc/hosts
template: >
src=hosts.j2
dest=/etc/hosts
owner=root
group=root
mode=0644
register: filechanged
tags: hosts
- name: Install /etc/hostname
template: >
src=hostname.j2
dest=/etc/hostname
owner=root
group=root
mode=0644
register: filechanged
tags: hostname
- name: Install /etc/network/interfaces
template: >
src=interfaces.j2
dest=/etc/network/interfaces
owner=root
group=root
mode=0644
register: filechanged
tags: interfaces
- name: Run hostname
command: /bin/hostname {{ hostalias }}
when: filechanged|success
tags: hostname-run
- name: Restart networking
service: name=networking state=restarted
when: filechanged|success
tags: network
which produces this even though nothing is changed:
ansible-playbook host.yml --check --ask-sudo-pass
PLAY [testers] ****************************************************************
GATHERING FACTS ***************************************************************
ok: [tstsmc1]
TASK: [host | Add the OS specific variables] **********************************
ok: [tstsmc1]
TASK: [host | Install /etc/hosts] *********************************************
ok: [tstsmc1]
TASK: [host | Install /etc/hostname] ******************************************
ok: [tstsmc1]
TASK: [host | Install /etc/network/interfaces] ********************************
ok: [tstsmc1]
TASK: [host | Run hostname] ***************************************************
skipping: [tstsmc1]
TASK: [host | Restart networking] *********************************************
changed: [tstsmc1]
PLAY RECAP ********************************************************************
tstsmc1 : ok=6 changed=1 unreachable=0 failed=0
My concern is why the restart networking would be run given that filechanged is not asserted
by the first four tasks (nor any of the tasks.)
{
Here’s an example of restarting two services when the contents of a file change, but only if the file changes:
- name: template configuration file template: src=template.j2 dest=/etc/foo.conf notify: - restart memcached - restart apache