ansible and dependencies

62 views
Skip to first unread message

Stuart Cracraft

unread,
Nov 13, 2015, 5:39:15 PM11/13/15
to Ansible Project
Hi - trying to ensure I can make tasks dependent on prior task successes in roles inside playbooks.

So far it doesn't happen. I'm using a combination of register/when.

What is the method within a series of tasks in a role's tasks/main.yml to make one dependent upon the prior one's running as success?

This only partially works:

---

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





Joanna Delaporte

unread,
Nov 13, 2015, 6:54:43 PM11/13/15
to Ansible Project
I'm not sure I understand what you are asking for...by design, a playbook (and therefore a role) will stop processing if any task fails. So, it is implicit that the next task won't happen unless the previous one succeeds.

Also, it looks like you are trying to change the hostname...is that correct? There is a hostname module that performs many of these steps.

Is there a reason you wouldn't use notify to trigger the network service restart, and call it as a handler?

For example, from Ansible docs:

{

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

Brian Coca

unread,
Nov 13, 2015, 6:54:47 PM11/13/15
to Ansible Project
you are using the same registered var in several tasks, the last one
will overwrite the previous ones, you might want to create a diff var
per task and then have a aggregated one.


--
Brian Coca
Reply all
Reply to author
Forward
0 new messages