Need help with a jinja2 filter

38 views
Skip to first unread message

Kenton Brede

unread,
Mar 12, 2017, 1:55:28 PM3/12/17
to ansible...@googlegroups.com
I'm trying to get the id of the security group that doesn't have 'Default' in the name.  In other words grab "sg-0a2fea74."

This is what  "sg_facts | json_query('instances[*].groups[*])" spits out:

  "groups": [
            {
                "id": "sg-828889fe",
                "name": "Default / EC2 / Linux Ansible Clients"
            },
            {
                "id": "sg-0a2fea74",
                "name": "EC2 / Shib Prd"
            },
            {
                "id": "sg-eb8b8087",
                "name": "Default / Linux Admin Access"
            }
        ],

My with_items filter below is not working but illustrates what I'm trying to do.  Anyone know how I can get this done?  -- Thanks

- name: Gather facts
  ec2_remote_facts:
    region: us-east-1
    filters:
      "tag:Resource_group_id": "{{ resource_group_id_tag }}"
  register: sg_facts
- debug: var={{ item }}
  with_items: "{{ sg_facts | json_query('instances[*].groups[*].[?name !~ 'Default'].id') }}"

Ansible 2.3 isn't in the yum repo yet or I could use ec2_group_facts, which would probably make this simpler...


Kenton Brede




Kenton Brede

unread,
Mar 12, 2017, 4:03:10 PM3/12/17
to ansible...@googlegroups.com
Actually "sg_facts | json_query('instances[*].groups[*])" spits out:

TASK [debug] *******************************************************************
ok: [localhost] => (item={u'id': u'sg-828889fe', u'name': u'Default / EC2 / Linux Ansible Clients'}) => {
    "<type 'dict'>": "VARIABLE IS NOT DEFINED!",
    "item": {
        "id": "sg-828883fe",
        "name": "Default / EC2 / Linux Ansible Clients"
    }
}
ok: [localhost] => (item={u'id': u'sg-eb8b8087', u'name': u'Default / Linux Admin Access / Port 22'}) => {
    "<type 'dict'>": "VARIABLE IS NOT DEFINED!",
    "item": {
        "id": "sg-eb8b8087",
        "name": "Default / Linux Admin Access / Port 22"
    }
}
ok: [localhost] => (item={u'id': u'sg-0a2fea74', u'name': u'EC2 / ITS Shib Prd'}) => {
    "<type 'dict'>": "VARIABLE IS NOT DEFINED!",
    "item": {
        "id": "sg-0a2fea74",
        "name": "EC2 / ITS Shib Prd"
    }
}

Kenton Brede




Reply all
Reply to author
Forward
0 new messages