I don't see where you're setting uid, the debug step, or its output. All I 
see is that

   loop:
     - "{{ uid }}"

is only producing one invocation of the task with the desired values all 
glommed into one string.
Please show your input code and resulting output. I suspect you're somehow 
producing a string rather than a list, but with nothing to look at, it's 
hard to know.
On Friday, December 3, 2021 at 4:32:01 PM UTC-5 lift...@gmail.com wrote:

> That works, but when I try to then call the IPA user_show API, which takes 
> the UID as a parameter, the entire list generated is sent in.
>
>   - name: Run user_show from IDM API using previously stored session cookie
>     uri:
>       url: "https://{{idmfqdn}}/ipa/session/json";
>       method: POST
>       headers:
>         Cookie: "{{ login.set_cookie }}"
>         Referer: "https://{{idmfqdn}}/ipa";
>         Content-Type: "application/json"
>         Accept: "application/json"
>       body_format: json
>       body: "{\"method\": \"user_show\",\"params\": [[ \"{{ item 
> }}\"],{\"all\": true,\"version\": \"{{ api_vers }}\"}]}"
>     register: user_show
>     loop:
>     - "{{ uid }}"
>
> TASK [Run user_show from IDM API using previously stored session cookie] 
> *************************************************************************
> ok: [localhost] => (item=[u'user1', u'user2', u'user3'])
>
>
>                 "invocation": {
>                     "module_args": {
>                         "attributes": null,
>                         "backup": null,
>                         "body": {
>                             "method": "user_show",
>                             "params": [
>                                 [
>                                     "[u'user1', u'user2', u'user3']"
> ],
>                                 {
>                                     "all": true,
>                                     "version": "2.237"
>                                 }
>                             ]
>                         },
>
> "message": "[u'user1', u'user2', u'user3']: user not found
>
> So, why does the debug print appear to print each UID out, but when I try 
> to reference them in the loop, they are sent over as 1 big string?
>
> Thanks,
> Harry
> On Friday, December 3, 2021 at 4:17:14 PM UTC-5 uto...@gmail.com wrote:
>
>> I really want to love Ansible, but the fact that such a simple data 
>> manipulation completely eludes the newbie doesn't help. Worse, that I've 
>> done this (or equivalent) dozens of times and it still takes me as long as 
>> it does to come up with a working demo ... [sigh].
>>
>> Anyway, here's a working demo, assuming I got your initial data shaped 
>> right. Good luck.
>>
>> ---
>> - name: Demo for processing user_find IPA API results
>>   hosts: localhost
>>   vars:
>>     user_find:
>>       json:
>>         result:
>>           result:
>>             - dn: 
>> "uid=harry.devine,cn=users,cn=accounts,dc=example,dc=com"
>>               gidnumber: [ "11111" ]
>>               givenname: [ "Harry" ]
>>               homedirectory: [ "/home/harry.devine" ]
>>               krbcanonicalname: [ "harry....@example.com" ]
>>               krbprincipalname: [ "harry....@example.com" ]
>>               loginshell: [ "/bin/bash" ]
>>               mail: [ "harry....@example.com" ]
>>               nsaccountlock: false
>>               sn: [ "Devine" ]
>>               telephonenumber: [ "(800) 867-5309" ]
>>               uid: [ "harry.devine" ]
>>               uidnumber: [ "1111" ]
>>             - dn: 
>> "uid=marve.devine,cn=users,cn=accounts,dc=example,dc=com"
>>               gidnumber: [ "11111" ]
>>               givenname: [ "Marve" ]
>>               homedirectory: [ "/home/marve.devine" ]
>>               krbcanonicalname: [ "marve....@example.com" ]
>>               krbprincipalname: [ "marve....@example.com" ]
>>               loginshell: [ "/bin/bash" ]
>>               mail: [ "marve....@example.com" ]
>>               nsaccountlock: false
>>               sn: [ "Devine" ]
>>               telephonenumber: [ "(800) 867-5309" ]
>>               uid: [ "marve.devine" ]
>>               uidnumber: [ "1111" ]
>>
>>   tasks:
>>     - name: Look at user_find.json.result.result
>>       debug:
>>         msg: "{{ user_find.json.result.result | to_json }}"
>>
>>     - name: Stash the uids with set_fact
>>       set_fact:
>>         demo_uids: "{{ 
>> user_find.json.result.result|map(attribute='uid')|flatten }}"
>>
>>     - name: Look at our set fact
>>       debug:
>>         msg: "{{ demo_uids }}"
>>
>>     - name: Or just loop over directly; no need to do a set_fact
>>       debug:
>>         msg: "{{ item }}"
>>       loop: "{{ user_find.json.result.result|map(attribute='uid')|flatten 
>> }}"
>>
>>
>> On Friday, December 3, 2021 at 12:01:14 PM UTC-5 lift...@gmail.com wrote:
>>
>>> So I'm still trying to get this to work.  I'm thinking that the fact is 
>>> one large item, so I need to know how I can loop through those items.  I'm 
>>> trying to get the UID of each user.  What the user_find IPA API call 
>>> returns is <variable>.json.result.result, and the users are added to the 
>>> fact in the following form:
>>>
>>> - [ user1 ]\n
>>> - [ user2 ]\n
>>>
>>> I'm setting the user_find variable and fact as follows:
>>>
>>>   - name: Run user_find from IDM API using previously stored session 
>>> cookie
>>>     uri:
>>>       url: "https://{{idmfqdn}}/ipa/session/json";
>>>       method: POST
>>>       headers:
>>>         Cookie: "{{ login.set_cookie }}"
>>>         Referer: "https://{{idmfqdn}}/ipa";
>>>         Content-Type: "application/json"
>>>         Accept: "application/json"
>>>       body_format: json
>>>       body: "{\"method\": \"user_find/1\",\"params\": [[],{\"version\": 
>>> \"{{ api_vers }}\"}]}"
>>>     no_log: true
>>>     register: user_find
>>>
>>>   - name: Set fact for users
>>>     set_fact:
>>>       uid: "{{ user_find.json.result.result|json_query('[*].uid') | list 
>>> | to_yaml }}"
>>>
>>> The user_find information is listed earlier in this thread.  So I'm 
>>> trying to got through that variable and pull out each UID.  Without the 
>>> to_yaml filter, those are shown as ["user1"], ["user2"], etc.  So how do I 
>>> loop through these?  Can I set up the fact as an array of user IDs and loop 
>>> through that?  If so, how?
>>>
>>> Thanks,
>>> Harry
>>> On Tuesday, November 30, 2021 at 8:26:48 AM UTC-5 lift...@gmail.com 
>>> wrote:
>>>
>>>> I had done that previously and knew it was getting the right data, but 
>>>> I put the debug back in and the redacted output is below.  There are over 
>>>> 1600 users, so I am only showing the start of the data in a redacted form. 
>>>>  
>>>> The debug print is printing "{{ user_find.json.result.result }}":
>>>>
>>>> TASK [Print users found] 
>>>> **********************************************************************************************************
>>>> ok: [auth1.secure-ose.faa.gov] => {
>>>>     "msg": [
>>>>         {
>>>>             "dn": 
>>>> "uid=harry.devine,cn=users,cn=accounts,dc=example,dc=com",
>>>>             "gidnumber": [
>>>>                 "11111"
>>>>             ],
>>>>             "givenname": [
>>>>                 "Harry"
>>>>             ],
>>>>             "homedirectory": [
>>>>                 "/home/harry.devine"
>>>>             ],
>>>>             "krbcanonicalname": [
>>>>                 "harry....@example.com"
>>>>             ],
>>>>             "krbprincipalname": [
>>>>                 "harry....@example.com"
>>>>             ],
>>>>             "loginshell": [
>>>>                 "/bin/bash"
>>>>             ],
>>>>             "mail": [
>>>>                 "harry....@example.com"
>>>>             ],
>>>>             "nsaccountlock": false,
>>>>             "sn": [
>>>>                 "Devine"
>>>>             ],
>>>>             "telephonenumber": [
>>>>                 "(800) 867-5309"
>>>>             ],
>>>>             "uid": [
>>>>                 "harry.devine"
>>>>             ],
>>>>             "uidnumber": [
>>>>                 "1111"
>>>>             ]
>>>>         },
>>>>
>>>> Thanks,
>>>> Harry
>>>>
>>>> On Monday, November 29, 2021 at 3:45:46 PM UTC-5 uto...@gmail.com 
>>>> wrote:
>>>>
>>>>> Before the step that's failing, insert a debug step with the msg: "{{ 
>>>>> user_find.json.result.result }}" (really? "result.result"? maybe...) 
>>>>> so you (and we) can be certain what your items actually look like. 
>>>>> Otherwise, we're just guessing.
>>>>>
>>>>>
>>>>> On Monday, November 29, 2021 at 3:05:37 PM UTC-5 lift...@gmail.com 
>>>>> wrote:
>>>>>
>>>>>> I am traversing our IPA server to get find all users, then I want to 
>>>>>> loop through all of them to get their password expiration date.  I use 
>>>>>> the 
>>>>>> IPA API via the uri module and register the variable, but no matter what 
>>>>>> I 
>>>>>> try to access the uid of each found user, I get the following error:
>>>>>>
>>>>>> TASK [Run user_show from IDM API using previously stored session 
>>>>>> cookie] **********************************************************
>>>>>> fatal: [localhost]: FAILED! => {"msg": "template error while 
>>>>>> templating string: expected name or number. String: {\"method\": 
>>>>>> \"user_show\",\"params\": [[ \"{{ item[0].['uid'] }}\"],{\"all\": 
>>>>>> true,\"version\": \"{{ api_vers }}\"}]}"}
>>>>>>
>>>>>> Here's the section of my playbook that seems to be giving me issues:
>>>>>>
>>>>>>   - name: Run user_find from IDM API using previously stored session 
>>>>>> cookie
>>>>>>     uri:
>>>>>>       url: "https://{{idmfqdn}}/ipa/session/json";
>>>>>>       method: POST
>>>>>>       headers:
>>>>>>         Cookie: "{{ login.set_cookie }}"
>>>>>>         Referer: "https://{{idmfqdn}}/ipa";
>>>>>>         Content-Type: "application/json"
>>>>>>         Accept: "application/json"
>>>>>>       body_format: json
>>>>>>       body: "{\"method\": \"user_find/1\",\"params\": 
>>>>>> [[],{\"version\": \"{{ api_vers }}\"}]}"
>>>>>>     register: user_find
>>>>>>
>>>>>>   - name: Run user_show from IDM API using previously stored session 
>>>>>> cookie
>>>>>>     uri:
>>>>>>       url: "https://{{idmfqdn}}/ipa/session/json";
>>>>>>       method: POST
>>>>>>       headers:
>>>>>>         Cookie: "{{ login.set_cookie }}"
>>>>>>         Referer: "https://{{idmfqdn}}/ipa";
>>>>>>         Content-Type: "application/json"
>>>>>>         Accept: "application/json"
>>>>>>       body_format: json
>>>>>>       body: "{\"method\": \"user_show\",\"params\": [[ \"{{ 
>>>>>> item[0].['uid'] }}\"],{\"all\": true,\"version\": \"{{ api_vers }}\"}]}"
>>>>>>     register: user_show
>>>>>>     loop:
>>>>>>     - "{{ user_find.json.result.result }}"
>>>>>>
>>>>>> Thanks,
>>>>>> Harry
>>>>>>
>>>>>

-- 
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-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/9471f1af-012a-4f81-bdf0-00eac932b90an%40googlegroups.com.

Reply via email to