Add servers to /etc/exports with Ansible

Viewed 495

I'm trying to get ansible to add lines to /etc/exports for each value in the {{ips}} variable and not have them all on the same line.

I have tried a few different ways including lineinfile, blockinfile and replace seems to get me closer than the others, but I am still not able to get the results I want.

For certain servers I have them being added in this format

/foo/bar server1.com (rw,sync,no_root_squash)
/foo/bar server2.com (rw,sync,no_root_squash)
/foo/bar server3.com (rw,sync,no_root_squash)

using this code.

- name: Add New server to /etc/exports
lineinfile:
  path: /etc/exports
  insertafter: "# This Line"
  line: "/foo/bar {{ item }}(rw,sync,no_root_squash)"
loop: "{{ ips.split(',') }}"

For other servers I need to have them in this format and I cannot seem to get it working.

/foo/bar server1(rw,sync,no_root_squash) server2(rw,sync,no_root_squash) server3(rw,sync,no_root_squash)
1 Answers

I figured out a way to get what I was looking for. If anyone knows of a better way I am open to suggestions, but so far this works.

  - name: Add Servers to /etc/exports
    lineinfile:
      path: /etc/exports
      insertafter: "# This Line"
      line: "/foo/bar {{ ips.split(',') | join('(rw,sync,no_root_squash) ') }}(rw,sync,no_root_squash)"

This add the servers in the ips variable in this format

/foo/bar server1(rw,sync,no_root_squash) server2(rw,sync,no_root_squash) server3(rw,sync,no_root_squash)
Related