Score:1

Create parameters from a list of lists

br flag

I have a fact, which is a list of lists, e.g. [ [disk1, filename1], [disk2, filename2], ... [diskN,filenameN] ]

I need to run a command in the format like command -x disk1,file=filename1 -x disk2,file=filename2 ... -x diskN,file=filenameN

I can't figure out how I can flatten the list and insert the option -x and file to create the string.

Any suggestion is appreciated. Thanks Billy

Score:1
br flag

Given the list

  disk_file:
    - [disk1, filename1]
    - [disk2, filename2]
    - [diskN, filenameN]

join the items. For example,

  params: "-x {{ disk_file|map('join', ',file=')|join(' -x ') }}"

gives

  params: -x disk1,file=filename1 -x disk2,file=filename2 -x diskN,file=filenameN

Example of a complete playbook for testing

- hosts: localhost

  vars:

    disk_file:
      - [disk1, filename1]
      - [disk2, filename2]
      - [diskN, filenameN]

    params: "-x {{ disk_file|map('join', ',file=')|join(' -x ') }}"

  tasks:

    - debug:
        var: params
    - command: echo '{{ params }}'
      register: out
    - debug:
        var: out.stdout

gives

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

TASK [debug] *********************************************************************************
ok: [localhost] => 
  params: -x disk1,file=filename1 -x disk2,file=filename2 -x diskN,file=filenameN

TASK [command] *******************************************************************************
changed: [localhost]

TASK [debug] *********************************************************************************
ok: [localhost] => 
  out.stdout: -x disk1,file=filename1 -x disk2,file=filename2 -x diskN,file=filenameN

PLAY RECAP ***********************************************************************************
localhost: ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
Billy K avatar
br flag
Actually it works fine after a quick test. Awesome.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.