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

when conditional evaluating to false, undefined dict variables in with_items receive error #11964

Closed
dylanjw opened this issue Aug 14, 2015 · 1 comment

Comments

@dylanjw
Copy link

dylanjw commented Aug 14, 2015

Issue Type: Bug Report
Ansible Version: ansible 2.0.0
Ansible Configuration: clean
Environment: Fedora 22

Summary:
Variables in with_items are checked with when statement evaluating to false.

Steps To Reproduce:

With INSTANCE_CREATION set to false, i get an error with this part of a playbook:

  - name: launch test and DB ec2 instances
      ec2:
        key_name:           "{{ AWS_SSH_KEY_NAME }}"
        instance_type:      "{{ WEBTIER_INSTANCE_TYPE }}"
        image:              "{{ AWS_BASE_IMAGE }}"
        instance_tags:
            Name:           "{{ IMAGE_NAME }}-testdb"
        count_tag:
            Name:           "{{ IMAGE_NAME }}-testdb"
        exact_count:        2
        wait:               true
        group:              "{{ AWS_SG_NAME }}"
        vpc_subnet_id:      "{{ vpc.subnets[0].id }}"
        assign_public_ip:   true
        region:             "{{ REGION }}"
        volumes:
          - device_name: /dev/sda1
            device_type: gp2
            volume_size: 32 
            delete_on_termination: true
      register: ec2test_instances
      when: INSTANCE_CREATION

    - name: launch apphost ec2 instances
      ec2:
        key_name:           "{{ AWS_SSH_KEY_NAME }}"
        instance_type:      "{{ WEBTIER_INSTANCE_TYPE }}"
        image:              "{{ AWS_BASE_IMAGE }}"
        instance_tags:
            Name:           "{{ IMAGE_NAME }}-apphost"
        count_tag:
            Name:           "{{ IMAGE_NAME }}-apphost"
        exact_count:        1
        wait:               true
        group:              "{{ AWS_SG_NAME }}"
        vpc_subnet_id:      "{{ vpc.subnets[0].id }}"
        assign_public_ip:   true
        instance_profile_name: ec2s3
        region:             "{{ REGION }}"
        volumes:
          - device_name: /dev/sda1
            device_type: gp2
            volume_size: 32 
      register: ec2apphost_instance
      when: INSTANCE_CREATION

    - name: add all instances to ec2-hosts group
      add_host:
          hostname:         "{{ item.public_ip }}"
          groupname:        ec2-hosts
      with_items: ec2test_instances.tagged_instances + ec2apphost_instance.tagged_instances
      when: INSTANCE_CREATION

Expected Results:
When INSTANCE_CREATION is false the variables ec2test_instances and
ec2apphost_instance wont get registered and the "add all instances to ec2-host group" task should be skipped without error.

Actual Results:

I receive the following error:

TASK [launch test and DB ec2 instances] ****************************************
skipping: [localhost] => {"changed": false, "skip_reason": "Conditional check failed", "skipped": true}

TASK [launch apphost ec2 instances] ********************************************
skipping: [localhost] => {"changed": false, "skip_reason": "Conditional check failed", "skipped": true}

TASK [add all instances to ec2-hosts group] ************************************
An exception occurred during task execution. The full traceback is:
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/ansible/executor/process/worker.py", line 118, in run
executor_result = TaskExecutor(host, task, job_vars, new_play_context, self._new_stdin, self._loader, shared_loader_obj).run()
File "/usr/lib/python2.7/site-packages/ansible/executor/task_executor.py", line 86, in run
items = self._get_loop_items()
File "/usr/lib/python2.7/site-packages/ansible/executor/task_executor.py", line 156, in _get_loop_items
loop_terms = listify_lookup_plugin_terms(terms=self._task.loop_args, templar=templar, loader=self._loader, fail_on_undefined=True)
File "/usr/lib/python2.7/site-packages/ansible/utils/listify.py", line 34, in listify_lookup_plugin_terms
terms = templar.template(terms, convert_bare=True, fail_on_undefined=fail_on_undefined)
File "/usr/lib/python2.7/site-packages/ansible/template/__init__.py", line 191, in template
result = self._do_template(variable, preserve_trailing_newlines=preserve_trailing_newlines, fail_on_undefined=fail_on_undefined, overrides=overrides)
File "/usr/lib/python2.7/site-packages/ansible/template/__init__.py", line 314, in _do_template
res = j2_concat(rf)
File "<template>", line 9, in root
File "/usr/lib/python2.7/site-packages/jinja2/runtime.py", line 489, in _fail_with_undefined_error
raise self._undefined_exception(hint)
UndefinedError: 'dict object' has no attribute 'tagged_instances'

fatal: [localhost]: FAILED! => {"failed": true, "stdout": ""}

PLAY RECAP *********************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=1
@bcoca
Copy link
Member

bcoca commented Aug 14, 2015

Possible Misunderstanding

Hi!

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.

  • when is evaluated for each item withing an with_ loop, so you cannot use when: false to shortcut undefineds in a with_ .
  • use the default filter to make sure with_ is always correct.

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.

Thank you once again for this and your interest in Ansible!

@bcoca bcoca closed this as completed Aug 14, 2015
rndmh3ro pushed a commit to dev-sec/ansible-collection-hardening that referenced this issue Jan 20, 2016
this prevents the task "remove suid/sgid bit from all binaries except in system and user whitelist"
from failing when the suid-var is not set and `os_security_suid_sgid_remove_from_unknown`
is not set either.
See: ansible/ansible#11964
rndmh3ro pushed a commit to dev-sec/ansible-collection-hardening that referenced this issue Jan 31, 2016
this prevents the task "remove suid/sgid bit from all binaries except in system and user whitelist"
from failing when the suid-var is not set and `os_security_suid_sgid_remove_from_unknown`
is not set either.
See: ansible/ansible#11964
@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
None yet
Projects
None yet
Development

No branches or pull requests

2 participants