rupenzal
rupenzal

Reputation: 1

Is there any way to remove specific key value when iterating over list of dictionaries in ansible?

I have below data in CSV format

instance_id,Tag1,Tag2,Tag3,Tag4
instance1,no,test,lite,no
instance2,yes,dev,enterprise,yes

when I read this data using:-

    read_csv:  
       path: <csv path>  
    register: instance
  - debug: var=instance

Upon debugging I get:

    ok: [localhost] => {  
        "instance": {  
            "changed": false,  
            "dict": {},  
            "failed": false,  
            "list": [  
                {  
                    "Tag1": "no",  
                    "Tag2": "test",  
                    "Tag3": "lite",  
                    "Tag4": "no",  
                    "instance_id": "instance1"  
                },  
                {  
                    "Tag1": "yes",  
                    "Tag2": "dev",  
                    "Tag3": "enterprise",  
                    "Tag4": "yes",  
                    "instance_id": "instance2"  
                }  
            ]  
       }  
   }  

Now I want to update Tag1, Tag2, Tag3, Tag4 on respective instance1, and instance2. I tried:-

  ec2_tag:
     region: us-west-1
     resource: "{{ instances.instance_id }}"
     state: present
     tags: "{{ instance.list[0] }}"


  loop: "{{ instance.list }}"
  loop_control:
         loop_var: instances

I am getting the desired result however I am getting instance_id also as a Tag. See below example

Tags for instance1 on my AWS console are :
Tag1: no
Tag2: test
Tag3: lite
Tag4: no
instance_id: instance1 {I do not want this as my tag attached to my instance}

Similarly, for instance2, I am getting respective tags:
Tag1: yes
Tag2: dev
Tag3: enterprise
Tag4: yes
instance_id: instance2 {I do not want this as my tag attached to my instance}

Upvotes: 0

Views: 255

Answers (1)

Zeitounator
Zeitounator

Reputation: 44595

You can skip list of dictionnary elements based on a test on one of their attribute with the selectattr/rejectattr jinja2 builtin filters, as demonstrated in the following playbook. Note that I used equality with rejectattr for a simple example, but you can use any other test you want (see jinja2 builtin tests and ansible specific ones) and switch to selectattr which uses the same syntax if more convenient.

---
- hosts: localhost
  gather_facts: false
  
  vars:
    my_csv:
      - field1: a
        field2: b
        field3: c
      - field1: d
        field2: value to skip
        field3: f
      - field1: x
        field2: y
        field3: z

  tasks:
    - name: rejectattr demo
      debug:
        msg: "{{ my_csv | rejectattr('field2', 'eq', 'value to skip') | list }}"

which gives:

PLAY [localhost] ***********************************************************************************************************************************************************************************************************************

TASK [rejectattr demo] *****************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": [
        {
            "field1": "a",
            "field2": "b",
            "field3": "c"
        },
        {
            "field1": "x",
            "field2": "y",
            "field3": "z"
        }
    ]
}

PLAY RECAP *****************************************************************************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Upvotes: 2

Related Questions