variable sub dict output shows undefined

18 views
Skip to first unread message

Punit Jain

unread,
Jun 19, 2019, 2:29:52 AM6/19/19
to Ansible Project
Hi,

I have a part of playbook task as below:

- name: get url data
  uri:
    uri: <path>
    method: GET
    user: username
    password: password
  register: dns_result
  no_log: true

- set_fact:
    dns_content: "{{ dns_result.content.records}}"

Gives an error:

"msg" : The task includes an option with an undefined variable. The error was ansible.vars.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'records'

however when I set_fact separately as below, it works :

- name: get url data
  uri:
    uri: <path>
    method: GET
    user: username
    password: password
  register: dns_result
  no_log: true

- set_fact:
    dns_content: "{{ dns_result.content}}"

- debug:
    msg: "{dns_contents.records}"

This works fine and returns results. Any advice what am i doing wrong here ?

Regards,
Punit


Kai Stian Olstad

unread,
Jun 19, 2019, 5:25:34 AM6/19/19
to ansible...@googlegroups.com
On 19.06.2019 08:29, Punit Jain wrote:
> Hi,
>
> I have a part of playbook task as below:
>
> - name: get url data
> uri:
> uri: <path>
> method: GET
> user: username
> password: password
> register: dns_result
> no_log: true
>
> - set_fact:
> dns_content: "{{ dns_result.content.records}}"
>
> Gives an error:
>
> "msg" : The task includes an option with an undefined variable. The
> error
> was ansible.vars.unsafe_proxy.AnsibleUnsafeText object' has no
> attribute
> 'records'

That is because is doesn't exist, you need to check what is contains
with

- debug: var=dns_result


> however when I set_fact separately as below, it works :
>
> - name: get url data
> uri:
> uri: <path>
> method: GET
> user: username
> password: password
> register: dns_result
> no_log: true
>
> - set_fact:
> dns_content: "{{ dns_result.content}}"
>
> - debug:
> msg: "{dns_contents.records}"

You only have one curly brackets so this is not treated as a variable
but as a string.
And if you add the missing curly brackets it will fail since you are
using dns_contents and not dns_content.


--
Kai Stian Olstad

Punit Jain

unread,
Jun 19, 2019, 8:26:49 AM6/19/19
to ansible...@googlegroups.com
Hi Kai,

It seems while copying i missed {{ but actually code does have this. So repasting code :

- name: get url data
  uri:
    uri: <path>
    method: GET
    user: username
    password: password
  register: dns_result
  no_log: true

- set_fact:
    dns_content: "{{ dns_result.content}}"

- debug:
    msg: "{{dns_content.records}}"

gives result:

ok: [10.4.12.5] =>
  "msg": [
      {
        "_links": {
           "self": {
               "href": "/api/name-services/dns/1e3w4r1q"
            }
          },
        "servers": [
             10.2.12.3"
          ],
      },
      {
         "_links": {
           "self": {
               "href": "/api/name-services/dns/1f7j9f0a"
            }
          },
        "servers": [
             10.2.122.4"
          ],
      },


The data in dns_result is :

ok: [10.4.12.5] =>
  "msg": [
      {
        "_links": {
           "self": {
               "href": "/api/name-services/dns?fields=*"
            }
          },
        "num_records": 2,
        "records": [
          {
            "_links": {
           "self": {
               "href": "/api/name-services/dns/1e3w4r1q"
            }
          },
        "servers": [
             10.2.12.3"
          ],
      },
      {
     ---- more data -----
      }

So why i cant access data like :

- set_fact:
    dns_content: "{{ dns_result.content.records}}"



--
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.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/00268525a8c184eff0795059cad93ea6%40olstad.com.
For more options, visit https://groups.google.com/d/optout.

Kai Stian Olstad

unread,
Jun 19, 2019, 11:22:01 AM6/19/19
to ansible...@googlegroups.com
On 19.06.2019 14:26, Punit Jain wrote:
> Hi Kai,
>
> It seems while copying i missed {{ but actually code does have this. So
> repasting code :
>
> - name: get url data
> uri:
> uri: <path>
> method: GET
> user: username
> password: password
> register: dns_result
> no_log: true
>
> - set_fact:
> dns_content: "{{ dns_result.content}}"

This doesn't add up, you say dns_result contain content, but you listing below show that dns_result doesn't contain content.
> "*records*": [
> {
> "_links": {
> "self": {
> "href": "/api/name-services/dns/1e3w4r1q"
> }
> },
> "servers": [
> 10.2.12.3"
> ],
> },
> {
> ---- more data -----
> }
>

According to this output dns_result is a list and there isn't a content, to get the records that would be {{ dns_result.0.records }}.


--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages