# Build file from two other files

**URL:** <https://forum.ansible.com/t/build-file-from-two-other-files/29225>\
**Category:** Ansible Project\
**Created:** [December 8, 2018, 1:30pm UTC](https://forum.ansible.com/t/build-file-from-two-other-files/29225 "2018-12-08T13:30:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andy\_Smith](https://avatars.discourse-cdn.com/v4/letter/a/898d66/32.png) [@Andy\_Smith](https://forum.ansible.com/u/Andy_Smith)\
**Post date:** [December 8, 2018, 1:30pm UTC](https://forum.ansible.com/t/build-file-from-two-other-files/29225/1 "2018-12-08T13:30:00Z")

</div>

Hi,

I feel I must be missing something very obvious here, but I can't  
find it, so here we go.

Let's say I've got a role's tasks/main.yml that includes some other  
tasks on a loop:

- include\_tasks: distribute\_thing.yml  
&nbsp;&nbsp;when: thing\_names is defined  
&nbsp;&nbsp;loop: "{{ thing\_names | flatten(levels=1) }}"  
&nbsp;&nbsp;loop\_control:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop\_var: thing\_name

thing\_names is just a list like

- alpha  
- bravo  
- …

now in distribute\_thing.yml for each thing\_name I want to copy  
several files:

- name: Distribute foo files for each thing  
&nbsp;&nbsp;copy:  
&nbsp;&nbsp;&nbsp;&nbsp;src: "{{ thing\_name }}/foo"  
&nbsp;&nbsp;&nbsp;&nbsp;dest: /etc/things/{{ thing\_name }}/foo  
&nbsp;&nbsp;&nbsp;&nbsp;mode: 0644

- name: Distribute bar files for each thing  
&nbsp;&nbsp;no\_log: true  
&nbsp;&nbsp;copy:  
&nbsp;&nbsp;&nbsp;&nbsp;src: "{{ thing\_name }}/bar"  
&nbsp;&nbsp;&nbsp;&nbsp;dest: /etc/things/{{ thing\_name }}/bar  
&nbsp;&nbsp;&nbsp;&nbsp;mode: 0640

Fine so far. But now I also want a file /etc/things/{{ thing\_name  
}}/foobar which is the combined content of foo and bar. I would only  
like to bother sending it over if either the foo or the bar task  
changed.

What is the simplest way to do it?

I don't care if the content comes from the control host or the  
target.

At first I thought a register variable on each of the foo and bar  
tasks like:

- name: Distribute foo files for each thing  
&nbsp;&nbsp;register: foo\_changed  
&nbsp;&nbsp;copy: …

then a template file like:

{{ foo\_changed['diff'][0]['after'] }}  
{{ bar\_changed['diff'][0]['after'] }}

to just concat those two register variables together. That does  
actually work for foo, but not bar, because bar has no diff content  
owing to the no\_log: true, which is necessary because the file  
content of bar is sensitive and shouldn't be logged.

Probably I missed some really simple way to just concat a bunch of  
files together and send that over. Is there something like that?

Thanks,  
Andy

---

<div class="post-metadata">

**Author:** ![Andy\_Smith](https://avatars.discourse-cdn.com/v4/letter/a/898d66/32.png) [@Andy\_Smith](https://forum.ansible.com/u/Andy_Smith)\
**Post date:** [December 8, 2018, 1:48pm UTC](https://forum.ansible.com/t/build-file-from-two-other-files/29225/2 "2018-12-08T13:48:51Z")

</div>

Typical, as soon as I ask the question I find a way to do it:

- name: Rebuild foobar file  
&nbsp;&nbsp;when: foo\_changed.changed or bar\_changed.changed  
&nbsp;&nbsp;no\_log: true  
&nbsp;&nbsp;copy:  
&nbsp;&nbsp;&nbsp;&nbsp;content: "{{ lookup('file', '/etc/things/' + thing\_name + '/foo') + '\n' + lookup('file', '/etc/things/' + thing\_name + '/bar') + '\n' }}"  
&nbsp;&nbsp;&nbsp;&nbsp;dest: /etc/things/{{ thing\_name }}/foobar  
&nbsp;&nbsp;&nbsp;&nbsp;mode: 0640

Is that the best way?

Thanks,  
Andy
