# Using a variable from one play across other plays

**URL:** <https://forum.ansible.com/t/using-a-variable-from-one-play-across-other-plays/45471>\
**Category:** Get Help\
**Created:** [March 3, 2026, 4:16pm UTC](https://forum.ansible.com/t/using-a-variable-from-one-play-across-other-plays/45471 "2026-03-03T16:16:02Z")\
**Posts on this page:** 12\
**Page:** 2

<div class="post-metadata">

**Author:** ![kks](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kks/32/10352_2.png) [@kks](https://forum.ansible.com/u/kks)\
**Post date:** [March 10, 2026, 7:53am UTC](https://forum.ansible.com/t/using-a-variable-from-one-play-across-other-plays/45471/21 "2026-03-10T07:53:50Z")

</div>

> [@bartowl](#):
>
> not add a play at the very top with hosts: all,

With anchors no need to have any tasks at all. This will also work:

```yaml
---
- name: Testing Bitwarden lookups
  hosts: localhost
  gather_facts: false
  tasks: []
  vars:
    global_tasks:
    - &set_fact_bitwarden
      name: Get Bitwarden password for badmin lab user
      ansible.builtin.set_fact:
        ansible_become_pass: >-
          {{ lookup('community.general.bitwarden', 'badmin - Lab User', field='password')[0] }}
        delegate_to: localhost

- name: Test using Bitwarden varaible
  hosts: ansible
  gather_facts: false
  tasks:
    - *set_fact_bitwarden

    - name: Check if `/etc/profile.d/ps1.sh` exists
      ansible.builtin.stat:
        path: /etc/profile.d/ps1.sh
      become: true
      register: stat_ps1_sh

```

There will be no LSP highlights as “global task” is not a task from ansible point of view

Anchor just copies the content from one place to another.  
I did not want to make a huge jump right to this final solution on the first post as it might confuse people.

---

<div class="post-metadata">

**Author:** ![bartowl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bartowl/32/7029_2.png) [@bartowl](https://forum.ansible.com/u/bartowl)\
**Post date:** [March 10, 2026, 8:32am UTC](https://forum.ansible.com/t/using-a-variable-from-one-play-across-other-plays/45471/22 "2026-03-10T08:32:14Z")

</div>

> [@kks](#):
>
> With anchors no need to have any tasks at all. This will also work:

Well yes and no… Anchors work just inside a single yaml file, as they are yaml objects. When ansible reads tihs file it is as if you would copy and paste the same code at multiple places.

Having this code at multiple places means it would be execuded MULTIPLE TIMES. This might not be desired, especially when running on an inventory with thousands of hosts - you do not want (not need) to re-execute this task for each play!

My suggested approach eecutes this lookup just a single time for entire playbook file regardless of number of hosts. It assigns a set\_fact level variable to all hosts and it is visible in ALL following plays without the need to re-execute it.

I know that anchors might get handy in some places, but for me this is a quick and dirty hack to avoid copy/paste same code within same file. I however do not like the idea to have same code in one file at multiple places in the first place…

---

<div class="post-metadata">

**Author:** ![bryonadams](https://avatars.discourse-cdn.com/v4/letter/b/67e7ee/32.png) [@bryonadams](https://forum.ansible.com/u/bryonadams)\
**Post date:** [March 19, 2026, 4:08pm UTC](https://forum.ansible.com/t/using-a-variable-from-one-play-across-other-plays/45471/23 "2026-03-19T16:08:36Z")

</div>

> [@bartowl](#):
>
> Hmm, why not add a play at the very top with hosts: all, a single task set\_facts from lookup but with run\_once true? This would call the task just once for a random host (typically first) without really connecting to it (as this is set\_facts).

Hi Bartowl, I’m having a bit of trouble envisioning what’s different from your suggestion and my original post (and what I’ve done wrong in that context). Ideally, I’m not installing Bitwarden and syncing that everywhere, I want to do the lookup from the place I’m running the playbook. The sync is a manual process from the CLI tool, and I have to grab a session key once I log in. Originally, I was running this I think as you described, except I was delegating it and having trouble.

I definitely didn’t really like that this was running more than once.

---

<div class="post-metadata">

**Author:** ![shertel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/shertel/32/1241_2.png) [@shertel](https://forum.ansible.com/u/shertel)\
**Post date:** [March 19, 2026, 4:25pm UTC](https://forum.ansible.com/t/using-a-variable-from-one-play-across-other-plays/45471/24 "2026-03-19T16:25:02Z")

</div>

You can use `run_once` in the play targeting your ansible\_test\_vm group. It will run once and you can access the fact directly instead of through hostvars:

```yaml
- name: Use Bitwarden pass from task
  hosts: ansible_test_vm
  tasks:
    - name: 'Get password for "badmin - Lab User" from Bitwarden'
      set_fact:
        bw_data: >-
          {{ lookup('community.general.bitwarden', 'badmin - Lab User', field='password')[0] }}
        cacheable: True
      run_once: True

    - name: Check if `/etc/profile.d/ps1.sh` exists
      ansible.builtin.stat:
        path: /etc/profile.d/ps1.sh
      become: true
      register: stat_ps1_sh

  vars:
    ansible_become_pass: "{{ bw_data | default(omit) }}"

```

> Originally, I was running this I think as you described, except I was delegating it and having trouble.

Your error was unrelated to delegation, it was caused by how you defined `vars:`. In this example I used `| default(omit)`, but see my comment above for other options to avoid that issue (regardless of delegation).

---

<div class="post-metadata">

**Author:** ![bryonadams](https://avatars.discourse-cdn.com/v4/letter/b/67e7ee/32.png) [@bryonadams](https://forum.ansible.com/u/bryonadams)\
**Post date:** [March 19, 2026, 6:01pm UTC](https://forum.ansible.com/t/using-a-variable-from-one-play-across-other-plays/45471/25 "2026-03-19T18:01:33Z")

</div>

Hi Shertel, I don’t want to run the play to get the password against the same host as I do other things. I either have to delegate it or have a play to grab it. It’s always worked just fine if I do everything on the same host as I grab the password.

---

<div class="post-metadata">

**Author:** ![shertel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/shertel/32/1241_2.png) [@shertel](https://forum.ansible.com/u/shertel)\
**Post date:** [March 19, 2026, 6:06pm UTC](https://forum.ansible.com/t/using-a-variable-from-one-play-across-other-plays/45471/26 "2026-03-19T18:06:10Z")

</div>

Just add `delegate_to: localhost` on that task in my example.

> It’s always worked just fine if I do everything on the same host as I grab the password.

Without `run_once` it runs on every host you target. With `run_once` it runs once and you can access the fact without using hostvars.

---

<div class="post-metadata">

**Author:** ![bryonadams](https://avatars.discourse-cdn.com/v4/letter/b/67e7ee/32.png) [@bryonadams](https://forum.ansible.com/u/bryonadams)\
**Post date:** [March 19, 2026, 6:29pm UTC](https://forum.ansible.com/t/using-a-variable-from-one-play-across-other-plays/45471/27 "2026-03-19T18:29:02Z")

</div>

I swear I did that before (before I tried the variable thing in the original post) but maybe I had a typo or something.

So now I have this (what you recommended) and it works.

```auto
- name: Test using Bitwarden varaible
  hosts: ansible_test_vm
  gather_facts: false
  tasks:
    - name: Get Bitwarden password for badmin lab user
      ansible.builtin.set_fact:
        bw_data: >-
          {{ lookup('community.general.bitwarden', 'badmin - Lab User', field='password')[0] }}
        cacheable: true
      delegate_to: localhost

    - name: Check if `/etc/profile.d/ps1.sh` exists
      ansible.builtin.stat:
        path: /etc/profile.d/ps1.sh
      become: true
      register: stat_ps1_sh

  vars:
    ansible_become_pass: "{{ bw_data | default(omit) }}"

```

If I have some other play further down that targets some other group of VMs for some reason, would I then have to run it again on that new group of hosts? I assumed something like the below would be cleaner since I’d only do it a single time but the variable isn’t persistent for the second play.

```auto
- name: Testing Bitwarden lookups
  hosts: localhost
  gather_facts: false
  tasks:
    - name: Get Bitwarden password for badmin lab user
      ansible.builtin.set_fact:
        bw_data: >-
          {{ lookup('community.general.bitwarden', 'badmin - Lab User', field='password')[0] }}
        cacheable: true
      run_once: true

- name: Test using Bitwarden varaible
  hosts: ansible
  gather_facts: false
  tasks:
    - name: Check if `/etc/profile.d/ps1.sh` exists
      ansible.builtin.stat:
        path: /etc/profile.d/ps1.sh
      become: true
      register: stat_ps1_sh

  vars:
    ansible_become_pass: "{{ bw_data | default(omit) }}"

```

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [March 19, 2026, 6:31pm UTC](https://forum.ansible.com/t/using-a-variable-from-one-play-across-other-plays/45471/28 "2026-03-19T18:31:30Z")

</div>

A few clarifications:

- set\_fact runs on the controller FOR the inventory\_hostname, not on the hosts, this is true for many actions like ‘debug’, see the ‘attributes’ section, anything not using the connection should behave this way
- run\_once has several side effects, one of them is to apply results to all hosts
- the result of set\_fact is to set a variable on a host .. so it kind of does run set\_fact for every host, you just skip the parts that show in the callback
- when using this strategy you are making N copies of the variables (N == number of hosts), when using the localhost/hostvars[‘localhost’] you only have 1 copy of the variable

---

<div class="post-metadata">

**Author:** ![shertel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/shertel/32/1241_2.png) [@shertel](https://forum.ansible.com/u/shertel)\
**Post date:** [March 19, 2026, 7:09pm UTC](https://forum.ansible.com/t/using-a-variable-from-one-play-across-other-plays/45471/29 "2026-03-19T19:09:48Z")

</div>

That is not what I suggested, and I don’t know why it would work unless localhost is the only host in that group. You asked how `run_once` was different from your original post, so my example was just a demonstration of how to use it, not a recommendation per se.

If you want one copy of the variable or want that variable accessible for other plays, I’d write it as:

```yaml
- name: Use Bitwarden pass from task
  hosts: ansible_test_vm
  tasks:
    # You could move this to its own play targeting localhost and remove delegate_to and delegate_facts if you want.
    - name: 'Get password for "badmin - Lab User" from Bitwarden'
      set_fact:
        bw_data: >-
          {{ lookup('community.general.bitwarden', 'badmin - Lab User', field='password')[0] }}
        cacheable: True
      delegate_to: localhost
      delegate_facts: True

    - name: Check if `/etc/profile.d/ps1.sh` exists
      ansible.builtin.stat:
        path: /etc/profile.d/ps1.sh
      become: true
      register: stat_ps1_sh

  vars:
    ansible_become_pass: "{{ hostvars['localhost']['bw_data'] | default(omit) }}" # You were missing "| default(omit)" in the original example (although IMO this is a bug in ansible-core)

- hosts: ansible
  vars:
    ansible_become_pass: "{{ hostvars['localhost']['bw_data'] }}" # Note that "| default(omit)" is no longer necessary since the fact was defined in an earlier play that always runs

```

---

<div class="post-metadata">

**Author:** ![bryonadams](https://avatars.discourse-cdn.com/v4/letter/b/67e7ee/32.png) [@bryonadams](https://forum.ansible.com/u/bryonadams)\
**Post date:** [March 19, 2026, 7:45pm UTC](https://forum.ansible.com/t/using-a-variable-from-one-play-across-other-plays/45471/30 "2026-03-19T19:45:19Z")

</div>

That makes sense. Thanks a lot, I appreciate it!

---

<div class="post-metadata">

**Author:** ![kks](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kks/32/10352_2.png) [@kks](https://forum.ansible.com/u/kks)\
**Post date:** [March 20, 2026, 6:43am UTC](https://forum.ansible.com/t/using-a-variable-from-one-play-across-other-plays/45471/31 "2026-03-20T06:43:43Z")

</div>

> [@bartowl](#):
>
> Having this code at multiple places means it would be execuded MULTIPLE TIMES

Yes, you are right. To be honest I forgot that inside one playbook set\_fact is visible in different plays. Scope of what is available where is not good documented for ansible.  
There are still use case where one would want to have lookup to be executed for each host, for instance when each host has different credentials.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [April 19, 2026, 6:44am UTC](https://forum.ansible.com/t/using-a-variable-from-one-play-across-other-plays/45471/32 "2026-04-19T06:44:07Z")

</div>

Closed on April 19, 2026, 6:44am UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.

[Previous page](https://forum.ansible.com/t/using-a-variable-from-one-play-across-other-plays/45471.md?page=1)
