# with\_file is what I've been looking for

**URL:** <https://forum.ansible.com/t/with-file-is-what-ive-been-looking-for/12194>\
**Category:** Ansible Project\
**Created:** [January 10, 2013, 9:26pm UTC](https://forum.ansible.com/t/with-file-is-what-ive-been-looking-for/12194 "2013-01-10T21:26:25Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dylan\_Martin](https://avatars.discourse-cdn.com/v4/letter/d/8797f3/32.png) [@Dylan\_Martin](https://forum.ansible.com/u/Dylan_Martin)\
**Post date:** [January 10, 2013, 9:26pm UTC](https://forum.ansible.com/t/with-file-is-what-ive-been-looking-for/12194/1 "2013-01-10T21:26:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [January 10, 2013, 9:33pm UTC](https://forum.ansible.com/t/with-file-is-what-ive-been-looking-for/12194/2 "2013-01-10T21:33:12Z")

</div>

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

They should be.

I'll file a bug.

--Michael

---

<div class="post-metadata">

**Author:** ![Yves\_Dorfsman](https://avatars.discourse-cdn.com/v4/letter/y/9de0a6/32.png) [@Yves\_Dorfsman](https://forum.ansible.com/u/Yves_Dorfsman)\
**Post date:** [January 10, 2013, 11:32pm UTC](https://forum.ansible.com/t/with-file-is-what-ive-been-looking-for/12194/3 "2013-01-10T23:32:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Yves\_Dorfsman](https://avatars.discourse-cdn.com/v4/letter/y/9de0a6/32.png) [@Yves\_Dorfsman](https://forum.ansible.com/u/Yves_Dorfsman)\
**Post date:** [January 10, 2013, 11:32pm UTC](https://forum.ansible.com/t/with-file-is-what-ive-been-looking-for/12194/4 "2013-01-10T23:32:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Dylan\_Martin1](https://avatars.discourse-cdn.com/v4/letter/d/ed655f/32.png) [@Dylan\_Martin1](https://forum.ansible.com/u/Dylan_Martin1)\
**Post date:** [January 11, 2013, 12:17am UTC](https://forum.ansible.com/t/with-file-is-what-ive-been-looking-for/12194/5 "2013-01-11T00:17:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [January 11, 2013, 1:15pm UTC](https://forum.ansible.com/t/with-file-is-what-ive-been-looking-for/12194/6 "2013-01-11T13:15:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [January 11, 2013, 1:23pm UTC](https://forum.ansible.com/t/with-file-is-what-ive-been-looking-for/12194/7 "2013-01-11T13:23:38Z")

</div>

FWIW, it seems it should also skip blank lines.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [January 11, 2013, 2:30pm UTC](https://forum.ansible.com/t/with-file-is-what-ive-been-looking-for/12194/8 "2013-01-11T14:30:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [January 11, 2013, 3:11pm UTC](https://forum.ansible.com/t/with-file-is-what-ive-been-looking-for/12194/9 "2013-01-11T15:11:30Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![Daniel\_Hokka\_Zakriss](https://avatars.discourse-cdn.com/v4/letter/d/f05b48/32.png) [@Daniel\_Hokka\_Zakriss](https://forum.ansible.com/u/Daniel_Hokka_Zakriss)\
**Post date:** [January 11, 2013, 4:21pm UTC](https://forum.ansible.com/t/with-file-is-what-ive-been-looking-for/12194/10 "2013-01-11T16:21:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Dylan\_Martin1](https://avatars.discourse-cdn.com/v4/letter/d/ed655f/32.png) [@Dylan\_Martin1](https://forum.ansible.com/u/Dylan_Martin1)\
**Post date:** [January 11, 2013, 4:21pm UTC](https://forum.ansible.com/t/with-file-is-what-ive-been-looking-for/12194/11 "2013-01-11T16:21:24Z")

</div>

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:  
&nbsp;&nbsp;- file: $filename  
&nbsp;&nbsp;- grep: -v "^#"  
&nbsp;&nbsp;- grep: -v "^[[:space:]]\*$"  
&nbsp;&nbsp;- lines\_to\_json:

I've used imaginary with\_grep and with\_lines\_to\_json

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [January 11, 2013, 4:22pm UTC](https://forum.ansible.com/t/with-file-is-what-ive-been-looking-for/12194/12 "2013-01-11T16:22:52Z")

</div>

We know this.

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

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [January 11, 2013, 4:24pm UTC](https://forum.ansible.com/t/with-file-is-what-ive-been-looking-for/12194/13 "2013-01-11T16:24:04Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![Dylan\_Martin1](https://avatars.discourse-cdn.com/v4/letter/d/ed655f/32.png) [@Dylan\_Martin1](https://forum.ansible.com/u/Dylan_Martin1)\
**Post date:** [January 11, 2013, 4:31pm UTC](https://forum.ansible.com/t/with-file-is-what-ive-been-looking-for/12194/14 "2013-01-11T16:31:33Z")

</div>

Roger. Sorry. Got carried away.

I keep wanting to do everything inside the playbook.
