The problem with implementing it that way is that the users's fact is 
referencing "user_show.results[item|int], but I can't add another loop 
variable.  I'm using with_sequence earlier in the playbook for testing  to 
limit the amount of users queried, and that number is needed for the 
user_show.results array.

Harry

On Wednesday, December 8, 2021 at 12:27:42 PM UTC-5 [email protected] 
wrote:

> Hi there,
> You can use the debug module directly to display the required output . 
> This can be done using with_together in a  loop as shown below
>
> debug:
>   msg: "{{ item.0 }}{{ item.1 }}{{ item.2 }}"
> with_together:
>      - "{{ 
> (user_show.results[item|int].json.result.result.krbpasswordexpiration[0]['__datetime__']
>  
> | to_datetime('%Y%m%d%H%M%SZ')).strftime('%s') }}"
>       - "{{ (ansible_date_time.epoch|int - 
> ((user_show.results[item|int].json.result.result.krbpasswordexpiration[0]['__datetime__']
>  
> | to_datetime('%Y%m%d%H%M%SZ')).strftime('%s'))|int) / (60*60*24) }}"
>       - "{{ user_show.results[item|int].json.result.result.uid[0] }}"
>
> If you need to store in a fact you can use the set_fact instead of debug 
> module as seen below 
>
> set_fact:
>       users:  "{{ item.0 }}{{ item.1 }}{{ item.2 }}"
>  with_together:
>      - "{{ 
> (user_show.results[item|int].json.result.result.krbpasswordexpiration[0]['__datetime__']
>  
> | to_datetime('%Y%m%d%H%M%SZ')).strftime('%s') }}"
>       - "{{ (ansible_date_time.epoch|int - 
> ((user_show.results[item|int].json.result.result.krbpasswordexpiration[0]['__datetime__']
>  
> | to_datetime('%Y%m%d%H%M%SZ')).strftime('%s'))|int) / (60*60*24) }}"
>       - "{{ user_show.results[item|int].json.result.result.uid[0] }}"
>  register: res
>
> set_fact:
>   userlist: "{{ item }}"
> loop: "{{ res.results |map(attribute='ansible_facts.users') |list }}"
>
> Your desired content should now be stored in the userlist variable
>
> Wole
> On Wednesday, December 8, 2021 at 10:50:00 AM UTC-5 [email protected] 
> wrote:
>
>> I had another topic about using a fact in a loop, but since this question 
>> is slightly different, I figured I'd start a new topic.
>>
>> Background:
>> I'm using the IPA API calls using the uri module to get a list of users 
>> in our IPA server.  Once I get the uses via user_find, I set a fact of just 
>> the uid's.  This works well.  Then I am using that fact to use the 
>> user_show API via uri.  This also works, but since we have over 1600 users, 
>> it takes a bit of time.  So I'm looping through the uid list to process the 
>> first 20.
>>
>> Issue:
>>
>> I am registering the user_show data in a variable called user_show.  I'm 
>> trying to set a fact that has the uid, password expiration, and the age 
>> calculation.  The problem I have is that the fact has the information from 
>> the last user processed, and is not an array of data as I expect.  Here's 
>> how I'm setting the facts:
>>
>>   - 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\": [[ \"{{ uid[item|int] 
>> }}\"],{\"all\": true,\"version\": \"{{ api_vers }}\"}]}"
>>     register: user_show
>>     with_sequence: start=0 end=19
>>
>>   - name: Set user_show fact
>>     set_fact:
>>       users:
>>       - "{{ 
>> (user_show.results[item|int].json.result.result.krbpasswordexpiration[0]['__datetime__']
>>  
>> | to_datetime('%Y%m%d%H%M%SZ')).strftime('%s') }}"
>>       - "{{ (ansible_date_time.epoch|int - 
>> ((user_show.results[item|int].json.result.result.krbpasswordexpiration[0]['__datetime__']
>>  
>> | to_datetime('%Y%m%d%H%M%SZ')).strftime('%s'))|int) / (60*60*24) }}"
>>       - "{{ user_show.results[item|int].json.result.result.uid[0] }}"
>>
>>   - name: Print users fact    <------------------- This shows the last 
>> user and not the previous 19
>>     debug:
>>       msg: "{{ users }}"
>>
>> So how can I accomplish this?
>>
>> 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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/ae47c4e6-1458-479c-9c77-185cb7387d71n%40googlegroups.com.

Reply via email to