Skip to content

aws_ssm lookup does not fail when parameter does not exist #126

Closed

Description

@Akasurde

From @tinyzimmer on Jan 02, 2019 20:09

SUMMARY

Using the aws_ssm lookup plugin to fetch an application secret, if the parameter does not exist, the plugin will fail silently setting an empty string for the queried value.

ISSUE TYPE
  • Bug Report
COMPONENT NAME

aws_ssm

ANSIBLE VERSION

2.7.5

CONFIGURATION
user@somecomputer /cygdrive/c/Users/{{ redacted }}/Development/{{ redacted }}
$ ansible-config dump --only-changed

user@somecomputer /cygdrive/c/Users/{{ redacted }}/Development/{{ redacted }}
$
OS / ENVIRONMENT

Running in Cygwin on Windows 10 Pro (work computer 🤢) but likely affects other platforms?

STEPS TO REPRODUCE
# test.yml
- name: Test Param Retrieval
  hosts: localhost
  gather_facts: false
  tasks:

    # I'd expect this to fail
    - set_fact:
        test_var: "{{ lookup('aws_ssm', 'nonexistant_param', region='us-east-1') }}"

    - debug:
        var: test_var
EXPECTED RESULTS

Fail because of non-existent parameter. Since it wasn't failing, I had no idea I was using the wrong AWS Shared Credentials Profile on the computer I was running this from. If this is expected behavior I suppose that's fine, but I don't see why it would be.

ACTUAL RESULTS

Playbook continues and any templates that use that value set it to an empty string

$ ansible-playbook test.yml
 [WARNING]: Unable to parse /etc/ansible/hosts as an inventory source

 [WARNING]: No inventory was parsed, only implicit localhost is available

 [WARNING]: provided hosts list is empty, only localhost is available. Note
that the implicit localhost does not match 'all'


PLAY [Test Param Retrieval] ****************************************************

TASK [set_fact] ****************************************************************
ok: [localhost]

TASK [debug] *******************************************************************
ok: [localhost] => {
    "test_var": ""
}

PLAY RECAP *********************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0

Copied from original issue: ansible/ansible#50466

Activity

  1. Akasurde commented on Aug 11, 2020

    @Akasurde
    MemberAuthor

    From @Edward-Ireson on Nov 19, 2019 16:32

    I had the same issue and had to work around it, I did

    "{{ lookup('aws_ssm', '/var/var1', region='eu-west-2', shortnames=true ) | default(NULL,true) }}"

    The lookup is passed an empty result which because of the true flag tries to set the default to NULL which then fails because NULL is not defined.

  2. ansibullbot commented on Aug 27, 2020

    @ansibullbot
  3. added
    bug
    module
    plugins
    on Aug 27, 2020
  4. markuman commented on Dec 6, 2020

    @markuman
    Member

    it looks like that this behaviour is by design.

    Furthermore, the documentation mentioned this behaviour also.

    If looking up an explicitly listed parameter by name which does not exist then the lookup will return a None value which will be interpreted by Jinja2 as an empty string. You can use the default filter to give a default value in this case but must set the second parameter to true (see examples below)

    https://docs.ansible.com/ansible/latest/collections/amazon/aws/aws_ssm_lookup.html

  5. JonTheNiceGuy commented on Dec 7, 2020

    @JonTheNiceGuy

    From @Edward-Ireson on Nov 19, 2019 16:32

    I had the same issue and had to work around it, I did

    "{{ lookup('aws_ssm', '/var/var1', region='eu-west-2', shortnames=true ) | default(NULL,true) }}"

    The lookup is passed an empty result which because of the true flag tries to set the default to NULL which then fails because NULL is not defined.

    NULL isn't defined. You're probably want something like this:

    - name: Test Param Retrieval
      hosts: localhost
      gather_facts: false
      tasks:
        - set_fact:
            test_var: "{{ lookup('aws_ssm', 'nonexistant_param', region='us-east-1') }}"
    
        - fail:
            when: test_var == ''

    or

    - name: Test Param Retrieval
      hosts: localhost
      gather_facts: false
      tasks:
        - fail:
          when: lookup('aws_ssm', 'nonexistant_param', region='us-east-1') == ''
    
        - set_fact:
            test_var: "{{ lookup('aws_ssm', 'nonexistant_param', region='us-east-1') }}"

    I don't believe any lookup will cause a "fail" state to occur.

  6. tremble commented on Dec 7, 2020

    @tremble
    Contributor

    The aws_secrets lookup recently added support for on_missing and on_denied (#122)

    A similar PR for aws_ssm might be a nice solution here.

  7. added
    waiting_on_contributor
    and removed on Dec 9, 2020
  8. added
    needs_info
    and removed
    waiting_on_contributor
    on May 6, 2021
  9. added
    waiting_on_contributor
    on May 21, 2021
  10. 2 remaining items

  11. removed
    waiting_on_contributor
    on May 31, 2021
  12. added
    waiting_on_contributor
    on May 31, 2021
  13. added
    feature
    and removed
    bug
    waiting_on_contributor
    on May 31, 2021
  14. added
    waiting_on_contributor
    on May 31, 2021
  15. removed
    module
    waiting_on_contributor
    on Jul 1, 2021
  16. ansibullbot commented on Jul 1, 2021

    @ansibullbot

    cc @linabuzachis
    click here for bot help

  17. added
    module
    waiting_on_contributor
    on Jul 1, 2021
  18. added a commit that references this issue on Jul 7, 2021
    790b54b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

affects_2.10featurehas_prmodulepluginswaiting_on_contributor

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions