简体   繁体   中英

ansible - using array of Variables and loop through it

I have to run steps like this around 20-25 times. How can I do with for loop (with_items).

I can have the the parameters URL1, Location1, pkg1.comamd1, $pkg1.command2 are pre defined and I can have them defined in the ansible play book. the Pkg1 value will be passed from the jenkins script

- get_url:
    url: "$URL1"
    dest: $Location1
  when: $Pkg1 != 'NONE' 
- Name : run the commands
  Shell: sh $pkg1.comamd1; sh $pkg1.command2
  when: Pkg1 != 'NONE' 

how can I create an array of variables and do it with_items

VarDetails {Pkg1, URL1, Location1, comamd1a, $command1b
            Pkg2, URL2, Location2, comamd2a, $command2b
            Pkg3, URL3, Location3, comamd3a, $command3b
            ....................
            ....................
            }    

I haven't tested but it must work with below reference example while using list of items.

- name: more complex items to add several users
  user:
    name: "{{ item.name }}"
    uid: "{{ item.uid }}"
    groups: "{{ item.groups }}"
    state: present
  with_items:
     - { name: testuser1, uid: 1002, groups: "wheel, staff" }
     - { name: testuser2, uid: 1003, groups: staff }

dont forget to add item before your variables by changing

url: "$URL1"
dest: $Location1

to

url: "item.url"
dest: "item.location"

and while referencing in with_items.. use your variable $URL1, $URL2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM