Parse / Loop ansible registered variable

Viewed 230

Trying to figure out how to filter out the list of UserNames in the output of the following playbook.


- name: Get all users
  ome_user_info:
    hostname: "{{ dellome_hostname }}"
    username: "{{ dellome_username }}"
    password: "{{ dellome_password }}"
  register: users

Now the output provides the following:

ok: [192.168.1.100] => {
    "users": {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        },
        "changed": false,
        "deprecations": [
            {
                "msg": "Distribution Ubuntu 18.04 on host 192.168.1.100 should use /usr/bin/python3, but is using /usr/bin/python for backward compatibility with prior Ansible releases. A future Ansible release will default to using the discovered platform python for this host. See https://docs.ansible.com/ansible/2.8/reference_appendices/interpreter_discovery.html for more information",
                "version": "2.12"
            }
        ],
        "failed": false,
        "user_info": {
            "192.168.1.100": {
                "@odata.context": "/api/$metadata#Collection(AccountService.Account)",
                "@odata.count": 3,
                "value": [
                    {
                        "@odata.id": "/api/AccountService/Accounts('10066')",
                        "@odata.type": "#AccountService.Account",
                        "Description": "admin",
                        "DirectoryServiceId": 0,
                        "Enabled": true,
                        "Id": "10066",
                        "IsBuiltin": true,
                        "Locked": false,
                        "Name": "admin",
                        "Password": null,
                        "Permissions@odata.navigationLink": "/api/AccountService/Accounts('10066')/Permissions",
                        "RoleId": "10",
                        "UserName": "admin",
                        "UserTypeId": 1
                    },
                    {
                        "@odata.id": "/api/AccountService/Accounts('10102')",
                        "@odata.type": "#AccountService.Account",
                        "Description": null,
                        "DirectoryServiceId": 0,
                        "Enabled": true,
                        "Id": "10102",
                        "IsBuiltin": false,
                        "Locked": false,
                        "Name": "dell",
                        "Password": null,
                        "Permissions@odata.navigationLink": "/api/AccountService/Accounts('10102')/Permissions",
                        "RoleId": "10",
                        "UserName": "dell",
                        "UserTypeId": 1
                    },
                    {
                        "@odata.id": "/api/AccountService/Accounts('10233')",
                        "@odata.type": "#AccountService.Account",
                        "Description": null,
                        "DirectoryServiceId": 10232,
                        "Enabled": true,
                        "Id": "10233",
                        "IsBuiltin": false,
                        "Locked": false,
                        "Name": "Domain Users",
                        "Password": null,
                        "Permissions@odata.navigationLink": "/api/AccountService/Accounts('10233')/Permissions",
                        "RoleId": "10",
                        "UserName": "Domain Users",
                        "UserTypeId": 2
                    }
                ]
            }
        }
    }
}

I was able to determine the following from the output of the registered variable users.

- debug: 
    var: "{{ users | length }}"

This provides me the length of 5. Which makes sense to an extent. If I start poking into the output I can then determine the following:

- debug: 
    var: "{{ users.user_info | length }}"

This shows me the length of 1 which makes sense. If I add the var using users.user_info I can then see the output below.

TASK [manage_users : debug] *************************************************************************************************************************************************************
ok: [192.168.1.100] => {
    "users.user_info": {
        "192.168.1.100": {
            "@odata.context": "/api/$metadata#Collection(AccountService.Account)",
            "@odata.count": 3,
            "value": [
                {
                    "@odata.id": "/api/AccountService/Accounts('10066')",
                    "@odata.type": "#AccountService.Account",
                    "Description": "admin",
                    "DirectoryServiceId": 0,
                    "Enabled": true,
                    "Id": "10066",
                    "IsBuiltin": true,
                    "Locked": false,
                    "Name": "admin",
                    "Password": null,
                    "Permissions@odata.navigationLink": "/api/AccountService/Accounts('10066')/Permissions",
                    "RoleId": "10",
                    "UserName": "admin",
                    "UserTypeId": 1
                },
                {
                    "@odata.id": "/api/AccountService/Accounts('10102')",
                    "@odata.type": "#AccountService.Account",
                    "Description": null,
                    "DirectoryServiceId": 0,
                    "Enabled": true,
                    "Id": "10102",
                    "IsBuiltin": false,
                    "Locked": false,
                    "Name": "dell",
                    "Password": null,
                    "Permissions@odata.navigationLink": "/api/AccountService/Accounts('10102')/Permissions",
                    "RoleId": "10",
                    "UserName": "dell",
                    "UserTypeId": 1
                },
                {
                    "@odata.id": "/api/AccountService/Accounts('10233')",
                    "@odata.type": "#AccountService.Account",
                    "Description": null,
                    "DirectoryServiceId": 10232,
                    "Enabled": true,
                    "Id": "10233",
                    "IsBuiltin": false,
                    "Locked": false,
                    "Name": "Domain Users",
                    "Password": null,
                    "Permissions@odata.navigationLink": "/api/AccountService/Accounts('10233')/Permissions",
                    "RoleId": "10",
                    "UserName": "Domain Users”,
                    "UserTypeId": 2
                }
            ]
        }
    }
}

Trying to figure out how I can loop through and get an array of the following: value —> UserName. Essentially I am going to take the following value and loop through and delete users that don’t equal the following. Admin, dell, domain users.

Now one might say you would easily just say while not = to items - then that list would work - I first need to figure out how to search and get the values out. I have tried the following:

- debug: 
    var: users.user_info().value()

- debug:
    var: users.user_info.find('UserName')

- debug:
    msg: UserName 
  loop: users.user_info."192.168.1.100".value

#- debug:
#    var: users.user_info."{{ dellome_hostname }}".UserName
#- debug: 
#    var: "(claims1 | from_json).value"
 
  #  msg: "{{ users.user_info.UserName | list }}"
  #  (output_text.stdout | from_json).ismaster


#- debug:
#    msg: "{{ item }}"
  #loop: "{{ users.user_info | from_json | list }}" 


At the end of this once I understand how to get the data out i can then create a loop to execute the following:

---
- name: Delete a User in Dell OME
  ome_user:
    hostname: "{{ dellome_hostname }}"
    username: "{{ dellome_username }}"
    password: "{{ dellome_password }}"
    state: "{{ requestedState }}"
    name: "{{ requstedUserName }}"

This is where i can then add the loop to eliminate users that don't meet the list of names i provide. Any help would be greatly appreciated.

Here are some of the errors i have run into.

TASK [manage_users : debug] *************************************************************************************************************************************************************
fatal: [192.168.1.100]: FAILED! => {"msg": "Unexpected templating type error occurred on ({{users.user_info().value()}}): 'dict' object is not callable"}
TASK [manage_users : debug] *************************************************************************************************************************************************************
fatal: [192.168.1.100]: FAILED! => {"msg": "template error while templating string: expected name or number. String: {{users.user_info.\"192.168.1.100\".value()}}"}

Or as i am looking at this - if i can figure out a way to create a loop that looks for the roleID and when it is not equal to 10 then delete the user.

1 Answers

Use json_query. The tasks below

    - set_fact:
        users_rm: "{{ users.user_info|
                      json_query('*.value[].UserName') }}"
    - debug:
        var: users_rm

give

  users_rm:
  - admin
  - dell
  - Domain Users

You are running the query at the host 192.168.1.100 and the dictionary users comprises the users from this single host only. If there are more hosts in the dictionary the asterisk '*' in the query above would select them all. It would be better to select users for the particular host the query is running at. For example, the task below gives the same result

    - set_fact:
        users_rm: "{{ users.user_info[inventory_hostname].value|
                      map(attribute='UserName')|
                      list }}"
Related