<p>It is a little bit tricky with Ansible. And also depends on the version you use. But I suppose you use relatively newest one.</p>
<p>I hope my answer helps, and if not, it gives you new ideas to explore :) And that I get your issue properly.</p>
<p>You can define global variable, however Ansible understoods it in specific way. If you declare it through configuration or environment vars, then it will be seen as &quot;global&quot;.</p>
<p>For example, you can define it in your <code>inventory</code> file:</p>
<pre><code>[myservers]
server1
server2
...

[myservers:vars]
foo=&quot;bar&quot;
</code></pre>
<p>You can define variable in <code>host</code> section of your play:</p>
<pre><code>- name: my play
  hosts: myservers
  vars:
    foo: bar
</code></pre>
<p>And also, you can work with <code>facts</code>, but as you already said, sometimes it doesn't look like the &quot;right&quot; option.</p>
<p>You can find some best practices for variables on <a href="https://spacelift.io/blog/ansible-variables" rel="nofollow noreferrer">Spacelift blog post</a> (disclaimer: I am Developer Advocate there).</p>

CC BY-SA 4.0
2023-08-02T07:45:20.827Z
2023-08-02T07:45:20.827Z
Pawel Piwosz
<p>I have an Ansible playbook, this playbook has a role on it, this role is configured to run only on 2 hosts:</p>
<pre><code>yaml
- hosts: node1,orchestrator1
  gather_facts: True
  any_errors_fatal: True
  remote_user: root
  roles:
    - role: configure_vault_backups
      when: &quot;add_vault_ds|bool == True&quot;
</code></pre>
<p>Inside the role there are some tasks that saves on some variables the output of some <code>shell</code> commands, BUT these tasks run only to one host (node1) <strong>OR</strong> the other host (orchestrator1), as an example:</p>
<pre><code>yaml
- name: Check if nfs_mount is online
  shell: mount | grep &quot;/var/lib/hypercx/datastores/nfs_mount&quot;
  register: nfs_mount_output
  ignore_errors: yes
  changed_when: false
  when: &quot;'orchestrator' in ansible_hostname and one_ip in ansible_all_ipv4_addresses&quot;
</code></pre>
<p>Now, I do have the variable with some values, BUT if I try to use this variable (in this case the variable es <code>nfs_mount_output</code>) in a task that will run on both hosts, when the role runs the role in the host that has NOT the variable defined in the previous tasks ansible complains telling me that the variable is not defined.</p>
<p>What I did was to create another task that set the a new variable with the previous variable, something like:</p>
<pre><code>yaml
- set_fact:
    nfs_mount_output_globlar: &quot;{{ nfs_mount_output }}&quot;
</code></pre>
<p>But I guess there is a better way to achieve it...</p>
<p>So the question is: can I make those variables somehow global(?) inside the task?</p>

<ansible>
Is there a way to declare global(?) variables inside an ansible task?
k.Cyborg
# Is there a way to declare global(?) variables inside an ansible task?

(Asked by: username_0 on 2023-07-28)

I have an Ansible playbook, this playbook has a role on it, this role is configured to run only on 2 hosts:
yaml
- hosts: node1,orchestrator1
  gather_facts: True
  any_errors_fatal: True
  remote_user: root
  roles:
    - role: configure_vault_backups
      when: "add_vault_ds|bool == True"

Inside the role there are some tasks that saves on some variables the output of some shell commands, BUT these tasks run only to one host (node1) OR the other host (orchestrator1), as an example:
yaml
- name: Check if nfs_mount is online
  shell: mount | grep "/var/lib/hypercx/datastores/nfs_mount"
  register: nfs_mount_output
  ignore_errors: yes
  changed_when: false
  when: "'orchestrator' in ansible_hostname and one_ip in ansible_all_ipv4_addresses"

Now, I do have the variable with some values, BUT if I try to use this variable (in this case the variable es nfs_mount_output) in a task that will run on both hosts, when the role runs the role in the host that has NOT the variable defined in the previous tasks ansible complains telling me that the variable is not defined.
What I did was to create another task that set the a new variable with the previous variable, something like:
yaml
- set_fact:
    nfs_mount_output_globlar: "{{ nfs_mount_output }}"

But I guess there is a better way to achieve it...
So the question is: can I make those variables somehow global(?) inside the task?

**Tags:** ansible


---

## 1 answers

### Answer by username_1 on 2023-08-02. Score: 1

It is a little bit tricky with Ansible. And also depends on the version you use. But I suppose you use relatively newest one.
I hope my answer helps, and if not, it gives you new ideas to explore :) And that I get your issue properly.
You can define global variable, however Ansible understoods it in specific way. If you declare it through configuration or environment vars, then it will be seen as "global".
For example, you can define it in your inventory file:
[myservers]
server1
server2
...

[myservers:vars]
foo="bar"

You can define variable in host section of your play:
- name: my play
  hosts: myservers
  vars:
    foo: bar

And also, you can work with facts, but as you already said, sometimes it doesn't look like the "right" option.
You can find some best practices for variables on Spacelift blog post (disclaimer: I am Developer Advocate there).


---

