Pass output variable of one role as an input to another role

40 views
Skip to first unread message

Ukesh Kumar Vasudevan

unread,
Mar 20, 2018, 11:41:41 AM3/20/18
to Ansible Project
Hi,

I am having the below ansible script.

While executing the script, I am getting error in the role 'install-packages'. The error is,

"msg": "The task includes an option with an undefined variable. The error was: {{ ip }}: 'ip' is undefined.

It is looking for 'ip' variable inside the role 'install-packages',

Please provide your feedback..

---

# Create an EC2 instance

- name: Create EC2 Instance
  hosts: "local"
  connection: "local"
  roles:
    - ec2-provision

- name: set_facts
  hosts: local
  tasks:
    - set_fact:
        ip: "{{ ec2.instances[0].private_ip }}"
    - debug:
        var: ip

- name: Install packages
  hosts: new_vms
  gather_facts: yes
  vars:
    ip_addrs: "{{ ip }}"
  roles:
   - install-packages


Kai Stian Olstad

unread,
Mar 22, 2018, 4:33:15 PM3/22/18
to ansible...@googlegroups.com
You are setting the variable on host local not on host new_vms so the
variable ip doesn't exist there.
To access the variable you need to use hostvars and specify which host
you like the get the ip variable from.

{{ hostvars['local'].ip }}

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