# remote file concatenation

**URL:** <https://forum.ansible.com/t/remote-file-concatenation/28408>\
**Category:** Ansible Project\
**Created:** [August 23, 2018, 6:22pm UTC](https://forum.ansible.com/t/remote-file-concatenation/28408 "2018-08-23T18:22:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dch](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dch/32/7132_2.png) [@dch](https://forum.ansible.com/u/dch)\
**Post date:** [August 23, 2018, 6:22pm UTC](https://forum.ansible.com/t/remote-file-concatenation/28408/1 "2018-08-23T18:22:39Z")

</div>

hi,

I've 3 files on a remote server after acme let's encrypt dns-01 process, that need to be concatenated in a specific order (private.key intermediate.cert public.cert ) for my TLS proxy, on the same system.

- the assemble module seems ideal but it doesn't enforce order, and it bundles a given directory, not specific files  
- using fetch module and jinja ends up with a local copy of a private key which I'd rather avoid  
- the easiest option (shell script) is not a way to learn new tricks in ansible 😃

Does anybody have suggestions on using some of the new features in loops to achieve this somehow?

[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_loops.html](https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html)

BTW this is what I currently tried; the copy/content block doesn't work yet as I've not figured out the correct way inside a jinja template to use {{ item }} inside the lookup:

- name: acme | fetch certificates for concatenation  
&nbsp;&nbsp;fetch:  
&nbsp;&nbsp;&nbsp;&nbsp;src: "/usr/local/etc/ssl/acme/{{ item }}"  
&nbsp;&nbsp;&nbsp;&nbsp;dest: "{{ config.domain }}/certs/{{ item }}"  
&nbsp;&nbsp;with\_items:  
&nbsp;&nbsp;&nbsp;&nbsp;- "{{ config.domain }}.crt"  
&nbsp;&nbsp;&nbsp;&nbsp;- "intermediate.crt"  
&nbsp;&nbsp;&nbsp;&nbsp;- "{{ config.domain }}.key"  
&nbsp;&nbsp;tags:  
&nbsp;&nbsp;&nbsp;&nbsp;- acme  
&nbsp;&nbsp;&nbsp;&nbsp;- concatenate

- name: acme | concatenate certs for haproxy  
&nbsp;&nbsp;copy:  
&nbsp;&nbsp;&nbsp;&nbsp;content: \>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ lookup('file', "{{ config.domain }}/certs/{{ item }}") }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ lookup('file', "{{ config.domain }}/certs/{{ item }}") }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ lookup('file', "{{ config.domain }}/certs/{{ item }}") }}  
&nbsp;&nbsp;&nbsp;&nbsp;dest: "/usr/local/etc/ssl/acme/{{ config.domain }}.all"  
&nbsp;&nbsp;&nbsp;&nbsp;mode: 0600  
&nbsp;&nbsp;&nbsp;&nbsp;owner: root..  
&nbsp;&nbsp;&nbsp;&nbsp;group: wheel  
&nbsp;&nbsp;with\_items:  
&nbsp;&nbsp;&nbsp;&nbsp;- "{{ config.domain }}.crt"  
&nbsp;&nbsp;&nbsp;&nbsp;- "intermediate.crt"  
&nbsp;&nbsp;&nbsp;&nbsp;- "{{ config.domain }}.key"  
&nbsp;&nbsp;tags:  
&nbsp;&nbsp;&nbsp;&nbsp;- acme  
&nbsp;&nbsp;&nbsp;&nbsp;- concatenate

thanks!

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [August 23, 2018, 9:19pm UTC](https://forum.ansible.com/t/remote-file-concatenation/28408/2 "2018-08-23T21:19:37Z")

</div>

> hi,
> 
> I've 3 files on a remote server after acme let's encrypt dns-01 process, that need to be concatenated in a specific order (private.key intermediate.cert public.cert ) for my TLS proxy, on the same system.
> 
> - the assemble module seems ideal but it doesn't enforce order,

It sort of does it's string sorting order.

> and it bundles a given directory, not specific files

You have regexp to filter out only the files you want, but yes they need to be in the same directory.

> - using fetch module and jinja ends up with a local copy of a private key which I'd rather avoid

You have slurp module, then the information is only in memory, but when you use copy module afterwards it might touch the disk, I'm not sure how Ansible is does it.

> - the easiest option (shell script) is not a way to learn new tricks in ansible 😃

Sometimes that is you best and/or only option 😉

> Does anybody have suggestions on using some of the new features in loops to achieve this somehow?

I would probably just do

- shell: cat {{ config.domain }}.crt intermediate.crt {{ config.domain }}.key \>/usr/local/etc/ssl/acme/{{ config.domain }}.all  
&nbsp;&nbsp;args:  
&nbsp;&nbsp;&nbsp;&nbsp;chdir: /usr/local/etc/ssl/acme

> BTW this is what I currently tried; the copy/content block doesn't work yet as I've not figured out the correct way inside a jinja template to use {{ item }} inside the lookup:
> 
> - name: acme | fetch certificates for concatenation  
> &nbsp;&nbsp;fetch:  
> &nbsp;&nbsp;&nbsp;&nbsp;src: "/usr/local/etc/ssl/acme/{{ item }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;dest: "{{ config.domain }}/certs/{{ item }}"  
> &nbsp;&nbsp;with\_items:  
> &nbsp;&nbsp;&nbsp;&nbsp;- "{{ config.domain }}.crt"  
> &nbsp;&nbsp;&nbsp;&nbsp;- "intermediate.crt"  
> &nbsp;&nbsp;&nbsp;&nbsp;- "{{ config.domain }}.key"  
> &nbsp;&nbsp;tags:  
> &nbsp;&nbsp;&nbsp;&nbsp;- acme  
> &nbsp;&nbsp;&nbsp;&nbsp;- concatenate
> 
> - name: acme | concatenate certs for haproxy  
> &nbsp;&nbsp;copy:  
> &nbsp;&nbsp;&nbsp;&nbsp;content: \>  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ lookup('file', "{{ config.domain }}/certs/{{ item }}") }}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ lookup('file', "{{ config.domain }}/certs/{{ item }}") }}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ lookup('file', "{{ config.domain }}/certs/{{ item }}") }}

You can't use {{}} inside {{}} since you already are in template mode, so you need to use concatenation.

&nbsp;&nbsp;{{ lookup('file', config.domain ~ '/certs/' ~ item) }}

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [August 23, 2018, 10:02pm UTC](https://forum.ansible.com/t/remote-file-concatenation/28408/3 "2018-08-23T22:02:38Z")

</div>

You can also create dir with symlinks to each file, have the symlink  
name keep the order you want 01, 02, 03, then run assemble module.
