Search code examples
ansiblelocalhostremote-servermountnfs

ansible mount a remote nfs share to the localhost server running ansible playbook


When running ansible.posix.mount, an ssh connection is created to the src, then the path directory is created on said server. I want to be able to just mount nfs share from a remote server locally on the box running the ansible playbook.

I understand that the second task in my playbook below is performing said task on any hosts in my inventory under "nfs-server", however I need to include that host as it is the destination ip variable I need to point the mount share at. I do not want to have to add IP address destinations to my settings.yml, would rather iterate via the inventory file if possible.

What am I missing?

Here is my playbook:

- name: Create directory
  hosts: localhost
  gather_facts: False

  tasks:
    - name: Include vars
      include_vars:
          file: settings.yaml
          name: settings

    - name: Create local directory "nfs-ansible"
      file:
        path: ./nfs-ansible
        state: directory
      delegate_to: localhost

- name: Mount nfs share to localhost
  hosts: nfs-server
  gather_facts: False
  become: yes

  tasks:
    - name: Include idrac vars
      include_vars:
          file: settings.yaml
          name: settings

    - name: Mount share from nfs-server
      ansible.posix.mount:
        src: "{{ hostvars[inventory_hostname].ipaddress }}:/nfs-share"
        path: ./nfs-ansible
        state: mounted
        fstype: nfs

Solution

  • As best I can tell from your question, you don't actually want to run against the nfs-server group, you merely want to use its ipaddress hostvar to mount locally. If that understanding is correct:

    - name: Create directory
      hosts: localhost
      gather_facts: False
      tasks:
        - name: Include vars
          include_vars:
              file: settings.yaml
              name: settings
    
        - name: Create local directory "nfs-ansible"
          file:
            path: ./nfs-ansible
            state: directory
          # delegate to is redundant in a playbook targeting "localhost" to begin with
          # delegate_to: localhost
    
        - name: Mount share from nfs-server
          become: yes
          ansible.posix.mount:
            src: "{{ hostvars[nfs_server0].ipaddress }}:/nfs-share"
            path: ./nfs-ansible
            state: mounted
            fstype: nfs
          vars:
            nfs_server0: '{{ groups["nfs-server"] | first }}'