Conditional task using when_changed

798 views
Skip to first unread message

Jon Chappell

unread,
Oct 4, 2013, 12:41:44 PM10/4/13
to ansible...@googlegroups.com
Greetings,

I'm running into unexpected behavior when trying to use a when_changed statement to trigger an immediate service reload if a change is made.  We wanted to do this instead of doing a flush_handlers to avoid impacting the timing handlers that were triggered by other plays.  

The following set of tasks in a role:
---
- name: Add the PostgreSQL user.
  postgresql_user:
    login_user: postgres
    name: stuff
    password: password_here
    role_attr_flags: CREATEDB
    state: present
  sudo_user: postgres

- name: Make the PostgreSQL stuff user allow password auth.
  lineinfile:
    create: yes
    dest: /etc/postgresql/9.3/main/pg_hba.conf
    line: "local   all             stuff                                  md5"
    regexp: "local\\s+all\\s+(stuff|all)\\s+(peer|md5)"
    state: present
  register: result

- name: Reload PostgreSQL if permissions have changed.
  service:
    name: postgresql
    status: reloaded
  when_changed: $result

-----------------------

When the middle play runs, it generates this output:
TASK: [Make the PostgreSQL stuff user allow password auth.] ******************
changed: [33.33.33.100] => {"changed": true, "item": "", "msg": "line replaced"}


After that, we get the following error:
TASK: [Reload PostgreSQL if permissions have changed.] ************************
fatal: [33.33.33.100] => Conditional expression must evaluate to True or False: is_changed($result)

FATAL: all hosts have already failed -- aborting


As best I can tell, we're calling when_changed appropriately.  Does anyone have insight into what we're doing wrong, or a reason why doing this action this way instead of through a handler would be incorrect?

James Cammarata

unread,
Oct 4, 2013, 1:46:56 PM10/4/13
to ansible...@googlegroups.com
Does this happen if you use the new syntax of "when: result.changed"?


--
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.
For more options, visit https://groups.google.com/groups/opt_out.



--

James Cammarata <jcamm...@ansibleworks.com>
Sr. Software Engineer, AnsibleWorks, Inc.
http://www.ansibleworks.com/

Jon Chappell

unread,
Oct 4, 2013, 2:13:04 PM10/4/13
to ansible...@googlegroups.com
Looks like it works with the new syntax.  I didn't even see a mention to that in the docs.  Must have slipped by me.

Michael DeHaan

unread,
Oct 4, 2013, 2:41:50 PM10/4/13
to ansible...@googlegroups.com
So BTW, this is wrong:

  when_changed: $result

Do this:

   when_changed: result

You only need to say "{{ foo }}" when you are forcing getting a string somewhere, and should never be using old style variables.


Michael DeHaan <mic...@ansibleworks.com>
CTO, AnsibleWorks, Inc.
http://www.ansibleworks.com/

Michael DeHaan

unread,
Oct 4, 2013, 2:43:13 PM10/4/13
to ansible...@googlegroups.com
Also, a bit confused here by the topic.

"changed_when" is a thing that controls when something reports changed or not.

"when" is something that decides if something executes or not

"when: foo.changed" is a thing

you can also say

"when: foo|changed" and this better works with tasks that might be skipped, etc, and doesn't require them to have a ".changed" attribute.


Michael DeHaan

unread,
Oct 4, 2013, 2:43:27 PM10/4/13
to ansible...@googlegroups.com
There is no "when_changed" :)


Reply all
Reply to author
Forward
0 new messages