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

Evaluating when: conditionals and with_item: #18216

Closed
basictheprogram opened this issue Oct 27, 2016 · 2 comments
Closed

Evaluating when: conditionals and with_item: #18216

basictheprogram opened this issue Oct 27, 2016 · 2 comments
Labels
affects_2.3 This issue/PR affects Ansible v2.3 bug This issue/PR relates to a bug.

Comments

@basictheprogram
Copy link

ISSUE TYPE
  • Bug Report
COMPONENT NAME

when: conditional
with_items:

ANSIBLE VERSION
$ ansible --version
ansible 2.3.0 (devel 4c9ded9cb7) last updated 2016/10/26 15:52:32 (GMT -500)
  lib/ansible/modules/core: (devel 06d0987848) last updated 2016/10/26 15:44:10 (GMT -500)
  lib/ansible/modules/extras: (devel 1e9615730f) last updated 2016/10/26 15:44:29 (GMT -500)
  config file =
  configured module search path = Default w/o overrides
CONFIGURATION

Stock devel configuration

OS / ENVIRONMENT

Running macOS Sierra
Managing Ubuntu Xenial

SUMMARY

The when: conditionals are not evaluated before with_items: variable references are resolved

STEPS TO REPRODUCE
- debug: msg="{{ bootstrap_sshguard_whitelist }}"
  when: bootstrap_sshguard_whitelist is defined

- name: sshguard whitelist entries
  lineinfile:
    dest: /etc/sshguard/whitelist
    regexp: '.*{{ item.ip }}.*$'
    line: "{{ item.ip }}        # {{ item.comment }}"
    state: present
  when: 
    - bootstrap_sshguard_whitelist is defined
    - st.stat is defined 
    - st.stat.exists
  with_items:
    - "{{  bootstrap_sshguard_whitelist  }}"
  notify:
    - reload sshguard
EXPECTED RESULTS

When boostrap_sshguard_whitelist is NOT defined the "sshguard whitelist entries" task should not be executed. But it looks like the ansible attempts to resolve the "{{ bootstrap_sshguard_whitelist }}" in the with_items:

ACTUAL RESULTS
TASK [bootstrap : debug] *******************************************************
skipping: [testing_xenial64]

TASK [bootstrap : sshguard whitelist entries] **********************************
fatal: [testing_xenial64]: FAILED! => {"failed": true, "msg": "'bootstrap_sshguard_whitelist' is undefined"}

The debug is skipped proving bootstrap_sshguard_whitelist is not defined.

@basictheprogram
Copy link
Author

Might be my misunderstanding as documented here?

#13791

@ansibot ansibot added bug_report affects_2.3 This issue/PR affects Ansible v2.3 labels Oct 27, 2016
@basictheprogram
Copy link
Author

My misunderstanding. Below tasks "fixes" my problems.

- name: sshguard whitelist entries
  lineinfile:
    dest: /etc/sshguard/whitelist
    regexp: '.*{{ item.ip }}.*$'
    line: "{{ item.ip }}        # {{ item.comment }}"
    state: present
  when: 
    - st.stat is defined 
    - st.stat.exists
  with_items:
    - "{{  bootstrap_sshguard_whitelist|default(omit) }}"
  notify:
    - reload sshguard

@ansibot ansibot added bug_report affects_2.3 This issue/PR affects Ansible v2.3 labels Oct 27, 2016
@ansibot ansibot added bug This issue/PR relates to a bug. and removed bug_report labels Mar 7, 2018
@ansible ansible locked and limited conversation to collaborators Apr 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.3 This issue/PR affects Ansible v2.3 bug This issue/PR relates to a bug.
Projects
None yet
Development

No branches or pull requests

2 participants