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/ebe06b5e-5bb1-4f61-8d91-38bcb9f13c0c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.