There are (at least) two ways to do it that both seem to work as expected:
[will@tangerine test]$ cat var_array.yml
---
- hosts: 127.0.0.1
connection: local
vars:
- array1: ['abcd', 'efgh', 'jhkl']
- array2:
- mnop
- qrst
- uvwy
tasks:
- name: print contents of array1
action: debug msg="{{item}}"
with_items: array1
- name: print contents of array2
action: debug msg="{{item}}"
with_items: array2
[will@tangerine test]$ ansible-playbook var_array.yml
PLAY [127.0.0.1]
**************************************************************
GATHERING FACTS
***************************************************************
ok: [127.0.0.1]
TASK: [print contents of array1]
**********************************************
ok: [127.0.0.1] => (item=abcd) => {
"item": "abcd",
"msg": "abcd"
}
ok: [127.0.0.1] => (item=efgh) => {
"item": "efgh",
"msg": "efgh"
}
ok: [127.0.0.1] => (item=jhkl) => {
"item": "jhkl",
"msg": "jhkl"
}
TASK: [print contents of array2]
**********************************************
ok: [127.0.0.1] => (item=mnop) => {
"item": "mnop",
"msg": "mnop"
}
ok: [127.0.0.1] => (item=qrst) => {
"item": "qrst",
"msg": "qrst"
}
ok: [127.0.0.1] => (item=uvwy) => {
"item": "uvwy",
"msg": "uvwy"
}
PLAY RECAP
********************************************************************
127.0.0.1 : ok=3 changed=0 unreachable=0
failed=0
On Thursday, December 12, 2013 3:38:42 AM UTC+10, Michael Baydoun wrote:
>
> I previous tried to define the array at the begining of the playbook in
> the vars section, but it only accepts key/value pairs, not an array. Maybe
> my syntax was incorrect. Is there an example of an array being defined in
> the vars section somewhere?
>
> On Wednesday, December 11, 2013 11:53:44 AM UTC-5, David Karban wrote:
>>
>> Hi,
>>
>> either define variable on the beggining of the play like here:
>>
>> http://www.ansibleworks.com/docs/playbooks_variables.html#variables-defined-in-a-playbook
>>
>> or, better use group vars, or host vars:
>>
>> http://www.ansibleworks.com/docs/playbooks_best_practices.html#group-and-host-variables
>>
>> David
>>
>>
>>
>> 2013/12/11 Michael Baydoun <[email protected]>
>>
>>> Can the following be refactored so the following array only appears once
>>> in the code?
>>>
>>> - [ '/home/youruser/a', '/home/youruser/b', '/home/youruser/c',
>>> '/home/youruser/d' ]
>>>
>>>
>>> ---
>>>
>>> - hosts: yourhost
>>>
>>> user: root
>>>
>>> gather_facts: no
>>>
>>> tasks:
>>>
>>> - lineinfile: dest={{ item[0] }}/.gitignore create=yes regexp='{{
>>> item[1] }}' line='*.{{ item[1] }}'
>>>
>>> with_nested:
>>>
>>> - [ '/home/youruser/a', '/home/youruser/b', '/home/youruser/c',
>>> '/home/youruser/d' ]
>>>
>>> - [ 'lck', 'log', 'Log' ]
>>>
>>> - shell: /usr/bin/git init chdir={{ item[0] }}
>>>
>>> with_items:
>>>
>>> - [ '/home/youruser/a', '/home/youruser/b', '/home/youruser/c',
>>> '/home/youruser/d' ]
>>>
>>> - shell: /usr/bin/git add -A ; /usr/bin/git commit -m 'auto'; exit 0
>>> chdir={{ item[0] }}
>>>
>>> with_items:
>>>
>>> - [ '/home/youruser/a', '/home/youruser/b', '/home/youruser/c',
>>> '/home/youruser/d' ]
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Ansible Project" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to [email protected].
>>> For more options, visit https://groups.google.com/groups/opt_out.
>>>
>>
>>
>>
>> --
>> David Karban
>> Specialista na správu linuxových serverů
>> www.karban.eu
>>
>
--
You received this message because you are subscribed to the Google Groups
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.