I wonder if you might be better off using include_vars to load all of your 
groups details in one pass, without multiple lookup calls.

http://docs.ansible.com/ansible/include_vars_module.html


There's a little bit more syntax to make your csv into yaml.  Instead of...
accounts,502,present,no
engineering,504,present,no

You'd write:

groups:
 - {name: accounts, gid: 502, state: present, system: no}
 - {name: engineering, gid: 504, state: present, system: no}

or 

--- 
groups: 
  - 
    gid: 502
    name: accounts
    state: present
    system: false
  - 
    gid: 504
    name: engineering
    state: present
    system: false


Then your playbook might look like:

     - name: load groups one
    include_vars: file=groups1.yml

  - name: Process groups one
    group: 
      name: {{ item.name }}
      gid: {{ item.gid }}
      state: {{ item.state }}
      system: {{ item.state }}
    with_items: "{{ groups }}"
    ignore_errors: True


If you still prefer your csv syntax, you might want to change to the key: 
value style instead of the key=value style as this preserves type 
information in yaml (so ints stay as ints, booleans as booleans etc).

Hope this helps,

Jon


On Tuesday, October 4, 2016 at 8:54:34 AM UTC+1, Asil Carlin wrote:
>
> Hi Kai,
>
> Thanks for taking the time to reply. I did first try using with_lines and 
> the awk command and using "item +", that throws up another error: This code
>
> ---
>
> - hosts: localhost
>   become: True
>   become_user: root
>
>   tasks:
>
>   - name: get groups
>     command: /usr/bin/awk -F',' '!/^#/ && !/^$/ { print $1 }' /var/tmp/
> groups.csv
>     register: groups_out
>
>   - debug: var=groups_out.stdout_lines
>
>   - name: Process groups one
>     group: >
>       name={{ lookup('csvfile', item + 'file=groups.csv col=0 delimiter=,'
> ) }}
>       gid={{ lookup('csvfile', item + 'file=groups.csv col=1 delimiter=,') 
> }}
>       state={{ lookup('csvfile', item + 'file=groups.csv col=2 
> delimiter=,') }}
>       system={{ lookup('csvfile', item + 'file=groups.csv col=3 
> delimiter=,') }}
>     with_items: "{{ groups_out.stdout_lines }}"
>     ignore_errors: True
>
>   - name: Process groups two
>     group: >
>       name={{ lookup('csvfile', item + 'file=groups.csv col=0 delimiter=,'
> ) }}
>       gid={{ lookup('csvfile', item + 'file=groups.csv col=1 delimiter=,') 
> }}
>       state={{ lookup('csvfile', item + 'file=groups.csv col=2 
> delimiter=,') }}
>       system={{ lookup('csvfile', item + 'file=groups.csv col=3 
> delimiter=,') }}
>     with_lines: /usr/bin/awk -F',' '!/^#/ && !/^$/ { print $1 }' /var/tmp/
> groups.csv
>
> with this csv:
>
> accounts,502,present,no
> engineering,504,present,no
>
> Both tasks complain about ansible.csv not existing in my CWD.
>
> fatal: [localhost]: FAILED! => {"failed": true, "msg": "csvfile: [Errno 2] 
> No such file or directory: u'/var/tmp/ansible.csv'"}
>
> No. If I touch /var/tmp/ansible.csv then the we're back to the following:
>
> failed: [localhost] (item=accounts) => {"failed": true, "item": "accounts"
> , "msg": "argument system is of type <type 'list'> and we were unable to 
> convert to bool"}
> failed: [localhost] (item=engineering) => {"failed": true, "item": 
> "engineering", "msg": "argument system is of type <type 'list'> and we 
> were unable to convert to bool"}
>
> To add, if I were to use the following syntax of "item":
>
> lookup('csvfile', 'item file=groups.csv col=0 delimiter=,') }}
>
> Then ansible doesn't look for a file called ansible.csv in my cwd and we 
> get the same "...unable to convert to bool" message.
>
> /bin/sh exists and is a link to /bin/bash.
>
> Assuming I've understood all the advice given, should I file a bug report? 
> Can anyone else reproduce this?
>
> Thanks and regards,
>
> Asil
>
>
> On Monday, October 3, 2016 at 7:41:57 PM UTC+1, Kai Stian Olstad wrote:
>>
>> On 03. okt. 2016 11:18, Asil Carlin wrote: 
>> > Hi, 
>> > 
>> > I'm trying to use csvfile lookups to populate values in the groups 
>> module, 
>> > but not having much luck. My csvfile: 
>> > 
>> > # groups.csv 
>> > # name, gid [optional - leave blank], state [present|absent], system 
>> > [yes|no] 
>> >  accounts,502,present,no 
>> > engineering,504,present,no 
>> > 
>> > The playbook: 
>> > 
>> > --- 
>> > 
>> > - hosts: localhost 
>> >   become: True 
>> >   become_user: root 
>> > 
>> >   tasks: 
>> > 
>> >   - name: get groups 
>> >     command: /usr/bin/awk -F',' '!/^#/ && !/^$/ { print $1 }' 
>> groups.csv 
>> >     register: groups_out 
>> > 
>> >   - name: Process groups 
>> >     group: > 
>> >       name="{{ lookup('csvfile', 'item file=groups.csv col=0') }}" 
>> >       gid="{{ lookup('csvfile', 'item file=groups.csv col=1') }}" 
>> >       state="{{ lookup('csvfile', 'item file=groups.csv col=2') }}" 
>> >       system="{{ lookup('csvfile', 'item file=groups.csv col=3') }}" 
>>
>> As you mention in a later post you are missing the delimiter, TAB is the 
>> default. 
>>
>> And you key is literally "item" on all you lookup. To use the variable 
>> item you'll have to concatenate like this. 
>>
>>    lookup('csvfile', item + ' file=groups.csv delimiter=, col=n') 
>>
>>
>> >     # with_lines: "/usr/bin/awk -F',' '!/^#/ && !/^$/ { print $1 }' 
>> > groups.csv" 
>> >     # with_items: "{{ groups_out.stdout_lines }}" 
>> >     with_lines: "{{ groups_out.stdout_lines }}" 
>>
>> You can't only have a variable in with_items, it must be a command 
>>
>> https://docs.ansible.com/ansible/playbooks_loops.html#iterating-over-the-results-of-a-program-execution
>>  
>>
>> > 
>> >  Using with_lines and groups_out.stdout_lines gives me: 
>> > 
>> > TASK [Process groups] 
>> > ********************************************************** 
>> > /bin/sh: accounts: command not found 
>>
>> You don't have /bin/sh, that can be a problem, since default, Ansible is 
>> depending on it. 
>>
>> -- 
>> Kai Stian Olstad 
>>
>

-- 
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].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/e9751fc8-2f31-4f93-801b-499aefdbf0c2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to