How to set the description for hosts in a dynamic inventory

396 views
Skip to first unread message

Andrew Mulheirn

unread,
Jul 4, 2022, 11:37:34 AM7/4/22
to AWX Project
Hi,

I'm using a dynamic inventory - python that pulls in hosts from a Google Sheet.  It all works well, except that the description always appears as 'imported'.   I can manually edit an imported entry to set the description for each (as you can see in the top line), but would like this to be automatic.

inventory.png

The reason I want this value at the higher level is because the 'name' in the first column is a serial number (for uniqueness) and the description is a more human-friendly string.

When I click on a host that shows as 'imported' in the description column on the list view, I can see that my description has been put in the extra variables:

inventory2.png


So I know that there's a value in there, but it is ending up in the wrong place.
If I browse the API for host 18, I see this:

inventory3.png


My description has become part of the host_vars it seems.  The description field in the next level up is 'imported'.

According to the API docs (https://docs.ansible.com/ansible-tower/latest/html/towerapi/api_ref.html#/Inventories/Inventories_inventories_hosts_create) the description should be settable and is blank by default.  Something is obviously setting it to 'imported' but I don't know what that could be.

It feels like inventory.add_host method doesn't allow the setting of a host description.  All you can do is inventory.set_variable which puts the value in the variables list.

If anyone has any ideas, I'd be glad to hear them!

Regards,
Andy

AWX Project

unread,
Jul 6, 2022, 3:43:33 PM7/6/22
to AWX Project
Hi!

Can you try "_awx_description" instead of "description" and see if that correctly adds the description to your imported hosts?


Let us know if that works for ya, we'll be sure to get proper documentation around this.

AWX Team

Andrew Mulheirn

unread,
Jul 7, 2022, 4:31:34 PM7/7/22
to AWX Project
Thanks very much - that looks like exactly what I was after.  It isn't working for me yet though - is this wrong?   I'm still getting 'Imported' in the description, and _awx_description is ending up in the host vars:

            # Update Desc
            self.inventory.set_variable(hostname, "_awx_description", descr)

--
You received this message because you are subscribed to the Google Groups "AWX Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to awx-project...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/awx-project/3cfe2f6b-3f4a-49ed-8285-e93facb96aacn%40googlegroups.com.

AWX Project

unread,
Jul 13, 2022, 4:01:28 PM7/13/22
to AWX Project
Hi,

Can you let us know which AWX version you are using?

Also, would mind reporting the resulting output when you run your inventory script from the command line? I can't really tell how your self.inventory.set_variable call is affecting the end result.

Thanks!

AWX Team

Andrew Mulheirn

unread,
Jul 21, 2022, 6:24:51 AM7/21/22
to AWX Project
Hi there, 

Thanks - is this output what you need to see?


Kind regards,
Andy

ansible-inventory -i inventory/dev-gsheets.yaml --list -vv
ansible-inventory [core 2.12.4]
  config file = /Users/username/Library/CloudStorage/OneDrive-company/sandbox/ansible-gsheets-inventory/ansible.cfg
  configured module search path = ['/Users/username/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/username/Library/CloudStorage/OneDrive-company/sandbox/ansible-gsheets-inventory/.venv/lib/python3.8/site-packages/ansible
  ansible collection location = /Users/username/.ansible/collections:/usr/share/ansible/collections
  executable location = /Users/username/sandbox/ansible-gsheets-inventory/.venv/bin/ansible-inventory
  python version = 3.8.2 (default, Dec 21 2020, 15:06:04) [Clang 12.0.0 (clang-1200.0.32.29)]
  jinja version = 3.1.1
  libyaml = True
Using /Users/username/Library/CloudStorage/OneDrive-company/sandbox/ansible-gsheets-inventory/ansible.cfg as config file
[gsheets] inventory file: /Users/username/Library/CloudStorage/OneDrive-company/sandbox/ansible-gsheets-inventory/inventory/dev-gsheets.yaml
[gsheets] reading plugin options
[gsheets] checking dependencies
[gsheets] sys path: ['/Users/username/Library/CloudStorage/OneDrive-company/sandbox/ansible-gsheets-inventory/.venv/bin', '/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python38.zip', '/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8', '/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/lib-dynload', '/Users/username/Library/CloudStorage/OneDrive-company/sandbox/ansible-gsheets-inventory/.venv/lib/python3.8/site-packages']
[gsheets] dependencies met
[gsheets] fetch API token
[gsheets] opening sheet
[gsheets] sheet opened: 1GacYZXvmEyL08pnZHsB_H5uVGIl8z7qgcjvnVezDuXA
[gsheets] worksheets: ['TEST1']
[gsheets] fetching host rows
[gsheets] worksheet opened: TEST1
[gsheets] total rows: 6
[gsheets] total matched hosts: 5
[gsheets] fetching groups
[gsheets] total groups: 7
[gsheets] start adding hosts
[gsheets] adding host: (name='FGT60ETK19099QQQ', address='172.29.252.1', groups=['AUS', 'RB1'], description='AUS_TEST1')
[gsheets] adding host: (name='FGT60ETK19099ZZZ', address='172.29.252.8', groups=['UK', 'andy'], description='UK_TEST_UK06')
[gsheets] adding host: (name='FGT60ETK19099YYY', address='172.29.252.17', groups=['GERMANY', 'RB2'], description='GERMANY_TEST_02')
[gsheets] adding host: (name='FGTTESTABC123', address='172.29.255.250', groups=['LAB'], description='branch2')
[gsheets] adding host: (name='FGTTESTABC456', address='172.29.255.251', groups=['LAB', 'andy'], description='branch3')
[gsheets] finished adding hosts
{
    "AUS": {
        "hosts": [
            "FGT60ETK19099QQQ"
        ]
    },
    "GERMANY": {
        "hosts": [
            "FGT60ETK19099YYY"
        ]
    },
    "LAB": {
        "hosts": [
            "FGTTESTABC123",
            "FGTTESTABC456"
        ]
    },
    "RB1": {
        "hosts": [
            "FGT60ETK19099QQQ"
        ]
    },
    "RB2": {
        "hosts": [
            "FGT60ETK19099YYY"
        ]
    },
    "UK": {
        "hosts": [
            "FGT60ETK19099ZZZ"
        ]
    },
    "_meta": {
        "hostvars": {
            "FGT60ETK19099ZZZ": {
                "_awx_description": "UK_TEST_UK06",
                "ansible_host": "172.29.252.8",
                "appliance_name": "UK_TEST_UK06",
                "gsheets_credentials_filepath": "private/credentials.json",
                "gsheets_sheet_id": "1GacYZXvmEyGARBAGEB_H5uVGIl8z7qgcjvnVezDuXA",
                "gsheets_worksheet_name": "TEST1",
                "ipsec_endpoint": "18.39.191.70",
                "is_automated": "YES",
                "is_onboarded": "YES",
                "management_ip": "172.29.252.8",
                "model": "FG-60E",
                "rulebase": "andy",
                "serial": "FGT60ETK19099ZZZ",
                "wan1_mode": "FOOBARZ"
            },
            "FGT60ETK19099YYY": {
                "_awx_description": "GERMANY_TEST_02",
                "ansible_host": "172.29.252.17",
                "appliance_name": "GERMANY_TEST_02",
                "gsheets_credentials_filepath": "private/credentials.json",
                "gsheets_sheet_id": "1GacYZXvmEyGARBAGEB_H5uVGIl8z7qgcjvnVezDuXA",
                "gsheets_worksheet_name": "TEST1",
                "ipsec_endpoint": "18.39.191.70",
                "is_automated": "YES",
                "is_onboarded": "YES",
                "management_ip": "172.29.252.17",
                "model": "FG-60E",
                "rulebase": "RB2",
                "serial": "FGT60ETK19099YYY",
                "wan1_mode": "TEST2"
            },
            "FGT60ETK19099QQQ": {
                "_awx_description": "AUS_TEST1",
                "ansible_host": "172.29.252.1",
                "appliance_name": "AUS_TEST1",
                "gsheets_credentials_filepath": "private/credentials.json",
                "gsheets_sheet_id": "1GacYZXvmEyGARBAGEB_H5uVGIl8z7qgcjvnVezDuXA",
                "gsheets_worksheet_name": "TEST1",
                "ipsec_endpoint": "18.39.191.70",
                "is_automated": "NO",
                "is_onboarded": "YES",
                "management_ip": "172.29.252.1",
                "model": "FG-60E",
                "rulebase": "RB1",
                "serial": "FGT60ETK19099QQQ",
                "wan1_mode": "dhcp"
            },
            "FGTTESTABC123": {
                "_awx_description": "branch2",
                "ansible_host": "172.29.255.250",
                "appliance_name": "branch2",
                "gsheets_credentials_filepath": "private/credentials.json",
                "gsheets_sheet_id": "1GacYZXvmEyGARBAGEB_H5uVGIl8z7qgcjvnVezDuXA",
                "gsheets_worksheet_name": "TEST1",
                "ipsec_endpoint": "10.210.195.1",
                "is_automated": "YES",
                "is_onboarded": "YES",
                "management_ip": "172.29.255.250",
                "model": "VM",
                "serial": "FGTTESTABC123",
                "wan1_mode": "TEST123"
            },
            "FGTTESTABC456": {
                "_awx_description": "branch3",
                "ansible_host": "172.29.255.251",
                "appliance_name": "branch3",
                "gsheets_credentials_filepath": "private/credentials.json",
                "gsheets_sheet_id": "1GacYZXvmEyGARBAGEB_H5uVGIl8z7qgcjvnVezDuXA",
                "gsheets_worksheet_name": "TEST1",
                "ipsec_endpoint": "10.210.195.1",
                "is_automated": "YES",
                "is_onboarded": "YES",
                "management_ip": "172.29.255.251",
                "model": "VM",
                "rulebase": "andy",
                "serial": "FGTTESTABC456",
                "wan1_mode": "TEST456"
            }
        }
    },
    "all": {
        "children": [
            "AUS",
            "GERMANY",
            "LAB",
            "RB1",
            "RB2",
            "UK",
            "andy",
            "ungrouped"
        ]
    },
    "andy": {
        "hosts": [
            "FGT60ETK19099ZZZ",
            "FGTTESTABC456"
        ]
    }
}


Andrew Mulheirn

unread,
Jul 21, 2022, 9:17:41 AM7/21/22
to AWX Project
Apologies - I didn't answer your first question.   This was using operator 0.23.0.   I'm now using operator 0.25.0. (AWX 21.3.0)

On Wed, 13 Jul 2022 at 21:01, AWX Project <awx-p...@googlegroups.com> wrote:

Andrew Mulheirn

unread,
Jul 21, 2022, 10:57:53 AM7/21/22
to AWX Project
Hi there,

An update for you - it works!  

I had set the inventory source to overwrite every time, but maybe it wasn't doing that.    Deleting the inventory hosts, and re-syncing the inventory now populates the description column as expected!

Really happy about that.

Thanks to the team who are answering questions on this - I really appreciate you taking the time.

Regards,
Andrew

AWX Project

unread,
Jul 21, 2022, 1:51:48 PM7/21/22
to AWX Project
Awesome, glad you got this working! thanks for the update
Reply all
Reply to author
Forward
0 new messages