# conditional gziping of files

**URL:** <https://forum.ansible.com/t/conditional-gziping-of-files/12342>\
**Category:** Ansible Project\
**Created:** [February 12, 2013, 7:04am UTC](https://forum.ansible.com/t/conditional-gziping-of-files/12342 "2013-02-12T07:04:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kahlil.hodgson](https://avatars.discourse-cdn.com/v4/letter/k/b9bd4f/32.png) [@kahlil.hodgson](https://forum.ansible.com/u/kahlil.hodgson)\
**Post date:** [February 12, 2013, 7:04am UTC](https://forum.ansible.com/t/conditional-gziping-of-files/12342/1 "2013-02-12T07:04:53Z")

</div>

Hi All,

I'm using the following task to ensure static files have an up to date pre-compressed version (so nginx can serve them more quickly):

- name: 'ensure static files have been gziped'  
&nbsp;&nbsp;&nbsp;with\_lines:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- find $src\_path/root/static/ -regex '.\*\.\(css\|js\)$' || true  
&nbsp;&nbsp;&nbsp;shell: gzip -n -9 -c $item \> $item.gz;

This takes \_ages\_ to complete (like whole seconds). Ideally I'd only like to run the command if the modification time of $item is more recent than $item.gz. (Like a Makefile would).

I've tried something like

- name: 'ensure static files have been gziped'  
&nbsp;&nbsp;&nbsp;with\_lines:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- find $src\_path/root/static/ -regex '.\*\.\(css\|js\)$' || true  
&nbsp;&nbsp;&nbsp;shell: \>  
&nbsp;&nbsp;if [$item -nt $item.gz];  
&nbsp;&nbsp;then  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gzip -n -9 -c $item \> $item.gz;  
&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;true;  
&nbsp;&nbsp;fi

(needs to return true so the task doesn't fail)

This appears to work, but looks absolutely awful.

Any ideas on a better approach?

Cheers,

Kal

---

<div class="post-metadata">

**Author:** ![Romeo\_Theriault](https://avatars.discourse-cdn.com/v4/letter/r/839c29/32.png) [@Romeo\_Theriault](https://forum.ansible.com/u/Romeo_Theriault)\
**Post date:** [February 12, 2013, 9:35am UTC](https://forum.ansible.com/t/conditional-gziping-of-files/12342/2 "2013-02-12T09:35:07Z")

</div>

A few things here. For every 'with\_lines' you're making a new ssh  
connection to the box to gzip one item. You'd be much better off dropping  
the 'with\_lines' and just putting the whole thing in one shell call.  
Something like this: (untested psuedo-bash-code)

- name: 'ensure static files have been gziped'  
&nbsp;&nbsp;shell: find $src\_path/root/static/ -regex '.\*\.\(css\|js\)$' -print0 |  
xargs -0 -n 10 `if [-- -nt "--".gz] then gzip -n -9 -c "--".gz else true  
fi`

That way it's run in one ssh connection. There are probably cleaner/better  
shell ways to do it but you get the idea 🙂 (try to fit it in one ssh  
invocation). Another easy way I'd be tempted to deal with this is just  
create a nice little script for this and use ansible's 'script' module to  
push it down and run it or better yet create yourself a ansible module to  
do it! 🙂

---

<div class="post-metadata">

**Author:** ![kahlil.hodgson](https://avatars.discourse-cdn.com/v4/letter/k/b9bd4f/32.png) [@kahlil.hodgson](https://forum.ansible.com/u/kahlil.hodgson)\
**Post date:** [February 12, 2013, 10:42am UTC](https://forum.ansible.com/t/conditional-gziping-of-files/12342/3 "2013-02-12T10:42:58Z")

</div>

Hello Romeo,

> A few things here. For every 'with\_lines' you're making a new ssh  
> connection to the box to gzip one item. You'd be much better off  
> dropping the 'with\_lines' and just putting the whole thing in one shell  
> call.

Curiously, I'm actually running this as a 'local\_action' ... my thinking is definitely not right.

> Another easy way I'd be tempted to deal with  
> this is just create a nice little script for this and use ansible's  
> 'script' module to push it down and run it or better yet create yourself  
> a ansible module to do it! 🙂

I think I may be trying to use the wrong 'hammer' to hit this one. A module may be the right way to go. Basically I want to run some local pre-processing before I run a play. I could just wrap this in a little script, but where would it live? This pre-processing is intrinsically linked with the task, never used anywhere else. I don't to have to maintain a little independent pre-proc script that gets shared with the admins.

Wondering if a there is scope for a 'prep' module that does some pre-task local pre-processing, that is, runs a local script under, say,'./bin', and can be nicely bundled with the playbook.

Cheers,

Kal

---

<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:** [February 12, 2013, 2:05pm UTC](https://forum.ansible.com/t/conditional-gziping-of-files/12342/4 "2013-02-12T14:05:43Z")

</div>

This may actually be a great case for the 'script' module, which is a  
way of pushing and running an arbitrary script.

---

<div class="post-metadata">

**Author:** ![kahlil.hodgson](https://avatars.discourse-cdn.com/v4/letter/k/b9bd4f/32.png) [@kahlil.hodgson](https://forum.ansible.com/u/kahlil.hodgson)\
**Post date:** [February 12, 2013, 6:42pm UTC](https://forum.ansible.com/t/conditional-gziping-of-files/12342/5 "2013-02-12T18:42:20Z")

</div>

Excellent idea! Works brilliantly. I've added a corresponding pro-tip:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[https://coderwall.com/p/xlbxkq](https://coderwall.com/p/xlbxkq)

Thanks guys!

K
