Coder Social home page Coder Social logo

Comments (7)

nikut avatar nikut commented on August 16, 2024 2

Thanks for the explanation, @sivel . This behaviour is not mentioned in the documentation and I think it's highly counterintuitive. Also, it's not an issue only with inventory_hostname, please consider the following playbook:

---
- name: Test pause
  hosts: testhosts
  gather_facts: no
  tasks:
    - local_action: set_fact pause_needed=true
      when: inventory_hostname == 'hostb'

    - local_action: pause seconds=5
      when: pause_needed is defined and pause_needed

I think it would be reasonable to assume that the above playbook would pause for 5 seconds at hostb, but this does not happen:

PLAY [Test pause] ************************************************************* 

TASK: [set_fact pause_needed=true] ******************************************** 
skipping: [hosta -> 127.0.0.1]
skipping: [hostc -> 127.0.0.1]
skipping: [hostd -> 127.0.0.1]
ok: [hostb -> 127.0.0.1]

TASK: [pause seconds=5] ******************************************************* 
skipping: [hosta -> 127.0.0.1]

PLAY RECAP ******************************************************************** 
hosta                      : ok=0    changed=0    unreachable=0    failed=0   
hostb                      : ok=1    changed=0    unreachable=0    failed=0   
hostc                      : ok=0    changed=0    unreachable=0    failed=0   
hostd                      : ok=0    changed=0    unreachable=0    failed=0   

How would you achieve the following?

  1. Update a configuration on a number of hosts and register the result (register: update)
  2. Restart the affected services when: update.changed
  3. Pause for a moment after each restart so that the cluster can realign itself (I tried to use pause ... when: update.changed for this but, obviously, failed)

from ansible-modules-core.

skynet avatar skynet commented on August 16, 2024 1

@mpdehaan - I had the same issue. pause is not well understood and/or explained.

from ansible-modules-core.

sivel avatar sivel commented on August 16, 2024

The pause action_plugin is what is referred to as a host list bypass plugin. This means that pause will not execute for every host targeted by a playbook. Instead pause will only execute on the first host in the hosts list as defined by inventory. Due to this, inventory_hostname will always evaluate to hosta, making some of your when statements false.

With that understanding, your tasks are doing what they are instructed to do.

from ansible-modules-core.

sivel avatar sivel commented on August 16, 2024

You can not reliably use host facts (host_vars, gathered facts, set_fact, registered vars) in a when statement with a host list bypass plugin.

As mentioned, ansible will only run pause for the first host as defined by inventory. This means that you are inspecting host facts for only that first host, and no other hosts.

You can utilize hostvars['somehost']['somevar'] to access vars for other hosts. That in combination with with_items could help, but you could find yourself pausing for every host in the play per pause task. So if you had 300 hosts, and you used with_items: play_hosts and had when: hostvars[item]['update']|changed it could sleep 300 times for 5 seconds each which equates to 25 minutes of pauses.

The better solution, instead of using a pause, would be to use wait_for and actually wait for something to happen, as opposed to an arbitrary pause.

from ansible-modules-core.

mpdehaan avatar mpdehaan commented on August 16, 2024

I'm going to close this one because of the host loop misunderstanding above, if there are further questions, discussion on ansible-project is quite welcome.

Thanks!

from ansible-modules-core.

nikut avatar nikut commented on August 16, 2024

@sivel , wait_for with a delay is what I ended up using to work around this, but I still think that this behaviour is counterintuitive. Also, sometimes there may not be anything externally accessible to wait for (for example, you might just want to allow some time for a cache to warm up).

@mpdehaan , at the very least this should be clearly pointed out in the documentation. I would be surprised if I was the first one to be confused by this.

Anyway, thanks for the clarification, guys.

from ansible-modules-core.

udondan avatar udondan commented on August 16, 2024

@sivel Thanks for the loop suggestion. In my case wait_for was not an option and I really required a conditional pause for every host the user has to confirm. With a loop this indeed works. If anyone else needs something like this, I posted my solution on SO: http://stackoverflow.com/a/35169496/2753241

from ansible-modules-core.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.