with_file is what I've been looking for

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

Ouch, they don't seem to be documented on
http://ansible.cc/docs/playbooks2.html !

They should be.

I'll file a bug.

--Michael

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).

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

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.

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.

FWIW, it seems it should also skip blank lines.

filter= option that defaults to blank lines and those that start with # or ;?

I'd make it default to ignoring blanks and "#".

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

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

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

We know this.

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

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 :slight_smile:

Roger. Sorry. Got carried away.

I keep wanting to do everything inside the playbook.