Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

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

Meta: end_host in Block being triggered despite tags/when statement #68655

Closed
michaelsmoody opened this issue Apr 3, 2020 · 8 comments
Closed
Labels
affects_2.9 This issue/PR affects Ansible v2.9 bug This issue/PR relates to a bug. module This issue/PR relates to a module. python3 support:community This issue/PR relates to code supported by the Ansible community. support:core This issue/PR relates to code supported by the Ansible Engineering Team. utilities Utilities category

Comments

@michaelsmoody
Copy link

SUMMARY

We have several tasks in our role that perform some checking, and if a specific set of conditions are met, ends the run for that host, via meta: end_host.

We place these tasks inside of a block section. We also have the block statement being part of tags, and the block section also has a when: statement that must match several conditions. While this has tended to work, I have two of these blocks that are always getting triggered. At first glance, this seemed similar to #64558, but the addition of blocks with when statements guided me to open a separate issue. If it is that the root cause is the same, then please feel to merge the issues.

Example playbook below.

ISSUE TYPE
  • Bug Report
COMPONENT NAME

meta
when
tags

ANSIBLE VERSION
ansible 2.9.6
  config file = None
  configured module search path = ['/Users/michaelsmoody/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/Cellar/ansible/2.9.6_1/libexec/lib/python3.8/site-packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 3.8.2 (default, Mar 11 2020, 00:28:52) [Clang 11.0.0 (clang-1100.0.33.17)]
CONFIGURATION
No Output
OS / ENVIRONMENT

macOS 10.14.6. Target device Windows Server 2016 (though confirmed on Linux target)
Confirmed against physical server across VPN, and local system. Confirmed against local Virtualbox VM. Confirmed against Vagrant.

STEPS TO REPRODUCE

export OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES; export PYTHONHTTPSVERIFY=0; ansible-playbook -i inventory/hosts -u ansible-control-user -k -l 127.0.0.1 main.yml --tags 'imy-first-tag' -e "someextravariable=content"

- block:
    - name: "Perform a set of tasks if conditions are met"
      pause:
        seconds: 5
        prompt: |
          "Several lines of output"
           "Another line of output"
           "We are providing some information to the user here"

    - name: "End the task if the conditions are met after the output is provided"
      meta: end_host

  when: avariablehere is defined and bvariablehere is defined and avariablehere['content']|b64decode|default('avariabledefault') is not search(bvariablehere['content']|b64decode|default('bvariabledefault'))

  tags:
    - my-first-tag
    - my-second-tag
    - my-third-tag
EXPECTED RESULTS

I expect that when the conditions are met, the play will end for the host. When the conditions are not met, the play will continue for the host.

ACTUAL RESULTS

When the conditions are met, the play ends for the host. When the conditions are not met, the play ends for the host. However, the pause/prompt does NOT appear, only the end_host is triggered. Similarly, it does not appear with -vvvvv, only a message about:

META: ending play for 127.0.0.1

Relevant output:

META: ending play for 127.0.0.1

@ansibot
Copy link
Contributor

ansibot commented Apr 3, 2020

Files identified in the description:

If these files are incorrect, please update the component name section of the description or use the !component bot command.

click here for bot help

@ansibot ansibot added affects_2.9 This issue/PR affects Ansible v2.9 bug This issue/PR relates to a bug. module This issue/PR relates to a module. needs_triage Needs a first human triage before being processed. python3 support:community This issue/PR relates to code supported by the Ansible community. support:core This issue/PR relates to code supported by the Ansible Engineering Team. utilities Utilities category labels Apr 3, 2020
@mkrizek
Copy link
Contributor

mkrizek commented Apr 3, 2020

There is a pull request to allow using tags on meta tasks: #67508

As for the when I am going to go ahead and close this in favor of #41313.

If you have further questions please stop by IRC or the mailing list:

@mkrizek mkrizek closed this as completed Apr 3, 2020
@mkrizek mkrizek removed the needs_triage Needs a first human triage before being processed. label Apr 3, 2020
@michaelsmoody
Copy link
Author

@mkrizek before you close it, can you confirm that #41313 is appropriate, considering this isn't necessarily specific to the meta/when issue, but rather that this is inside of a block statement, and somehow the block is being partially triggered, even though it itself has a when statement?

Essentially, the meta and the pause/prompt are inside of a block section, which itself has a tag/when statement. And yet, despite the meta being inside of a block, which should only be followed upon tag/when condition match, it's still happening? If the block itself doesn't happen, should tasks contained within the block, even if it is a meta statement?

I just want to confirm with you that this is the correct behavior?

@michaelsmoody
Copy link
Author

Also, is there a better solution for ending a host that this meta: end_host, at the moment, given this logic I need? Open to suggestions, though this seemed to be the one that documentation suggested. Thank you for your patience, and to the entire team for such a wonderful piece of software. Ansible saves so much time.

@mkrizek
Copy link
Contributor

mkrizek commented Apr 3, 2020

Sorry, I misread. meta: end_host does support when:

- hosts: h1, h2
  gather_facts: no
  tasks:
    - debug:
        msg: should run on both
    - block:
      - debug:
          msg: should only run on h1
      - meta: end_host
      when: inventory_hostname == 'h1'
    - debug:
        msg: should only run on h2

So only tags is the issue here which is handled in the PR I linked.

@michaelsmoody
Copy link
Author

In the case that meta: end_host does support when, then there is an actual bug here. The when statement conditions are not being satisfied, and yet meta: end_host is still being triggered.

That's provable in that the pause/prompt isn't triggered, but the meta: end_host is, tags or no tags.

Thoughts?

@mkrizek
Copy link
Contributor

mkrizek commented Apr 3, 2020

My example says otherwise. Can you provide a simple reproducer that I can copy and run on my machine? Thank you!

@mkrizek
Copy link
Contributor

mkrizek commented Apr 3, 2020

- hosts: h1, h2
  gather_facts: no
  tasks:
    - debug:
        msg: should run on both
    - block:
      - debug:
          msg: should not run on either host
      - pause:
          seconds: 5
          prompt: "ansible"
      - meta: end_host
      when: False
    - debug:
        msg: should run on both
PLAY [h1, h2] **********************************************************************************************************

TASK [debug] ***********************************************************************************************************
ok: [h1] => {
    "msg": "should run on both"
}
ok: [h2] => {
    "msg": "should run on both"
}

TASK [debug] ***********************************************************************************************************
skipping: [h1]
skipping: [h2]

TASK [pause] ***********************************************************************************************************
skipping: [h1]

TASK [debug] ***********************************************************************************************************
ok: [h1] => {
    "msg": "should run on both"
}
ok: [h2] => {
    "msg": "should run on both"
}

PLAY RECAP *************************************************************************************************************
h1                         : ok=2    changed=0    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0   
h2                         : ok=2    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0

@ansible ansible locked and limited conversation to collaborators May 1, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.9 This issue/PR affects Ansible v2.9 bug This issue/PR relates to a bug. module This issue/PR relates to a module. python3 support:community This issue/PR relates to code supported by the Ansible community. support:core This issue/PR relates to code supported by the Ansible Engineering Team. utilities Utilities category
Projects
None yet
Development

No branches or pull requests

3 participants