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

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

So you are saying that you want:

- CONFLUENT_SECURITY_MASTER_KEY be set?
- That value be persistent through the remainder of the playbook run?

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

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
  command: do something
  environment:
    CONFLUENT_SECURITY_MASTER_KEY: "{{ some_confluent_value }}"
  register: output
  changed_when: output.rc = 0

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