Bootstrap FreeKB - Ansible - Remove elements from a List
Ansible - Remove elements from a List

Updated:   |  Ansible articles

If you are not familar with lists, check out Ansible - Getting Started with Array Dictionary List.

reject is a Jinja filter that can be used to remove an element from a list. In this example eq (equals) is used to remove "apple" from the fruits list.

---
- hosts: localhost
  gather_facts: false
  tasks:
  - set_fact:
      fruits: [ 'pineapple', 'apple', 'banana', 'orange', 'grapes' ]

  - name: original fruits list
    debug:
      var: fruits

  - name: Remove 'apple' from the 'fruits' list
    set_fact:
      fruits: "{{ fruits | reject('eq', 'apple') | list }}"

  - name: updated fruits list
    debug:
      var: fruits
...

 

In this example, search is used to remove elements containing "apple" from the "fruits" list. This will remove "apple" and "pineapple".

---
- hosts: localhost
  gather_facts: false
  tasks:
  - set_fact:
      fruits: [ 'pineapple', 'apple', 'banana', 'orange', 'grapes' ]

  - name: original fruits list
    debug:
      var: fruits

  - name: Remove elements containing 'apple' from the 'fruits' list
    set_fact:
      fruits: "{{ fruits | reject('search', 'apple') | list }}"

  - name: updated fruits list
    debug:
      var: fruits
...

 

Running this playbook should return the following.

TASK [original fruits list] 
ok: [localhost] => {
    "fruits": [
        "pineapple", 
        "apple", 
        "banana", 
        "orange", 
        "grapes"
    ]
}

TASK [Remove elements containing 'apple' from the 'fruits' list] 
ok: [localhost]

TASK [updated fruits list]
ok: [localhost] => {
    "fruits": [
        "banana", 
        "orange", 
        "grapes"
    ]
}

 

AVOID TROUBLE

The --extra-vars command line option or Extra Variables in Ansible Automation Platform (Tower) will take precedence over set_fact. In other words, reject will fail to remove elements from a list if the list was created using the --extra-vars command line option or Extra Variables in Ansible Automation Platform (Tower). For example 

ansible-playbook demo.yml --extra-vars '{"fruits":["apple","banana","grapes"]}'

 

In this scenario, apple will NOT be removed from the fruits list since the --extra-vars option takes precedence over set_fact.

TASK [original fruits list] 
ok: [localhost] => {
    "fruits": [
        "apple", 
        "banana", 
        "grapes"
    ]
}

TASK [Remove elements containing 'apple' from the 'fruits' list] 
ok: [localhost]

TASK [updated fruits list]
ok: [localhost] => {
    "fruits": [
        "apple", 
        "banana", 
        "grapes"
    ]
}

 

 

A regular expression can be used. This will remove "apple" and will not remove "pineapple".

---
- hosts: localhost
  gather_facts: false
  tasks:
  - set_fact:
      fruits: [ 'pineapple', 'apple', 'banana', 'orange', 'grapes' ]

  - name: original fruits list
    debug:
      var: fruits

  - name: Remove 'apple' from the 'fruits' list
    set_fact:
      fruits: "{{ fruits | reject('search', '^apple$') | list }}"

  - name: updated fruits list
    debug:
      var: fruits
...

 

And here is an example of how to remove an item from a list when the item is a variable.

---
- hosts: all
  tasks:
  - debug:
      var: ansible_all_ipv4_addresses

  - debug:
      var: ansible_default_ipv4.address

  - name: set_fact secondary_ipv4_addresses
    set_fact:
      secondary_ipv4_addresses: "{{ ansible_all_ipv4_addresses | reject('search', ansible_default_ipv4.address) | list }}"

  - debug:
      var: secondary_ipv4_addresses
...

 

In this example, the default IP address is removed so that the secondary_ipv4_addresses list does not contain the default IP address.

ok: [server1.example.com] => {
    "ansible_all_ipv4_addresses": [
        "10.11.12.13", 
        "10.11.12.14", 
        "10.11.12.15", 
        "10.11.12.16"
    ]
}

TASK [debug] 
ok: [server1.example.com] => {
    "ansible_default_ipv4.address": "10.11.12.13"
}

TASK [set_fact secondary_ipv4_addresses] 
ok: [server1.example.com]

TASK [debug] 
ok: [server1.example.com] => {
    "secondary_ipv4_addresses": [
        "10.11.12.14", 
        "10.11.12.15", 
        "10.11.12.16"
    ]
}

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 004a5f in the box below so that we can be sure you are a human.