Create a variable based on a variable

62 views
Skip to first unread message

Dimitri Yioulos

unread,
May 8, 2024, 12:34:36 PMMay 8
to Ansible Project
Hello, all.

The subject of this post is probably terribly named. That said, here's what I'm trying to accomplish:

I have two VMware vCenter hosts - vcenter1.mycompany.com and vcenter2.mycompany.com. Within each host is are two datacenters - datacenter1 and datacenter2. for each host, the datacenter alignments are vcenter1 -- datacenter1 and vcenter2 -- datacenter2.

I have a number of playbooks that do actions against vCenter. For example, I have one deletes all snapshots. It looks like this:

---

- hosts: all
  become: false
  gather_facts: false

  vars_prompt:

    - name: "vcenter_username"
      prompt: "Enter your Vcenter username (without @vsphere.local)"
      private: no
    - name: "vcenter_password"
      prompt: "Enter your VMware password"
      private: yes
    - name: "vcenter_hostname"
      prompt: "Enter your Vcenter host name:\nvcenter1\nvcenter2\n"
      private: no
    - name: "vcenter_datacenter"
      prompt: "Enter your Vcenter datacenter:\ndatacenter1\ndatacenter2\n"
      private: no

  tasks:

    - name: Find vm folder name
      ~

    - name: Remove all snapshots of all VMs
      community.vmware.vmware_guest_snapshot:
        hostname: "{{ vcenter_hostname }}.fmycompany.com"
        username: "{{ vcenter_username }}@vsphere.local"
        password: "{{ vcenter_password }}"
        datacenter: "{{ vcenter_datacenter }} Datacenter"
        folder: "{{ vm_folder.folders | regex_replace(\"([\\[\\]'])\",'') }}"
        name: "{{ inventory_hostname }}"
        state: remove_all
        validate_certs: False
      delegate_to: localhost


As you can see, at present, I have to provide the vCenter name, and the corresponding datacenter name in order for the playbook to work. I would like to eliminate the need to specify the datacenter name, and have that populate datacenter ("{{ vcenter_datacenter }} Datacenter") based on the vCenter host that I choose (hostname: "{{ vcenter_hostname }}.fmycompany.com"). I'm not clever enough to suss that out, and would appreciate your help.

andregr...@gmail.com

unread,
May 8, 2024, 1:52:26 PMMay 8
to Ansible Project
i am quite a newbee with ansible, but i would suggest to 
a) use host-variables with e.g. "datacenter" set to the matching datacenter value or
b) use a regex on the host variable to create the datacenter on it

regards,
andre

John Harmon

unread,
May 8, 2024, 6:49:38 PMMay 8
to Ansible Project
Just off the top of my head you can use set_fact.
- name: Set datacenter variable based on hostname
  set_fact:
    datacenter: "datacenter{{ ansible_hostname[-1] }}"
  when: ansible_hostname | regex_search('^[a-zA-Z0-9]*[1-2]')


You can then just use {{ datacenter }} and it should populate correctly.  I haven't tested the above, but if it doesn't work it should at least put you in the ball park.

Rowe, Walter P. (Fed)

unread,
May 14, 2024, 5:34:29 AMMay 14
to 'Rowe, Walter P. (Fed)' via Ansible Project
A dictionary would solve this using the guest name as the key.

vcenter:
  host1: { datacenter: datacenter1, vcenter: vcenter2 }
  host2: { datacenter: datacenter1, vcenter: vcenter1 }
  host3: { datacenter: datacenter2, vcenter: vcenter2 }
  host4: { datacenter: datacenter2, vcenter: vcenter1 }

{{ vcenter[inventory_hostname].datacenter }}
{{ vcenter[inventory_hostname].vcenter }}

Keeping that dictionary update to date will be tedious but could be automated too. Query each vcenter / datacenter pair for its list of VMs and generate a vars file with the above dictionary. Include that vars file in your playbooks. Alternatively (and more expensive) is to create an initial task that queries the each vcenter / datacenter pair and creates this dictionary in real time.

Walter
--
Walter Rowe, Division Chief
Infrastructure Services Division
Mobile: 202.355.4123

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/424d790e-e657-405b-b720-8ce0448df4f4n%40googlegroups.com.

Message has been deleted

Dimitri Yioulos

unread,
May 17, 2024, 3:03:01 PMMay 17
to Ansible Project
Than you all. I'll make note of your suggestions. I solved my issue like this (whether it's "optimal, I don;t know):

  vars:
    vcenter_datacenter: >-
      {% if vcenter_hostname == 'vcenter1' %}
        
Datacenter1 Datacenter
      {% elif vcenter_hostname == 'vcenter2' %}
        
Datacenter2 Datacenter
      {% else %}
        Default Datacenter
      {% endif %}

Brian Coca

unread,
May 17, 2024, 5:02:18 PMMay 17
to ansible...@googlegroups.com
Also:

vars:
vcenter_datacenter: '{{ vcenter_hostname ==
"vcenter1"|ternary("First", vcenter_hostname ==
"vcenter2"|ternary("datacenter1", "datacenter2")) }}' Dataceenter

but i would do this:

vars:
DCS:
vcenter1: First
vcenter2: datacenter1
vcenter_datacenter: '{{ DCS[vcenter_hostname]|default("datacenter2")
}}' Datacenter

--
----------
Brian Coca (he/him/yo)

Abhijeet Janwalkar

unread,
May 23, 2024, 7:19:33 AMMay 23
to Ansible Project
Not exactly based on the variables, but I use this to find the datacenter in the vCenter (in my case I have only one datacenter per vCenter so it works)

- name: Gather information about all datacenters in vCenter
  community.vmware.vmware_datacenter_info:
    hostname: "{{ hostname }}"
    username: "{{ username }}"
    password: "{{ password }}"
    validate_certs: "{{ false}}"
  delegate_to: localhost
  register: datacenterInfo

#- name: Display datacenter Info
  #ansible.builtin.debug:
    #msg: "{{ datacenterInfo.datacenter_info[0].name }}"

- name: set Fact - datacenter
  set_fact:
    datacenter: "{{ datacenterInfo.datacenter_info[0].name }}"
Hope this helps.

Warm Regards,
Abhi
Reply all
Reply to author
Forward
0 new messages