Using Ansible to manage ESXI Inventory

Viewed 160

I am trying to use Ansible to modify the DNS settings on a group of ESXI servers. I've been able to get my playbook to change the settings on a single server like this:

--- 
- hosts: localhost
  tasks:
  - name: Configure ESXi hostname and DNS servers
    vmware_dns_config:
      hostname: 'myesxiserver.domain.local'
      username: 'username'
      password: 'password'
      dns_servers:
          - x.x.x.x
          - x.x.x.x
    delegate_to: localhost

How can I get this to work for multiple servers? The Ansible documentation provides this example:

---
- hosts: localhost
  tasks:
  - name: Configure ESXi hostname and DNS servers
    vmware_dns_config:
      hostname: '{{ esxi_hostname }}'
      username: '{{ esxi_username }}'
      password: '{{ esxi_password }}'
      change_hostname_to: esx01
      domainname: foo.org
      dns_servers:
        - 8.8.8.8
        - 8.8.4.4
  delegate_to: localhost

I'm not clear on how to iterate through a list of hosts and pass the correct values into the variable '{{ esxi_hostname }}' for each of my servers. I'm assuming that the variables can be passed using an inventory file but I haven't found any good examples on how to do this for ESXI servers.

1 Answers

So I did get this working.

---
- hosts: localhost
  vars_files:
    - vars.yml
    - vars2.yml

  tasks:
  - name: Configure ESXi hostname and DNS servers
    vmware_dns_config:
      hostname: "{{ item }}"
      username: 'myadmin'
      password: "{{ Password }}"
      validate_certs: no
      change_hostname_to:  "{{ item }}" 
      domainname: foo.org
      dns_servers:
          - x.x.x.x
          - x.x.x.x
    delegate_to: localhost
    loop: "{{ esxihost }}"

I had to pass a list of host names using vars_file and iterate through it using the loop keyword. I tried to use the {{inventory_hostname}} variable along with a standard inventory file but because SSH is not generally enabled by default on ESXi servers I would get an SSH connection error.

Related