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.
On Wednesday, May 8, 2024 at 11:52:26 AM UTC-6 [email protected] wrote:
> 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
>
> Dimitri Yioulos schrieb am Mittwoch, 8. Mai 2024 um 18:34:36 UTC+2:
>
>> 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 <http://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.
>>
>
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/f649b281-ed50-46e0-963a-c477b65bd930n%40googlegroups.com.