VMware Cloud Community
puntjuh
Contributor
Contributor

Ansible playbook NFS datastore mounting

Hello,

I've created a playbook to mount NFS datastores to my ESX hosts/vCenter. When i provide the hostname of my ESX hosts it will work without issue, however, if i provide it through an inventory file it fails with no hosts matched. 

Replacing the variable " {{ inventory_hostname }} " with a hostname will make the script work. 

 

the command run is: 

ansible-playbook -i inventory/esx_host.ini -l esxhosts mount_nfs_datastore.yml -vvvvvv 

The resultant verbose output: 

ansible-playbook [core 2.13.2]
  config file = /usr/home/admin.dimitri/ansible.cfg
  configured module search path = ['/home/admin.dimitri/.ansible/plugins/modules', '/usr/local/share/py39-ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.9/site-packages/ansible
  ansible collection location = /home/admin.dimitri/.ansible/collections:/usr/local/share/py39-ansible/collections
  executable location = /usr/local/bin/ansible-playbook
  python version = 3.9.15 (main, Dec 12 2022, 11:28:09) [Clang 13.0.0 (git@github.com:llvm/llvm-project.git llvmorg-13.0.0-0-gd7b669b3a
  jinja version = 3.1.2
  libyaml = True
Using /usr/home/admin.dimitri/ansible.cfg as config file
setting up inventory plugins
host_list declined parsing /usr/home/admin.dimitri/inventory/esx_host.ini as it did not pass its verify_file() method
script declined parsing /usr/home/admin.dimitri/inventory/esx_host.ini as it did not pass its verify_file() method
auto declined parsing /usr/home/admin.dimitri/inventory/esx_host.ini as it did not pass its verify_file() method
yaml declined parsing /usr/home/admin.dimitri/inventory/esx_host.ini as it did not pass its verify_file() method
Parsed /usr/home/admin.dimitri/inventory/esx_host.ini inventory source with ini plugin
Loading collection community.vmware from /home/admin.dimitri/.ansible/collections/ansible_collections/community/vmware
Loading callback plugin default of type stdout, v2.0 from /usr/local/lib/python3.9/site-packages/ansible/plugins/callback/default.py
Attempting to use 'default' callback.
Skipping callback 'default', as we already have a stdout callback.
Attempting to use 'junit' callback.
Attempting to use 'minimal' callback.
Skipping callback 'minimal', as we already have a stdout callback.
Attempting to use 'oneline' callback.
Skipping callback 'oneline', as we already have a stdout callback.
Attempting to use 'tree' callback.

PLAYBOOK: mount_nfs_datastore.yml ***************************************************************************************************************************
Positional arguments: mount_nfs_datastore.yml
verbosity: 6
connection: smart
timeout: 10
become_method: sudo
tags: ('all',)
check: True
inventory: ('/usr/home/admin.dimitri/inventory/esx_host.ini',)
subset: esxhosts
forks: 5
1 plays in mount_nfs_datastore.yml
Please enter your username: xxxxx\xxxxx
Please enter your password:
Please enter the vCenter IP Address: 10.14.92.104
Please enter the volume name: dfasdfgdf
Please provide the type of DataStore (Ex: nfs,nfs41 or vmfs): nfs41
Please provide the IP Address of the LIF serving the data: 10.14.10.132
Please specify whether to mount or unmount, enter present to mount, absent to unmount:

PLAY [Mount NFS datastore to vCenter] ***********************************************************************************************************************
skipping: no hosts matched

PLAY RECAP **************************************************************************************************************************************************

 

---
  - hosts: localhost
    name: Mount NFS datastore to vCenter
    vars_prompt:
       - name: username
         prompt: Please enter your username
         private: false
       - name: password
         prompt: Please enter your password
         unsafe: true
       - name: vCenter_IP
         prompt: Please enter the vCenter IP Address
         private: false
       - name: Volume_Name
         prompt: Please enter the volume name
         private: false
       - name: Datastore_Type
         prompt: "Please provide the type of DataStore (Ex: nfs,nfs41 or vmfs)"
         private: false
       - name: NFS_Server
         prompt: Please provide the IP Address of the LIF serving the data
         private: false
       - name: Mount_Unmount
         prompt: Please specify whether to mount or unmount, enter present to mount, absent to unmount
         #present will ignore an existing volume, absent will also ignore if it does not exist
         private: false

    collections:
      - community.vmware
    tasks:
    - name: Gather facts of the vCenter server
      community.vmware.vmware_host_facts:
        hostname: "{{ vCenter_IP }}"
        username: "{{ username }}"
        password: "{{ password }}"
        validate_certs: false
        esxi_hostname: "{{ inventory_hostname }}"
      register: host_facts
    - name: debugging inventory
      debug:
        var: inventory_hostname
    - name: debugging
      debug:
        msg: "{{ host_facts }}"
    - name: Mount NFS Datastore
      community.vmware.vmware_host_datastore:
        hostname: "{{ vCenter_IP }}"
        username: "{{ username }}"
        password: "{{ password }}"
        datastore_name: "{{ Volume_Name }}"
        nfs_path: "/{{ Volume_Name }}"
        datastore_type: "{{ Datastore_Type }}"
        nfs_ro: false
        nfs_server: "{{ NFS_Server }}"
        state: "{{ Mount_Unmount }}"
        validate_certs: false
        esxi_hostname: "{{ inventory_hostname }}"

 

the inventory file: 

{
    "_meta": {
        "hostvars": {}
    },
    "all": {
        "children": [
            "esxhosts",
            "ungrouped"
        ]
    },
    "esxhosts": {
        "hosts": [
            "HM0-ESX033.ESX. ",
            "HM0-ESX034.ESX. ",
            "HM0-ESX035.ESX. ",
            "HM0-ESX036.ESX. ",
            "HM0-ESX037.ESX. ",
            "HM0-ESX038.ESX. ",
            "HM0-ESX039.ESX. ",
            "HM0-ESX040.ESX. ",
            "HM0-ESX041.ESX. ",
            "HM0-ESX042.ESX. ",
            "HM0-ESX043.ESX. ",
            "HM0-ESX044.ESX. ",
            "HM0-ESX045.ESX. ",
            "HM0-ESX046.ESX. ",
            "HM0-ESX047.ESX. ",
            "HM0-ESX048.ESX. ",
            "HM0-ESX049.ESX. ",
            "HM0-ESX050.ESX. ",
            "HM0-ESX051.ESX. ",
            "HM0-ESX052.ESX. ",
            "HM0-ESX053.ESX. ",
            "HM0-ESX054.ESX. ",
            "HM0-ESX055.ESX. ",
            "HM0-ESX056.ESX. ",
            "HM0-ESX061.ESX. ",
            "HM0-ESX062.ESX. ",
            "HM0-ESX063.ESX. ",
            "HM0-ESX064.ESX. ",
            "HM0-ESX065.ESX. ",
            "HM0-ESX066.ESX. ",
            "HM0-ESX067.ESX. ",
            "HM0-ESX068.ESX. "
        ]
    }
}

 

any help is greatly appreciated, thanks!

Dimitri. 

Reply
0 Kudos
1 Reply
puntjuh
Contributor
Contributor

I think i've misunderstood the inventory_hostname part.. replaced it with a loop which works just as well. 

 

- name: Mount NFS Datastore
community.vmware.vmware_host_datastore:
hostname: "{{ vCenter_IP }}"
username: "{{ username }}"
password: "{{ password }}"
datastore_name: "{{ Volume_Name }}"
nfs_path: "/{{ Volume_Name }}"
datastore_type: "{{ Datastore_Type }}"
nfs_ro: false
nfs_server: "{{ NFS_Server }}"
state: "{{ Mount_Unmount }}"
validate_certs: false
esxi_hostname: "{{ item }}"
loop: "{{ lookup('file', 'inventory/esx_host_inventory.txt').splitlines() }}"

Reply
0 Kudos