Build file from two other files

Visto 14 veces
Saltar al primer mensaje no leído

Andy Smith

no leída,
8 dic 2018, 8:30:108/12/18
a ansible...@googlegroups.com
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
when: thing_names is defined
loop: "{{ thing_names | flatten(levels=1) }}"
loop_control:
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
copy:
src: "{{ thing_name }}/foo"
dest: /etc/things/{{ thing_name }}/foo
mode: 0644

- name: Distribute bar files for each thing
no_log: true
copy:
src: "{{ thing_name }}/bar"
dest: /etc/things/{{ thing_name }}/bar
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
register: foo_changed
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

Andy Smith

no leída,
8 dic 2018, 8:49:018/12/18
a ansible...@googlegroups.com
On Sat, Dec 08, 2018 at 01:30:00PM +0000, Andy Smith wrote:
> Probably I missed some really simple way to just concat a bunch of
> files together and send that over. Is there something like that?

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

- name: Rebuild foobar file
when: foo_changed.changed or bar_changed.changed
no_log: true
copy:
content: "{{ lookup('file', '/etc/things/' + thing_name + '/foo') + '\n' + lookup('file', '/etc/things/' + thing_name + '/bar') + '\n' }}"
dest: /etc/things/{{ thing_name }}/foobar
mode: 0640

Is that the best way?

Thanks,
Andy
Responder a todos
Responder al autor
Reenviar
0 mensajes nuevos