with_file is what I've been looking for

1,340 views
Skip to first unread message

Dylan Martin

unread,
Jan 10, 2013, 4:26:25 PM1/10/13
to ansible...@googlegroups.com
If you're learning to use ansible, as I am, you might find this helpful.

I just discovered that you can do stuff like this:

# pretend my_rpms.txt is a text file full of the names of rpm packages that I want to install, one name per line

- name: install some rpms from yum
  yum: name=$item
  with_file: my_rpms.txt

There's also a with_pipe and with_fileglob and all kinds of stuff in lib/ansible/runner/lookup_plugins/ in the ansible source tree. 

I didn't find this documented anywhere, so I thought I'd mention it in the list to help anyone else who is in the same situation as me.

Thanks!
-Dylan

Michael DeHaan

unread,
Jan 10, 2013, 4:33:12 PM1/10/13
to ansible...@googlegroups.com
Ouch, they don't seem to be documented on
http://ansible.cc/docs/playbooks2.html !

They should be.

I'll file a bug.

--Michael
> --
>
>

Yves Dorfsman

unread,
Jan 10, 2013, 6:32:15 PM1/10/13
to ansible...@googlegroups.com
On 2013-01-10 14:26, Dylan Martin wrote:
>
> - name: install some rpms from yum
> yum: name=$item
> with_file: my_rpms.txt
>

In this case does it run the rpm command once for each line in the file?
I am running into this situation with apt-get, running apt-get against a large
list of packages is much faster than running it once against each package,
also, you can solve dependency issues this way (if there is a choice of
dependency for a given package).

--
Yves. http://www.SollerS.ca/
http://ipv6.SollerS.ca
http://blog.zioup.org/

Yves Dorfsman

unread,
Jan 10, 2013, 6:32:59 PM1/10/13
to ansible...@googlegroups.com

Is there a way to make this option aware of comments, like only using lines
that do not start with a hash sign?

Dylan Martin

unread,
Jan 10, 2013, 7:17:22 PM1/10/13
to ansible...@googlegroups.com
The yum and apt modules have extra brains that lets them use $item
efficiently. So, it's not running yum a separate time for each
module.

My example was wrong: with_lines does what I described.

If you wanted to skip lines with a hash, you could probably use with_pipe like

with_pipe: grep -v '^#" my_rpms.txt

but I'm not sure if you'd need to json-ifty the output.
> --
>
>

Michael DeHaan

unread,
Jan 11, 2013, 8:15:21 AM1/11/13
to ansible...@googlegroups.com
On Thu, Jan 10, 2013 at 6:32 PM, Yves Dorfsman <yv...@zioup.com> wrote:
>
> Is there a way to make this option aware of comments, like only using lines
> that do not start with a hash sign?

It's not presently possible, though as I can't think of any reason why
this would ever be a bad thing the way it was originally intended (to
keep a package list as a separate file, for instance), I'll gladly
take patches to enable this.

Michael DeHaan

unread,
Jan 11, 2013, 8:23:38 AM1/11/13
to ansible...@googlegroups.com
FWIW, it seems it should also skip blank lines.

Brian Coca

unread,
Jan 11, 2013, 9:30:51 AM1/11/13
to ansible...@googlegroups.com
filter= option that defaults to blank lines and those that start with # or ;?

On Fri, Jan 11, 2013 at 8:23 AM, Michael DeHaan
<michael...@gmail.com> wrote:
> , it seems it should also skip blank lines.




--
Brian Coca
Stultorum infinitus est numerus
0110000101110010011001010110111000100111011101000010000001111001011011110111010100100000011100110110110101100001011100100111010000100001
Pedo mellon a minno

Michael DeHaan

unread,
Jan 11, 2013, 10:11:30 AM1/11/13
to ansible...@googlegroups.com
I'd make it default to ignoring blanks and "#".

Only worry about a filter arg if we need it later.
> --
>
>

Daniel Hokka Zakrisson

unread,
Jan 11, 2013, 11:21:11 AM1/11/13
to ansible...@googlegroups.com
Michael DeHaan wrote:
> I'd make it default to ignoring blanks and "#".
>
> Only worry about a filter arg if we need it later.

with_file only gets complete files. For processing line-based data, there
is with_lines which takes a command to execute, where you can use grep as
you see fit.

Daniel

> On Fri, Jan 11, 2013 at 9:30 AM, Brian Coca <bria...@gmail.com> wrote:
>> filter= option that defaults to blank lines and those that start with #
>> or ;?
>>
>> On Fri, Jan 11, 2013 at 8:23 AM, Michael DeHaan
>> <michael...@gmail.com> wrote:
>>> , it seems it should also skip blank lines.
>>
>>
>>
>>
>> --
>> Brian Coca
>> Stultorum infinitus est numerus
>> 0110000101110010011001010110111000100111011101000010000001111001011011110111010100100000011100110110110101100001011100100111010000100001
>> Pedo mellon a minno
>>
>> --
>>
>>
>
> --
>
>

Dylan Martin

unread,
Jan 11, 2013, 11:21:24 AM1/11/13
to ansible...@googlegroups.com
What I'd really like is some way to pipe with_X plugins through each
other. with_file = reads the contents of a file but doesn't output
json. with_lines = reads contents of a file and outputs json, one
item per line. with_pipe = puts output into one var. Should we write
with_pipe_lines to make with_pipe output json? Maybe something like

with:
- file: $filename
- grep: -v "^#"
- grep: -v "^[[:space:]]\*$"
- lines_to_json:

I've used imaginary with_grep and with_lines_to_json

On Fri, Jan 11, 2013 at 7:11 AM, Michael DeHaan
<michael...@gmail.com> wrote:
> I'd make it default to ignoring blanks and "#".
>
> Only worry about a filter arg if we need it later.
>
> On Fri, Jan 11, 2013 at 9:30 AM, Brian Coca <bria...@gmail.com> wrote:
>> filter= option that defaults to blank lines and those that start with # or ;?
>>
>> On Fri, Jan 11, 2013 at 8:23 AM, Michael DeHaan
>> <michael...@gmail.com> wrote:
>>> , it seems it should also skip blank lines.
>>
>>
>>
>>
>> --
>> Brian Coca
>> Stultorum infinitus est numerus
>> 0110000101110010011001010110111000100111011101000010000001111001011011110111010100100000011100110110110101100001011100100111010000100001
>> Pedo mellon a minno
>>
>> --
>>
>>
>
> --
>
>

Michael DeHaan

unread,
Jan 11, 2013, 11:22:52 AM1/11/13
to ansible...@googlegroups.com
On Fri, Jan 11, 2013 at 11:21 AM, Daniel Hokka Zakrisson
<dan...@hozac.com> wrote:
> Michael DeHaan wrote:
>> I'd make it default to ignoring blanks and "#".
>>
>> Only worry about a filter arg if we need it later.
>
> with_file only gets complete files. For processing line-based data, there
> is with_lines which takes a command to execute, where you can use grep as
> you see fit.

We know this.

However, executing grep is gross here, adds additional syntax, and we
will be smarter than this.

Michael DeHaan

unread,
Jan 11, 2013, 11:24:04 AM1/11/13
to ansible...@googlegroups.com
On Fri, Jan 11, 2013 at 11:21 AM, Dylan Martin
<Dylan....@seattlecolleges.edu> wrote:
> What I'd really like is some way to pipe with_X plugins through each
> other. with_file = reads the contents of a file but doesn't output
> json. with_lines = reads contents of a file and outputs json, one
> item per line. with_pipe = puts output into one var. Should we write
> with_pipe_lines to make with_pipe output json? Maybe something like
>
> with:
> - file: $filename
> - grep: -v "^#"
> - grep: -v "^[[:space:]]\*$"
> - lines_to_json:

If you want this, I'd suggest you write a plugin to do it, but it's
going to have to look a little different.

I am not interested in seeing this complication in core, and want to
see the number of concepts not continue to grow unbounded :)

Dylan Martin

unread,
Jan 11, 2013, 11:31:33 AM1/11/13
to ansible...@googlegroups.com
Roger. Sorry. Got carried away.

I keep wanting to do everything inside the playbook.
> --
>
>
Reply all
Reply to author
Forward
0 new messages