I have a playbook that contains the two separate hosts one is 'localhost' and another is 'xyz'.
- hosts: localhost
connection: local
gather_facts: False
vars_prompt:
- name: "value"
prompt: "Enter your value"
private: yes
roles:
- a
###
- name: Define xyz
hosts: xyz
roles:
- a
- b
When we run the above playbook then in 'localhost' var_promot take the values from the user and after that, I want to use those values in the 'xyz' host roles in shell commands.
Currently, Inside 'a' role I have this type of code but it gives me an error.
- name: echo
shell: " echo {{ value | urlencode }}"
register: sout
Any idea how to do that or any other way for it?
CodePudding user response:
in first playbook, you could use a task which creates a dummy host variable_holder
and use a shared var: (use the module add_host
)
- name: add variables to dummy host
add_host:
name: "variable_holder"
shared_variable: "{{ value }}"
and in second play, you just recall the var shared
- name: Define xyz
hosts: xyz
vars:
value: "{{ hostvars['variable_holder']['shared_variable'] }}"
the 2 plays have to be in same playbook you launch....
CodePudding user response:
Neither vars_prompt nor roles is needed to reproduce the problem. Use simple vars and tasks instead. For example
- hosts: localhost
vars:
test_var: foo
tasks:
- debug:
var: test_var
- hosts: xyz
vars:
test_var: "{{ hostvars.localhost.test_var }}"
tasks:
- debug:
var: test_var
gives (debug output only)
ok: [localhost] =>
test_var: foo
ok: [xyz] =>
test_var: VARIABLE IS NOT DEFINED!
The variable test_var was declared in the first play and assigned to the host localhost. To use the variable in another host the dictionary hostvars is needed. But, the problem is that the variable test_var was not "instantiated". This means the variable was not included in the hostvars. Let's test it
- hosts: localhost
vars:
test_var: foo
tasks:
- debug:
var: test_var
- debug:
var: hostvars.localhost.test_var
- hosts: xyz
gather_facts: false
tasks:
- debug:
var: hostvars.localhost.test_var
gives (debug output only)
ok: [localhost] =>
test_var: foo
ok: [localhost] =>
hostvars.localhost.test_var: VARIABLE IS NOT DEFINED!
ok: [xyz] =>
hostvars.localhost.test_var: VARIABLE IS NOT DEFINED!
The problem will disappear when -e, --extra-vars is used
shell> ansible-playbook playbook.yml -e test_var=bar
gives (debug output only)
ok: [localhost] =>
test_var: bar
ok: [localhost] =>
hostvars.localhost.test_var: bar
ok: [xyz] =>
hostvars.localhost.test_var: bar
To fix the problem for other sources of variables "instantiate" the variable in the first play. Use set_fact
- hosts: localhost
vars:
test_var: foo
tasks:
- debug:
var: test_var
- debug:
var: hostvars.localhost.test_var
- set_fact:
test_var: "{{ test_var }}"
- debug:
var: hostvars.localhost.test_var
- hosts: xyz
vars:
test_var: "{{ hostvars.localhost.test_var }}"
tasks:
- debug:
var: test_var
gives (debug output only)
ok: [localhost] =>
test_var: foo
ok: [localhost] =>
hostvars.localhost.test_var: VARIABLE IS NOT DEFINED!
ok: [localhost] =>
hostvars.localhost.test_var: foo
ok: [xyz] =>
test_var: foo
Test other sources, e.g.
shell> cat host_vars/localhost
test_var: foo
- hosts: localhost
tasks:
- debug:
var: test_var
- set_fact:
test_var: "{{ test_var }}"
- hosts: xyz
vars:
test_var: "{{ hostvars.localhost.test_var }}"
tasks:
- debug:
var: test_var
gives (debug output only)
ok: [localhost] =>
test_var: foo
ok: [xyz] =>
test_var: foo