How to use variables from one task in another task

Hello Ansible Gurus,

I am new to Ansible and trying to get my hands dirty on playbooks. I wrote an yaml file to use a variable from one host tasks to another. Here is my code ( logically this code may not make sense in real world, I am trying to understand features in Ansible )

May not be exactly what you are looking for but you can have a new task with same variables but you use delegate_to another host.

  • name: “create a file in the directory”
    file:
    path: “/home/ansible_user/from_controller4/file_name”
    state: touch
    when: result is succeeded

delegate_to: another_host

No. But you can use hostvars to lookup variable for other hosts.

Hi Kai,

I have been trying various ways to get my playbook working by using hostvars.

Here are some of the ways I have tied, with no luck

Trial1 : when: {{ hostvars['hostname1][‘ansible_facts’][‘distribution’]}} == “CentOS”

Trial2: when: ansible_facts[‘hostname1’][‘distribution’] == “CentOS”

Can you help me with this?

Thanks
Srini.

Try this one

  when: "hostvars['hostname1']['ansible_facts']['distribution'] == 'CentOS'"

Hi Vladimir,

I have tried with your suggestion and got following error.

> > *Trial1* :
> > when: {{hostvars['hostname1]['ansible_facts']['distribution']}} ==
> > "CentOS"
>
> Try this one
> when: "hostvars['hostname1']['ansible_facts']['distribution'] ==
> 'CentOS'"

I have tried with your suggestion and got following error.
fatal: [172.19.251.173]: FAILED! => {"msg": "The conditional check
'hostvars['hostname1']['ansible_facts']['distribution'] == 'CentOS''
failed. The error was: error while evaluating conditional
(hostvars['hostname1']['ansible_facts']['distribution'] == 'CentOS'):
\"hostvars['hostname1']\" is undefined\n\n

Don't use undefined hostnames.

Hi Vladimir,

hostname1 is defined in my inventory file. Here is the yml file snippet

  • hosts: hostname2
    gather_facts: true
    tasks:

> > > > *Trial1* :
> > > > when: {{hostvars['hostname1]['ansible_facts']['distribution']}} ==
> > > > "CentOS"
> > >
> > > Try this one
> > > when: "hostvars['hostname1']['ansible_facts']['distribution'] ==
> > > 'CentOS'"
>
> > I have tried with your suggestion and got following error.
> > fatal: [172.19.251.173]: FAILED! => {"msg": "The conditional check
> > 'hostvars['hostname1']['ansible_facts']['distribution'] == 'CentOS''
> > failed. The error was: error while evaluating conditional
> > (hostvars['hostname1']['ansible_facts']['distribution'] == 'CentOS'):
> > \"hostvars['hostname1']\" is undefined\n\n

> Don't use undefined hostnames.

hostname1 is defined in my inventory file. Here is the yml file snippet

- hosts: hostname2
  gather_facts: true
  tasks:
    -
      file:
       path: "/home/ansible_user/controller3/file_name"
       state: touch
      when: "hostvars['hostname1']['ansible_facts']['distribution'] ==
'CentOS'"
When I comment'when'this code runs fine. Please suggest.

The playbook is running at 'hostname2' only and knows nothing about
'hostname1' at all. You might want to take a look at

    - debug:
        var: hostvars

Cheers,

  - vlado

Hello

  • debug:
    var: hostvars

shows all the hosts in my inventory file, so I believe my playbook has access to all hosts. To hash out issue with inventory file, I modified my code as below.

Sure. Attach the tarball with the output of "- debug: var=hostvars"

Sure. Attached output of hostvars.

(attachments)

hostvars.log (4.02 KB)

The distribution variable is a gather facts variable, and when you run against hostname2 only that variable is not accessible because Ansible has not state between playbook run.

One solution would be to use fact caching, or the easiest, just run gather facts on hostname1 first in the same playbook by adding following code before you play above.

- hosts: hostname1
  father_facts: true

Kai,

I don’t think it an issue with gather facts. Take a look at below code.

  • hosts: hostname2
    tasks:

Hello Ansible Gurus,

Any inputs for me to resolve this issue.

If I understand you correctly than try to do this. Make a folder host_var in your inventory folder. Than make a file with the exact same name as your host inside it,

something like: inventory/host_var/host1.yaml

Inside that file you'll put the vars and those you can you use global for that host.

Hi All,

Thanks for your time and efforts to help me out with the issue. I understood the mistak. I missed out making changes in the config file as described in below link

https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#caching-facts

TA