相关文章推荐

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement . We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account config file = configured module search path = Default w/o overrides python version = 2.6.9 (unknown, Aug 5 2016, 11:15:31) [GCC 4.3.4 [gcc-4_3-branch revision 152973]]
CONFIGURATION

no config file

OS / ENVIRONMENT

Ansible running on SUSE 11 SP4

SUMMARY

end_play "action" of the meta module is not working
I took the https://gist.github.com/jimi-c/c45b225e101fe673e42ffb719f259fbd example given by @jimi-c in #1476 but on my system it doesn't work

STEPS TO REPRODUCE
# cat test_meta_play_end.yml 
- hosts: all
  gather_facts: no
  connection: local
  tasks:
  - debug: msg="task 1"
  - debug: msg="task 2"
  - meta: end_play
    when: inventory_hostname == 'host3'
  - debug: msg="you shouldn't see me on host3"
# ansible-playbook -vvv test_meta_play_end.yml -i host1,host2,host3                                                                                                  
No config file found; using defaults
PLAYBOOK: test_meta_play_end.yml ************************************************************************************************************************************************************************************************************
1 plays in test_meta_play_end.yml
PLAY [all] **********************************************************************************************************************************************************************************************************************************
META: ran handlers
TASK [debug] ********************************************************************************************************************************************************************************************************************************
task path: /fs/softs/ansible/test_meta_play_end.yml:5
ok: [host1] => {
    "msg": "task 1"
ok: [host2] => {
    "msg": "task 1"
ok: [host3] => {
    "msg": "task 1"
TASK [debug] ********************************************************************************************************************************************************************************************************************************
task path: /fs/softs/ansible/test_meta_play_end.yml:6
ok: [host1] => {
    "msg": "task 2"
ok: [host2] => {
    "msg": "task 2"
ok: [host3] => {
    "msg": "task 2"
META: 
TASK [debug] ********************************************************************************************************************************************************************************************************************************
task path: /fs/softs/ansible/test_meta_play_end.yml:9
ok: [host1] => {
    "msg": "you shouldn't see me on host3"
ok: [host2] => {
    "msg": "you shouldn't see me on host3"
ok: [host3] => {
    "msg": "you shouldn't see me on host3"
META: ran handlers
META: ran handlers
PLAY RECAP **********************************************************************************************************************************************************************************************************************************
host1                      : ok=3    changed=0    unreachable=0    failed=0   
host2                      : ok=3    changed=0    unreachable=0    failed=0   
host3                      : ok=3    changed=0    unreachable=0    failed=0   
EXPECTED RESULTS

The play should end for host3 and we should not see the "you shouldn't see me on host3" message

ACTUAL RESULTS

"you shouldn't see me on host3" is displayed while it should not

For the linear strategy, meta tasks are just run once and for all hosts currently being iterated over. This causes an issue for (I think) not only end_play but also clear_facts and clear_host_errors.
This is due to _evaluate_conditional(target_host) being in the wrong place.
This is a working version based on the version 2.3.2.0

I tried to replace original file with yours, but it's not working well. Probably not compatible with ansible 2.4

Run with your file gives:

ERROR! Unexpected Exception, this is probably a bug: No module named six.moves

If I removed problematic lines, I get

ERROR! Unexpected Exception, this is probably a bug: cannot import name action_loader

so, I just gave up.

I just accidental noticed this behavior. I thought 'end_play' is per host, but in fact it seems to stop the full playbook. Please either merge this PR OR at least put a big fat warning into the documentation that 'end_play' halts the full execution.

This behavior can screw you royally if you don't expect it (and don't look for it).

(I added the same comment to the PR; sorry for the redundancy)

@bcoca since you wrote ticket and not pr in #32785 I confused the PR with this ticket:

IMHO this is still a bug/surprising behavior:

cat > 'debug_end_play.yml' <<EOF
- hosts: all
  tasks:
    - debug:
        var: ansible_distribution_major_version
    # do not run on CentOS/RHEL 6
    - meta: end_play
      when: ansible_distribution_major_version == '6'
    - debug:
        msg: After end_play
ansible-playbook -i rhel7,rhel6 debug_end_play.yml 
PLAY [all] ********************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ********************************************************************************************************************************************************************************************
ok: [rhel7]
ok: [rhel6]
TASK [debug] ******************************************************************************************************************************************************************************************************
ok: [rhel7] => {
    "ansible_distribution_major_version": "7"
ok: [rhel6] => {
    "ansible_distribution_major_version": "6"
TASK [debug] ******************************************************************************************************************************************************************************************************
ok: [rhel7] => {
    "msg": "After end_play"
ok: [rhel6] => {
    "msg": "After end_play"
PLAY RECAP ********************************************************************************************************************************************************************************************************
rhel6                      : ok=3    changed=0    unreachable=0    failed=0   
rhel7                      : ok=3    changed=0    unreachable=0    failed=0   
$ ansible-playbook -i rhel6,rhel7 debug_end_play.yml 
PLAY [all] ********************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ********************************************************************************************************************************************************************************************
ok: [rhel7]
ok: [rhel6]
TASK [debug] ******************************************************************************************************************************************************************************************************
ok: [rhel6] => {
    "ansible_distribution_major_version": "6"
ok: [rhel7] => {
    "ansible_distribution_major_version": "7"
PLAY RECAP ********************************************************************************************************************************************************************************************************
rhel6                      : ok=2    changed=0    unreachable=0    failed=0   
rhel7                      : ok=2    changed=0    unreachable=0    failed=0  
          

Possible Misunderstanding

Thanks very much for your submission to Ansible. It sincerely means a lot to us.

We believe the ticket you have filed is being somewhat misunderstood, as one thing works a little differently than stated.

The conditional applied follows the rules of run_once or 'bypass host loop' modules, meta: end_play is not PER host. As such the conditional will be calculated ONLY on the first host to reach the task and then the task is applied to ALL hosts in the play.

What you want is a feature that does not exist yet, see @tyll's ticket for meta: skip_host or similar (remove_host_from_play, end_play_for_host ... bikeshed will happen).

In the future, this might be a topic more well suited for the user list, which you can also post here if you'd like some more help with the above.

  • https://groups.google.com/forum/#!forum/ansible-project
  • Thank you once again for this and your interest in Ansible!