Subbeh
Subbeh

Reputation: 924

Ansible: output to dict instead of list

I have a dictionary with containers specified as follows (simplified):

container:
  - name: test1
  ...
  
  - name: test2
  ...

To create the test1 container, I can run it as follows:

- docker_container: "{{ containers[lookup('index_of', containers, 'eq', 'test1', 'name')] }}"

I thought I could simplify this as follows:

- docker_container: "{{ containers | selectattr('name', 'eq', 'test1') }}"

But the output is generated as a list instead of a dictionary.

Using debug, the output of both commands is:

    "msg": [
        {
            "name": "test1"

vs

    "msg": {
        "name": "test1"

I'm still learning about jinja and ansible. How can I make this work? I tried adding | dict and | flatten at the end, but this throws an error.

Upvotes: 1

Views: 74

Answers (1)

Frenchy
Frenchy

Reputation: 17037

use the plugin first:

- docker_container: "{{ containers | selectattr('name', 'eq', 'test1') |first}}"

Upvotes: 2

Related Questions