Ansible - Register variable in not defined

1,861 views
Skip to first unread message

James Gibson

unread,
Mar 22, 2019, 11:06:31 AM3/22/19
to Ansible Project
I am trying to figure out why my variable is not defined and have not been able to find any solutions online.
Can anyone help?


test.yml
---
- name: Test playbook
  hosts: localhost
  connection: local
  vars: 
      test: hello

  tasks:
     - name: test var
       debug:
           msg: "{{ test}}"
       register: test_out
       debug:
           var: test_out

output from playbook:
PLAY [Test playbook] ***********************************************************************************************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************************************************************************************************
ok: [localhost]

TASK [test var] ****************************************************************************************************************************************************************************************
ok: [localhost] => {
    "test_out": "VARIABLE IS NOT DEFINED!"
}

PLAY RECAP *********************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0

syed.qasi...@gmail.com

unread,
Mar 22, 2019, 11:17:40 AM3/22/19
to Ansible Project
Try this it worked for me, A play takes a list of tasks so each tasks should start with dash `-`:

---
- name: Test playbook
  hosts: localhost
  connection: local
  vars:
      test: hello

  tasks:
     - name: test var
       debug:
           msg: "{{ test }}"
       register: test_out

     - debug:
           var: test_out

James Gibson

unread,
Mar 22, 2019, 11:57:09 AM3/22/19
to Ansible Project
Thanks Syed. I added a validation step and that is failing. 

Updated playbook
-----------------------------------
---
  2 - name: Test playbook
  3   hosts: localhost
  4   connection: local
  5   vars:
  6       test: hello
  7
  8   tasks:
  9      - name: test var
 10        debug:
 11            msg: "{{ test }}"
 12        register: test_out
 13
 14      - debug:
 15            var: test_out
 16
 17      - name: verify var
 18        assert:
 19          that:
 20            - "'{{ test_out.test }}' == hello'"


Output:
PLAY [Test playbook] ***********************************************************************************************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************************************************************************************************
ok: [localhost]

TASK [test var] ****************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "hello"
}

TASK [debug] *******************************************************************************************************************************************************************************************
ok: [localhost] => {
    "test_out": {
        "changed": false,
        "failed": false,
        "msg": "hello"
    }
}

TASK [verify var] **************************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'test'\n\nThe error appears to have been in '/home/ox/test.yml': line 17, column 8, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n     - name: verify var\n       ^ here\n"}
        to retry, use: --limit @/home/ox/test.retry

PLAY RECAP *********************************************************************************************************************************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=1

Qasim Sarfraz

unread,
Mar 22, 2019, 12:02:27 PM3/22/19
to ansible...@googlegroups.com
 "test_out": {
        "changed": false,
        "failed": false,
        "msg": "hello"
    }

there is no key named test here. You need to use 'test_out.msg' above.

--
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/d81834fd-51c2-4da0-a5bc-aabf2a2a1d90%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

James Gibson

unread,
Mar 22, 2019, 12:11:17 PM3/22/19
to Ansible Project
Yes that worked, but for the client I am working with, I am unable to change the validation. How would I define it so that test_out.test would work?

Kai Stian Olstad

unread,
Mar 22, 2019, 1:06:04 PM3/22/19
to ansible...@googlegroups.com
You can't since debug doesn't return test.


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