1

I’m looking to loop with a list into a dict but can’t find a way with loop/with_*, etc. without reworking my bars.

I’d be glad if some one have a tip!

Here are my bars:

apps:
 - app1
 - app2
 - app3

My settings:

settings:
  app1:
    url: myself
    arg1: value1
    arg2: value2
  app2:
    arg8: value32
    arg38: value00
  app3:
    arg42: /bin/false

And i want to execute a command for each app for each arg.

So my solution would be to transform the list and dict into:

settings:
 - name: app1
   arg: url
   value: myself
 - name: app1
   arg: arg1
   value: value1
 - name: app1
   arg: arg2
   value: value2
 - name: app2
   arg: ...

And then do a with_items on this list and exec my command with name arg and value.
Is this the only solution ?

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
totor42
  • 13
  • 2

1 Answers1

0

This can be achieved with a in between set_fact, listing all the arguments of an application, then using the subelement Jinja filter provided by Ansible to create the commands based on the created fact.

Given the playbook:

- hosts: all
  gather_facts: no
  vars:
    settings:
      app1:
        url: myself
        arg1: value1
        arg2: value2
      app2:
        arg8: value32
        arg38: value00
      app3:
        arg42: /bin/false
      
  tasks:
    ## 
    # This `set_fact` build this list [
    #  {"name": "app1", "args": ["url", "arg1", "arg2"]},
    #  {"name": "app2", "args": ["arg8", "arg38"]},
    #  {"name": "app2", "args": ["arg42"]}
    # ]
    ##
    - set_fact: 
        app_args: |-
          {{ 
            app_args | default([]) + 
            [ {'name': item, 'args': settings[item].keys() | list} ] 
          }}
      loop: "{{ settings.keys() }}"

    - debug:
        msg: >- 
          some_command {{ item.0.name }} 
          --{{ item.1 }}={{ settings[item.0.name][item.1] }}
      loop: "{{ app_args | subelements('args') }}"

Which would yield the recap:

PLAY [all] *******************************************************************************************************

TASK [set_fact] **************************************************************************************************
ok: [localhost] => (item=app1)
ok: [localhost] => (item=app2)
ok: [localhost] => (item=app3)

TASK [debug] *****************************************************************************************************
ok: [localhost] => (item=[{'name': 'app1', 'args': ['url', 'arg1', 'arg2']}, 'url']) => {
    "msg": "some_command app1 --url=myself"
}
ok: [localhost] => (item=[{'name': 'app1', 'args': ['url', 'arg1', 'arg2']}, 'arg1']) => {
    "msg": "some_command app1 --arg1=value1"
}
ok: [localhost] => (item=[{'name': 'app1', 'args': ['url', 'arg1', 'arg2']}, 'arg2']) => {
    "msg": "some_command app1 --arg2=value2"
}
ok: [localhost] => (item=[{'name': 'app2', 'args': ['arg8', 'arg38']}, 'arg8']) => {
    "msg": "some_command app2 --arg8=value32"
}
ok: [localhost] => (item=[{'name': 'app2', 'args': ['arg8', 'arg38']}, 'arg38']) => {
    "msg": "some_command app2 --arg38=value00"
}
ok: [localhost] => (item=[{'name': 'app3', 'args': ['arg42']}, 'arg42']) => {
    "msg": "some_command app3 --arg42=/bin/false"
}

PLAY RECAP *******************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83