# How to set-up playbook level Environment variable after a few tasks

**URL:** <https://forum.ansible.com/t/how-to-set-up-playbook-level-environment-variable-after-a-few-tasks/36483>\
**Category:** Ansible Project\
**Created:** [March 27, 2022, 2:57am UTC](https://forum.ansible.com/t/how-to-set-up-playbook-level-environment-variable-after-a-few-tasks/36483 "2022-03-27T02:57:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arundhati\_Bhende](https://avatars.discourse-cdn.com/v4/letter/a/34f0e0/32.png) [@Arundhati\_Bhende](https://forum.ansible.com/u/Arundhati_Bhende)\
**Post date:** [March 27, 2022, 2:57am UTC](https://forum.ansible.com/t/how-to-set-up-playbook-level-environment-variable-after-a-few-tasks/36483/1 "2022-03-27T02:57:54Z")

</div>

I need to set a playbook level Environment variable after executing some tasks. The variable needs to be set-up separately on each server by reading a file on the server – I am told I should use slurp, but do not know how to use it to define playbook level environment variable.

Can someone help?

This is the current version of the playbook

- name: server properties

hosts: kafka\_broker

vars:

ansible\_ssh\_extra\_args: “-o StrictHostKeyChecking=no”

ansible\_host\_key\_checking: false

date: “{{ lookup(‘pipe’, ‘date +%Y%m%d-%H%M%S’) }}”

copy\_to\_dest: “/export/home/kafusr/kafka/secrets”

server\_props\_loc: “/etc/kafka”

secrets\_props\_loc: “{{ server\_props\_loc }}/secrets”

environment:

CONFLUENT\_SECURITY\_MASTER\_KEY: "”

tasks:

- name: Create a directory if it does not exist

file:

path: “{{ copy\_to\_dest }}”

state: directory

mode: ‘0755’

- name: Find files from “{{ server\_props\_loc }}”

find:

paths: /etc/kafka/

patterns: “server.properties\*”

# … the rest of the task

register: etc\_kafka\_server\_props

- name: Find files from “{{ secrets\_props\_loc }}”

find:

paths: /etc/kafka/secrets

patterns: “\*”

# … the rest of the task

register: etc\_kafka\_secrets\_props

- name: Copy the files

copy:

src: “{{ item.path }}”

dest: “{{ copy\_to\_dest }}”

remote\_src: yes

loop: “{{ etc\_kafka\_server\_props.files + etc\_kafka\_secrets\_props.files }}”

Need help is setting up CONFLUENT\_SECURITY\_MASTER\_KEY after the copy files task to read the a file from secrets directory using slurp, and have it avaibale at playbook level

Regards

---

<div class="post-metadata">

**Author:** ![rilindo](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/rilindo/32/357_2.png) [@rilindo](https://forum.ansible.com/u/rilindo)\
**Post date:** [March 28, 2022, 2:57pm UTC](https://forum.ansible.com/t/how-to-set-up-playbook-level-environment-variable-after-a-few-tasks/36483/2 "2022-03-28T14:57:29Z")

</div>

So you are saying that you want:

- CONFLUENT\_SECURITY\_MASTER\_KEY be set?  
- That value be persistent through the remainder of the playbook run?

---

<div class="post-metadata">

**Author:** ![Arundhati\_Bhende](https://avatars.discourse-cdn.com/v4/letter/a/34f0e0/32.png) [@Arundhati\_Bhende](https://forum.ansible.com/u/Arundhati_Bhende)\
**Post date:** [March 28, 2022, 4:32pm UTC](https://forum.ansible.com/t/how-to-set-up-playbook-level-environment-variable-after-a-few-tasks/36483/3 "2022-03-28T16:32:24Z")

</div>

Yes. But the value will be different on each of the target machines and can be determined after the copy tasks are executed

---

<div class="post-metadata">

**Author:** ![rilindo](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/rilindo/32/357_2.png) [@rilindo](https://forum.ansible.com/u/rilindo)\
**Post date:** [March 28, 2022, 5:13pm UTC](https://forum.ansible.com/t/how-to-set-up-playbook-level-environment-variable-after-a-few-tasks/36483/4 "2022-03-28T17:13:35Z")

</div>

Alright. My thought would be to register the master key as a fact in one play:

register: some\_confluent\_value

And you set the environment variable on the next play that needs it.

- name: some confluent operation  
&nbsp;&nbsp;command: do something  
&nbsp;&nbsp;environment:  
&nbsp;&nbsp;&nbsp;&nbsp;CONFLUENT\_SECURITY\_MASTER\_KEY: "{{ some\_confluent\_value }}"  
&nbsp;&nbsp;register: output  
&nbsp;&nbsp;changed\_when: output.rc = 0

---

<div class="post-metadata">

**Author:** ![Arundhati\_Bhende](https://avatars.discourse-cdn.com/v4/letter/a/34f0e0/32.png) [@Arundhati\_Bhende](https://forum.ansible.com/u/Arundhati_Bhende)\
**Post date:** [March 28, 2022, 5:18pm UTC](https://forum.ansible.com/t/how-to-set-up-playbook-level-environment-variable-after-a-few-tasks/36483/5 "2022-03-28T17:18:27Z")

</div>

But, this would mean, I have to set it as environment in all the subsequent plays, right? Not at the playbook level?
