Sharing ansible variable between plays
Solution 1:
A value you set with set_fact
will be available between different plays. Keep in mind that set_fact are set for a specific host. Your first play is run against localhost so the fact is part of the localhosts variables. So it the following play you should be able to access it with a task like this.
- debug:
var: hostvars['localhost']['hostmap']
Solution 2:
You could use dummy host which will store all the variables you want to share with a help of add_host. Then just access your variables with hostvars
- hosts: "18.184.109.70"
user: ubuntu
tasks:
- name: set a variable
set_fact:
shared_variable: "Some value"
- name: add variables to dummy host
add_host:
name: "variable_holder"
shared_variable: "{{ shared_variable }}"
- hosts: "18.196.135.59"
user: ubuntu
vars:
shared_variable: "{{ hostvars['variable_holder']['shared_variable'] }}"
tasks:
- debug:
msg: "{{ shared_variable }}"