2

I am running shell command in my playbook to extract the results of a command. The output of

- debug: msg="{{ dblist.stdout_lines }}"

is

ok: [host] => {
    "msg": [
"inst1:db1"
"inst1:db2"
"inst1:db3"
"inst2:db4"
"inst2:db3"
  ]
}

I need to store this value in a format that allows me to run my next playbook considering the value of item.0 as inst1 and item.1 as db2 and so on.

Currently

- debug: msg="{{ item.0 }} has  a value {{ item.1 }}"
  with_items: "{{ dblist.stdout_lines }}"

is giving values like

ok: [host] => (item=inst1:db1) => {
    "msg": "i has  a value n"
}
ok: [host] => (item=inst1:db2) => {
    "msg": "i has  a value n"

Thanks.

3

For example

    - debug:
        msg: "{{ _key }} has a value {{ _val }}"
      loop: "{{ dblist.stdout_lines }}"
      vars:
        _arr: "{{ item.split(':') }}"
        _key: "{{ _arr.0 }}"
        _val: "{{ _arr.1 }}"

gives

  msg: inst1 has a value db1
  msg: inst1 has a value db2
  msg: inst1 has a value db3
  msg: inst2 has a value db4
  msg: inst2 has a value db3
2

If you're on a new enough version (ansible-core>=2.11), you can use the split filter:

    - debug:
        msg: "{{ item.0 }} has a value {{ item.1 }}"
      loop: "{{ dblist.stdout_lines | map('split', ':') }}"

It's possible to achieve a similar result on older versions, but it's uglier:

    - debug:
        msg: "{{ item.0 }} has a value {{ item.1 }}"
      loop: "{{ dblist.stdout_lines }}"
      loop_control:
        loop_var: _item
      vars:
        item: "{{ _item.split(':') }}"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.