0

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

0

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.

| | |
  • Any Other way i can achieve this? Since i dont want to create new directory. – celcoprab Sep 16 at 7:21
  • It didn't create the wanted result anyway, since the result still contained duplicate entries. I now posted a working variant. – Gerald Schneider Sep 16 at 7:30
  • These 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 Sep 16 at 7:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.