1

I'm running a javaprogram with shell module to iterate over a list of users, for each user I want to loop through a number of groups to add to each user.

I'm not looking to add new users/groups to the OS but to a program that runs on the server.

All users in USER_LIST should be added to all groups in GROUP_LIST.

Non working example:

vars: 
GROUP_LIST:
- DATA
- ONE
- TWO

USER_LIST:
- USER1
- USER2
- USER3

- name: add all users to all groups
  shell:
  cmd: |
    java -jar /opt/alt/Manager.jar mod -g {{GROUP_LIST}} {{item}} 
  loop: {{USER_LIST}}
0

1 Answer 1

4

you should use filter product:

- name: add all users to all groups
  shell:
  cmd: |
    java -jar /opt/alt/Manager.jar mod -g {{item.1}} {{item.0}} 
  loop: {{USER_LIST | product(GROUP_LIST) | list}}

for example:

  tasks:
    - name: list to dict
      debug: 
        msg: "java -jar /opt/alt/Manager.jar mod -g {{item.1}} {{item.0}}"
      loop: "{{ USER_LIST | product(GROUP_LIST) | list }}"

gives:

ok: [localhost] => (item=['USER1', 'DATA']) => {
    "msg": "java -jar /opt/alt/Manager.jar mod -g DATA USER1"
}
ok: [localhost] => (item=['USER1', 'ONE']) => {
    "msg": "java -jar /opt/alt/Manager.jar mod -g ONE USER1"
}
ok: [localhost] => (item=['USER1', 'TWO']) => {
    "msg": "java -jar /opt/alt/Manager.jar mod -g TWO USER1"
}
ok: [localhost] => (item=['USER2', 'DATA']) => {
    "msg": "java -jar /opt/alt/Manager.jar mod -g DATA USER2"
}
ok: [localhost] => (item=['USER2', 'ONE']) => {
    "msg": "java -jar /opt/alt/Manager.jar mod -g ONE USER2"
}
ok: [localhost] => (item=['USER2', 'TWO']) => {
    "msg": "java -jar /opt/alt/Manager.jar mod -g TWO USER2"
}
ok: [localhost] => (item=['USER3', 'DATA']) => {
    "msg": "java -jar /opt/alt/Manager.jar mod -g DATA USER3"
}
ok: [localhost] => (item=['USER3', 'ONE']) => {
    "msg": "java -jar /opt/alt/Manager.jar mod -g ONE USER3"
}
ok: [localhost] => (item=['USER3', 'TWO']) => {
    "msg": "java -jar /opt/alt/Manager.jar mod -g TWO USER3"
}

following the ansible version, you have, no need to add | list

Sign up to request clarification or add additional context in comments.

4 Comments

Right, as in Ansible documentation also explained under Iterating over nested lists.
I had to add ``` | list ´´´ after product(GROUP_LIST). Otherwise the code worked fine! Cheers.
You should have an old version of ansible
Right, I'm using 2.9. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.