Automate Custome Attribute Population - Vmware

84 views
Skip to first unread message

esxi...@gmail.com

unread,
Feb 22, 2021, 5:41:08 PM2/22/21
to Ansible Project
Hi

Here are my custom attributes


Capture.JPG


Today these are populated with community.vmware.vmware_guest_custom_attributes


But that is manually from the file.. it has human error possible

eg Ubuntu 20.04 folks may type Ubuntu20.4


There is facts which ansible gather has the OS info.. eg -m setup -a "filter=ansible_distribution_version"


Is it somehow possible to get the OS info populated "automatically" with a playbook .. ie update the custom attributes dynamically with real time data ?

Any suggestions Please ?

Thanks

esxi...@gmail.com

unread,
Feb 22, 2021, 5:42:18 PM2/22/21
to Ansible Project
I will give 1 more reason .. eg someone upgrade OS from 16.04 to 20.04 .. may forget to update the custom attributes...

Abhijeet Kasurde

unread,
Feb 23, 2021, 12:30:23 AM2/23/21
to ansible...@googlegroups.com
You can do this using dynamic inventory and delegate_to

Here is how -

1. Gather facts about virtual machines
2. Gather facts about custom attributes using vmware_guest_info
3. Compare and change if needed

you can see this playbook

---
- hosts: running
tasks:
- setup:
register: r

- debug:
msg: "{{ r.ansible_facts.ansible_distribution }}"

- name: Get custom attributes information
vmware_guest_info:
name: CentOS7_Rest
datacenter: Asia-Datacenter1
folder: /Asia-Datacenter1/vm/
register: vm_info
delegate_to: localhost

- debug:
msg: "{{ vm_info }}"

- name: Update OS information
vmware_guest_custom_attributes:
name: CentOS7_Rest
datacenter: Asia-Datacenter1
folder: /Asia-Datacenter1/vm/
state: present
attributes:
- name: OS
value: "{{ r.ansible_facts.ansible_distribution }}"
when: vm_info.instance.customvalues.OS != r.ansible_facts.ansible_distribution
delegate_to: localhost

--
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/6a28a2f4-92d4-4963-8a8d-0c79c64b87cbn%40googlegroups.com.


--
Thanks,
Abhijeet Kasurde

esxi...@gmail.com

unread,
Feb 23, 2021, 1:49:36 AM2/23/21
to Ansible Project
Thanks a lot Abhijit .. i will give it a try  & report issue if any.

Kiran Kumar

unread,
Feb 24, 2021, 3:17:43 AM2/24/21
to ansible...@googlegroups.com
Thanks Abhijit, Awesome ,  it worked !!!!

Only issue is

value: "{{ r.ansible_facts.ansible_distribution }}"
when: vm_info.instance.customvalues.OS != r.ansible_facts.ansible_distribution

that Just give Ubuntu ... is it possible to extract the info from setup module ie below part..


        "ansible_lsb": {
            "codename": "bionic",
            "description": "Ubuntu 18.04.5 LTS",
            "id": "Ubuntu",
            "major_release": "18",
            "release": "18.04"
        },





Hopefully that is also present in CentOS nodes that way .. so the description part ...

r.ansible_facts.ansible_lsb.description ? will do ..


Kiran Kumar

unread,
Feb 24, 2021, 3:24:35 AM2/24/21
to ansible...@googlegroups.com
Wow r.ansible_facts.ansible_lsb.description did worked !!! Thanks again Abhijit

Kiran Kumar

unread,
Feb 24, 2021, 5:05:03 AM2/24/21
to ansible...@googlegroups.com

Setup gives :

        "ansible_distribution": "Ubuntu",
        "ansible_distribution_file_parsed": true,
        "ansible_distribution_file_path": "/etc/os-release",
        "ansible_distribution_file_variety": "Debian",
        "ansible_distribution_major_version": "18",
        "ansible_distribution_release": "bionic",
        "ansible_distribution_version": "18.04",



I would like to use OS name as

Ubuntu20.04 & not Ubuntu 20.04.2 LTS

So tried below .. as value .. all 3 failed .. any suggestions Please, on how to concatenate  ?

#######################################

The offending line appears to be:

        - name: OS
          value: "{{ r.ansible_facts.ansible_distribution }}" "{{ r.ansible_facts.ansible_distribution_version }}"
                                                              ^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes. Always quote template expression brackets when they
start a value. For instance:

    with_items:
      - {{ foo }}

Should be written as:

    with_items:
      - "{{ foo }}"
#######################################
The offending line appears to be:

        - name: OS
          value: "{{ r.ansible_facts.ansible_distribution }}"+"{{ r.ansible_facts.ansible_distribution_version }}"
                                                             ^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes. Always quote template expression brackets when they
start a value. For instance:

    with_items:
      - {{ foo }}

Should be written as:

    with_items:
      - "{{ foo }}"

#######################################
{"msg": "template error while templating string: expected token 'end of print statement', got 'r'. String: {{ r.ansible_facts.ansible_distribution r.ansible_facts.ansible_distribution_version }}"}

#######################################

Stefan Hornburg (Racke)

unread,
Feb 24, 2021, 5:14:23 AM2/24/21
to ansible...@googlegroups.com
On 2/24/21 11:04 AM, Kiran Kumar wrote:
>
> Setup gives :
>
>         "ansible_distribution": "Ubuntu",
>         "ansible_distribution_file_parsed": true,
>         "ansible_distribution_file_path": "/etc/os-release",
>         "ansible_distribution_file_variety": "Debian",
>         "ansible_distribution_major_version": "18",
>         "ansible_distribution_release": "bionic",
>         "ansible_distribution_version": "18.04",
>
>
>
> I would like to use OS name as
>
> Ubuntu20.04 & not Ubuntu 20.04.2 LTS
>
> So tried below .. as /*value */.. all 3 failed .. any suggestions Please, on how to concatenate  ?
>
> #######################################
>
> The offending line appears to be:
>
>         - name: OS
>           value: "{{ r.ansible_facts.ansible_distribution }}" "{{ r.ansible_facts.ansible_distribution_version }}"
>                                                               ^ here

This should do the trick:

value: "{{ r.ansible_facts.ansible_distribution }} {{ r.ansible_facts.ansible_distribution_version }}"

Regards
racke
> On Wed, Feb 24, 2021 at 12:24 AM Kiran Kumar <esxi...@gmail.com <mailto:esxi...@gmail.com>> wrote:
>
> Wow r.ansible_facts.ansible_lsb.description did worked !!! Thanks again Abhijit
>
> On Wed, Feb 24, 2021 at 12:17 AM Kiran Kumar <esxi...@gmail.com <mailto:esxi...@gmail.com>> wrote:
>
> Thanks Abhijit, Awesome ,  it worked !!!!
>
> Only issue is
>
> value: "{{ r.ansible_facts.ansible_distribution }}"
> when: vm_info.instance.customvalues.OS != r.ansible_facts.ansible_distribution
>
> that Just give Ubuntu ... is it possible to extract the info from setup module ie below part..
>
>
>         "ansible_lsb": {
>             "codename": "bionic",
> _*            "description": "Ubuntu 18.04.5 LTS",*_
> Capture.JPG
>
>
> Today these are populated with community.vmware.vmware_guest_custom_attributes
>
>
> But that is manually from the file.. it has human error possible
>
> eg Ubuntu 20.04 folks may type Ubuntu20.4
>
>
> There is facts which ansible gather has the OS info.. eg -m setup -a
> "filter=ansible_distribution_version"
>
>
> Is it somehow possible to get the OS info populated "automatically" with a playbook .. ie update
> the custom attributes dynamically with real time data ?
>
> Any suggestions Please ?
>
> Thanks
>
> --
> 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/6a28a2f4-92d4-4963-8a8d-0c79c64b87cbn%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/6a28a2f4-92d4-4963-8a8d-0c79c64b87cbn%40googlegroups.com?utm_medium=email&utm_source=footer>.
>
>
>
> --
> Thanks,
> Abhijeet Kasurde
>
> --
> 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 <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/653118a7-176e-48c8-921b-f23830da9534n%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/653118a7-176e-48c8-921b-f23830da9534n%40googlegroups.com?utm_medium=email&utm_source=footer>.
>
> --
> 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 <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/CAGhW9Js43VnoQRBUP2XH7%3DTU4JTZ%3DsQdrcXj_OYjv0_k14%3DQ-w%40mail.gmail.com
> <https://groups.google.com/d/msgid/ansible-project/CAGhW9Js43VnoQRBUP2XH7%3DTU4JTZ%3DsQdrcXj_OYjv0_k14%3DQ-w%40mail.gmail.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

OpenPGP_signature

Abhijeet Kasurde

unread,
Feb 24, 2021, 5:15:16 AM2/24/21
to ansible...@googlegroups.com
how about -
"{{ r.ansible_facts.ansible_distribution ~ ' ' ~ r.ansible_facts.ansible_distribution_version }}"



--
Thanks,
Abhijeet Kasurde

Kiran Kumar

unread,
Feb 24, 2021, 1:11:51 PM2/24/21
to ansible...@googlegroups.com
"{{ r.ansible_facts.ansible_distribution ~ ' ' ~ r.ansible_facts.ansible_distribution_version }}" , works


it gives

Ubuntu 18.04

How to get

Ubuntu18.04

Ie remove the space ?

Will try few things ..  Any docs Please on this ?  where you go these .. ~ ' ' ~ :)

BTW yes, "{{ r.ansible_facts.ansible_distribution }} {{ r.ansible_facts.ansible_distribution_version }}"  , worked too , even that add that space ...



Kiran Kumar

unread,
Feb 24, 2021, 1:13:48 PM2/24/21
to ansible...@googlegroups.com
Ok this one was easy .. just did below

value: "{{ r.ansible_facts.ansible_distribution }}{{ r.ansible_facts.ansible_distribution_version }}"

Thanks again Abhi & Stefan Hornburg (Racke)
Reply all
Reply to author
Forward
0 new messages