How to merge two files into single file using ansible in remote server
6,308
This works. It reads the contents of all files and reduces the resulting array of lines to unique values. Then a new file with those lines is created.
- hosts: localhost
gather_facts: no
vars:
hostsfiles:
- /tmp/hosts1
- /tmp/hosts2
tasks:
- name: read files
command: awk 1 {{ hostsfiles | join(' ') }}
register: hosts_contents
- name: create hosts file
copy:
dest: /tmp/hosts
content: "{{ hosts_contents.stdout_lines | unique |join('\n') }}"
I'm using awk 1
instead of cat
to add potentially missing line breaks to the end of the source files.
Author by
celcoprab
Updated on September 18, 2022Comments
-
celcoprab over 1 year
I need to merge two files without duplicate entries in it. is there any way i can achieve it through ansible modules. Ex i have two files /etc/hosts1 and /etc/hosts2. I need to have one /etc/hosts file with all entries present in both /etc/hosts1 and /etc/hosts2 without duplicate entries. How can i achieve this. An example would be appreciated
- name: Merge two files assemble: src: /etc/hosts1 dest: /etc/hosts2
The above assemble module fails
-
celcoprab over 3 yearsAny Other way i can achieve this? Since i dont want to create new directory.
-
dortegaoh over 3 yearsIt didn't create the wanted result anyway, since the result still contained duplicate entries. I now posted a working variant.
-
celcoprab over 3 yearsThese are my entries cat /etc/hosts_1 A B C D E F G H I J K cat /etc/hosts_2 12345678 I followed your command but its not merging or appending to the file. cat /etc/hosts A B C D E F G H I J K
-
celcoprab over 3 yearscan i merge three files
-
celcoprab over 3 yearsread files task gives error when any of the file is not is present. How to avoid that
-
dortegaoh over 3 yearsIf you have follow up questions, you should ask new questions for them. Provide your playbook in the state it currently is, so someone can give you a matching answer.