3

Ansible Version: 2.4.2.0

From here and here, I was able write below playbook

$ cat test.yml 
- name: Finding Master VMs
  hosts: all-compute-host
  remote_user: heat-admin
  tasks:
  - name: Getting master VM's hostname
    shell: hostname
    register: hostname_output

- name: Access in different play
  hosts: localhost
  connection: local

  tasks:
  - name: Testing vars
    debug: var='{{ hostvars[item]['hostname_output']['stdout'] }}'
    with_items: groups['all-compute-host']

I don't want use gather_facts: true and access hostname from it. Getting below error when I try to above playbook

-----------OUTPUT REMOVED----------------
TASK [Testing vars] *******************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: u\"hostvars['groups['all-compute-host']']\" is undefined\n\nThe error appears to have been in '/tmp/test.yml': line 18, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n  tasks:\n  - name: Testing vars\n    ^ here\n\nexception type: <class 'ansible.errors.AnsibleUndefinedVariable'>\nexception: u\"hostvars['groups['all-compute-host']']\" is undefined"}
    to retry, use: --limit @/tmp/test.retry
-----------OUTPUT REMOVED----------------

I tried below things

  1. debug: var=hostvars[item]['hostname_output']['stdout']
  2. debug: var=hostvars.item.hostname_output.stdout'

I have also tried direct host group instead of iterating item like below

  1. debug: var=hostvars.all-compute-host.hostname_output.stdout'
  2. debug: var=hostvars['all-compute-host']['hostname_output']['stdout']
ok: [localhost] => {
    "hostvars['all-compute-host']['hostname_output']['stdout']": "VARIABLE IS NOT DEFINED!"
}

I have also try direct host name.But no use

  1. debug: var='hostvars.compute01.hostname_output.stdout'

1 Answer 1

2

The problem is with your debug statement:

tasks:
  - name: Testing vars
    debug: var='{{ hostvars[item]['hostname_output']['stdout'] }}'
    with_items: groups['all-compute-host']

There are two issues:

  • The value of the var argument to the debug module is evaluated in a Jinja templating context. That means you must not use the {{...}} template markers.

  • On the hand, the argument to with_items does need the {{...}} markers; without that, you're iterating over a list consisting of a single item, the literal string groups['all-compute-host'].

With both of those problems fixed (and a few minor stylistic changes), you get:

tasks:
  - name: Testing vars
    debug:
      var: hostvars[item].hostname_output.stdout
    with_items: "{{ groups['all-compute-host'] }}"
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.