heavyguidence
heavyguidence

Reputation: 373

Ansible mount for different directories on different servers

we have different mount points configured in /etc/fstab file for different servers. How can i achieve the same using Ansible?

For example:

server A has a source node1:/data/col1/RMAN mounted at path /mountPointA

Above can we achieved using Ansible's mount module such as


- name: Add Mount Points
mount:
  path: /mnt/dvd
  src : /dev/sr0
  fstype: iso09660
  opts: ro
  boot: yes
  state: present

BUT

What if i have another server "B" which has source /node1:/data/col1/directoryB needs to be mounted at path /mountPointB. However this server does not need the first mount point to be configured.

Is it possible to achieve it in single yml file?

In other words

Host      source                   dest

hostA    /source/directoryA     /mnt

hostB    /source/directoryB     /mnt or /mnt/subdirectory #assuming subdir exists

i hope this make sense. sorry for the confusion. This playbook will be run on bulk of hosts, how do i make sure the right host is automatically selected to be using the right mount points

Upvotes: 0

Views: 4361

Answers (1)

larsks
larsks

Reputation: 311606

There are a number of ways to approach this. I'm going to suggest an approach that would let you defined multiple mountpoints per host, if you so desired. If you have a host_vars directory in the same place as your playbook, Ansible will look there for files named after your hosts and load variables from those files. For example, if you have a host in your inventory named serverA, Ansible would look for host_vars/serverA.yml.

We're going to take advantage of that to specify per-host mount configurations. Create a file host_vars/serverA.yml containing:

mountinfo:
  - src: node1:/data/col1/RMAN
    dst: /mountpointA
    fstype: nfs

And create host_vars/serverB.yml with:

mountinfo:
  - src: node1:/data/col1/directoryB
    dst: /mountpointB
    fstype: nfs

And then in your playbook:

- name: Add Mount Points
  mount:
    path: "{{ item.dst }}"
    src : "{{ item.src }}"
    fstype: "{{ item.fstype }}"
    opts: ro
    boot: yes
    state: present
  with_items: "{{ mountinfo }}"
  when: mountinfo is defined

In case something in this answer wasn't clear I've wrapped up the above as a runnable example here.

Upvotes: 2

Related Questions