Transfer Ansible variables from one role (running on one host) to another role that is running on another host in the same playbook

I have a playbook that plays different roles on different hosts. Is it possible to transfer a variable from one role running on one host to another role on another host running in the same run? Or any workaround?

playbook
   host1
     role1
       here I get some variables: var1 var2 ...etc
   host2
     role2
       here I need to use var1 var2 ... etc from the above host/role

The task in role1, which sets the variable db, is as follows:

- shell: cd /ACE/conf && grep ^db.url local1.properties | awk -F/ '{print $4}' | awk -F? '{print $1}'
  register: db

UPDATE: on the first host, the values ​​are dynamic, it is like a configuration file that is always updated. After I save the values ​​in variables on host 1 with role1, I then go to host2, run role2 and make stuff with these values ​​from the variables stored in host1.

I tried with hostvars:

{{ hostvars.LBL.db.stdout }}
{{ hostvars['LBL']['db'] }}
{{ hostvars['LBL']['db']['stdout'] }}

:

in get_variables raise Exception("host not found: %s" % hostname) Exception: host not found: LBL

LBL ,

, , . . ?

hostvars :

---   
- name: test hostvars host1
  hosts: LBL
  tasks:
    - command: "ls /bin"
        register: ls_out

- name: test hostvars host2
  hosts: LM
  tasks:
    - debug:
        var: "{{ hostvars['LBL']['ls_out']['stdout'] }}"

:

fatal: [10.104.148.138] => host not found: LBL

/ ..//

[root@NS1 ansible]# cat /etc/ansible/hosts
[LBL]
10.104.148.136
[LM]
10.104.148.138
+7
5

.

:

fatal: [10.104.148.138] => host not found: LBL

, LBL - , . LBL : 10.104.148.136

:

1. (/etc/ansible/hosts) :

LBL ansible_ssh_host=10.104.148.136
LM ansible_ssh_host=10.104.148.138

2. , , LBL - , , :

{{ hostvars['10.104.148.136']['db']['stdout'] }}

LBL - . .

+6

, , .

,

:

---
- hosts: host1
- roles:
    - role1
    - role2

: role1 role2.

set_fact.

role1:

name: save precious value
set_fact: 
  pantsu: shiroi

role2:

name: Nozoki...
debug: msg="Color is {{ pantsu }}"

( )

:

[group_foo]
host1
host2
[group_bar]
host3
host4

group_vars/group_foo

important_value=bla-bla-ba

: playbook group2.

.

group_vars/group_bar

other_var: '{{hostvars[groups["group_foo"][0]].important_value}}'

, "0".

+12

. , : ansible-playbook sync.yaml -e "source = host1 destination = host2"

:

---

- name: get_sync_facts
  hosts: "{{ source }}"
  roles:
    - set_sync_facts

- name: sync
  hosts: "{{ destination }}" 
  roles:
    - get_sync_facts
    - sync

set_sync_facts:

---

- set_fact: src_media_dir='/some/dir/'
- set_fact: src_user='myuser'
- set_fact: src_host='1.1.1.1'
- set_fact: src_port=12345
- set_fact: src_db_user='dbuser'
- set_fact: src_db_password='something'
- set_fact: src_db_name='some_db'

( -, )

get_sync_facts:

---

- set_fact: src_media_dir={{ hostvars[source]['src_media_dir'] }}
- set_fact: src_user={{ hostvars[source]['src_user'] }}
- set_fact: src_host={{ hostvars[source]['src_host'] }}
- set_fact: src_port={{ hostvars[source]['src_port'] }}
- set_fact: src_db_user={{ hostvars[source]['src_db_user'] }}
- set_fact: src_db_password={{ hostvars[source]['src_db_password'] }}
- set_fact: src_db_name={{ hostvars[source]['src_db_name'] }}

- , , set_sync_facts.

+2

, , , -. sed "target", .

"" 1 .

:

[ansible_local]
localhost

[machine1]
machine1.domain.tld

upgrade_packages.yml

---
- hosts: '{{ target }}'
  sudo: yes
  tasks:
  - name: check for Debian system
    shell: /bin/false
    when: ansible_pkg_mgr != "apt"

  - name: full-upgrade all packages
    apt: update_cache=yes upgrade=full
    register: upgrade_result

- hosts: ansible_local
  tasks:
  - name: find out host from target
    shell: /bin/sed -n -e '/^\[{{ target }}\]$/,/^\[.*\]$/ { /^\[/d; /^$/d; p; }' {{ inventory_file }}
    register: target_inventory

  - name: Display all facts from target machine (change when to true if needed)
    debug: var=hostvars[target_inventory.stdout]
    when: false

  - name: Display upgrade result on ansible_local node
    debug: var=hostvars[target_inventory.stdout].upgrade_result.msg

: ansible-playbook ./upgrade-packages.yml -e "target=machine1" -v

+1

, h1, h2, h3, { "important_fact": "foo" } ( bar baz )

, _ , :

- set_fact:
    important_facts_list: "{{ hostvars | json('*.important_fact') }}"
  run_once: yes
  delegate_to: h1

important_facts_list [ 'foo', 'bar', 'baz' ], with_items.

0

All Articles