Get a value from a fact from type type

17 views
Skip to first unread message

Seth Daemen

unread,
Feb 15, 2020, 11:22:34 AM2/15/20
to Ansible Project

I have a fact hostUUid when I display it with the following debug command 

  - name: debug uuid
    debug
:
      msg
:
       
- "{{ hostUUid | type_debug }}"      
       
- "{{ hostUUid }}"




I get the following result:

ok: [localhost] => {
   
"msg": [
       
"list",
       
"[{u'id': u'7261c5626862-abafe49d9bac'}]"
   
]
}


How can i get only the '7261c5626862-abafe49d9bac' value.
I want to use that value in a body for a api request.
When I use this in the in the uri module as body 

body'{"environmentUuid": "7261c5626862","hostUuid": {{ hostUUid }}"}'


I get following body with debug mode 

"body": "{\"environmentUuid\": \"7261c5626862\",\"hostUuid\": {u'id': u'7261c5626862-abafe49d9bac'}\"}",



but the hostUuid needs to be only the value
How can i solve that {{ hostUUid.id }} does also not work

Stefan Hornburg (Racke)

unread,
Feb 15, 2020, 11:26:44 AM2/15/20
to ansible...@googlegroups.com
On 2/15/20 5:22 PM, Seth Daemen wrote:
>
> I have a fact hostUUid when I display it with the following debug command 
>
> |
>   -name:debug uuid
>     debug:
>       msg:
>         -"{{ hostUUid | type_debug }}"     
>        -"{{ hostUUid }}"
> |
>
>
>
>
> I get the following result:
>
> |
> ok:[localhost]=>{
>     "msg":[
>         "list",
>         "[{u'id': u'7261c5626862-abafe49d9bac'}]"
>     ]
> }
> |
>
>
> How can i get only the '7261c5626862-abafe49d9bac' value.

It is the first element of the list, so you need to use {{ hostUUid[0] }}.

Regards
Racke

> I want to use that value in a body for a api request.
> When I use this in the in the uri module as body 
>
> body: '{"environmentUuid": "7261c5626862","hostUuid": {{ hostUUid }}"}'
>
>
> I get following body with debug mode 
>
> |
> "body":"{\"environmentUuid\": \"7261c5626862\",\"hostUuid\": {u'id': u'7261c5626862-abafe49d9bac'}\"}",
> |
>
>
>
> but the hostUuid needs to be only the value
> How can i solve that {{ hostUUid.id }} does also not work
>
> --
> 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/71184c8e-4ad6-40c1-9106-6188d5e437d6%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/71184c8e-4ad6-40c1-9106-6188d5e437d6%40googlegroups.com?utm_medium=email&utm_source=footer>.


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

signature.asc

Seth Daemen

unread,
Feb 15, 2020, 12:44:04 PM2/15/20
to Ansible Project
I already tried {{ hostUUid[0] }} gives also not the result I want.
that gives this in the debug:

ok: [localhost] => {
   
"msg": [
       
"list",

       
{
           
"id": "7261c5626862-abafe49d9bac"
       
}
   
]
}


the body now look like this:
"body": "{\"environmentUuid\": \"7261c5626862\",\"hostUuid\": {u'id': u'7261c5626862-abafe49d9bac'}\"}",


but is must be

"body": "{\"environmentUuid\": \"7261c5626862\",\"hostUuid\": \"7261c5626862-abafe49d9bac\'"}",




Op zaterdag 15 februari 2020 17:22:34 UTC+1 schreef Seth Daemen:

Seth Daemen

unread,
Feb 15, 2020, 2:08:14 PM2/15/20
to Ansible Project
solved my issue.

{{ hostUUid[0] }}

is working. But I had to set:


body_formatjson

in my uri

Op zaterdag 15 februari 2020 18:44:04 UTC+1 schreef Seth Daemen:

Stefan Hornburg (Racke)

unread,
Feb 15, 2020, 2:08:58 PM2/15/20
to ansible...@googlegroups.com
On 2/15/20 6:44 PM, Seth Daemen wrote:
> I already tried {{ hostUUid[0] }} gives also not the result I want.
> that gives this in the debug:
>

It is indeed a dict within a list, so please try hostUUid[0].id

Regards
Racke

> |
> ok:[localhost]=>{
>     "msg":[
>         "list",
>         {
>             "id":"7261c5626862-abafe49d9bac"
>         }
>     ]
> }
> |
>
>
> the body now look like this:
> |
> "body":"{\"environmentUuid\": \"7261c5626862\",\"hostUuid\": {u'id': u'7261c5626862-abafe49d9bac'}\"}",
> |
>
>
> but is must be
>
> |
> "body":"{\"environmentUuid\": \"7261c5626862\",\"hostUuid\": \"7261c5626862-abafe49d9bac\'"}",
> |
>
>
>
>
> Op zaterdag 15 februari 2020 17:22:34 UTC+1 schreef Seth Daemen:
>
>
> I have a fact hostUUid when I display it with the following debug command 
>
> |
>   -name:debug uuid
>     debug:
>       msg:
>         -"{{ hostUUid | type_debug }}"     
>        -"{{ hostUUid }}"
> |
>
>
>
>
> I get the following result:
>
> |
> ok:[localhost]=>{
>     "msg":[
>         "list",
>         "[{u'id': u'7261c5626862-abafe49d9bac'}]"
>     ]
> }
> |
>
>
> How can i get only the '7261c5626862-abafe49d9bac' value.
> I want to use that value in a body for a api request.
> When I use this in the in the uri module as body 
>
> body: '{"environmentUuid": "7261c5626862","hostUuid": {{ hostUUid }}"}'
>
>
> I get following body with debug mode 
>
> |
> "body":"{\"environmentUuid\": \"7261c5626862\",\"hostUuid\": {u'id': u'7261c5626862-abafe49d9bac'}\"}",
> |
>
>
>
> but the hostUuid needs to be only the value
> How can i solve that {{ hostUUid.id }} does also not work
>
> --
> 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/f44aab92-f469-48e8-b661-0f06f075bcfb%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/f44aab92-f469-48e8-b661-0f06f075bcfb%40googlegroups.com?utm_medium=email&utm_source=footer>.
signature.asc
Reply all
Reply to author
Forward
0 new messages