Q: "Set env vars in a play and use in the next play."
A: It is not possible. You can set environment in the scope of a play. There is no such thing as a 'scope of a playbook'. For example, the second play knows nothing about the environment set in the first play only
- hosts: localhost
environment:
MY_VAR1: my variable 1
tasks:
- command: echo $MY_VAR1
register: out
- debug:
var: out.stdout
- hosts: localhost
tasks:
- command: echo $MY_VAR1
register: out
- debug:
var: out.stdout
gives (abridged)
out.stdout: my variable 1
out.stdout: $MY_VAR1
As a workaround, you can put the environment into the vars. For example, into the group_vars/all
shell> cat group_vars/all/my_env.yml
my_env:
MY_VAR1: my variable 1
MY_VAR2: my variable 2
Then, you can use it in each play
- hosts: localhost
environment: "{{ my_env }}"
tasks:
- command: echo $MY_VAR1
register: out
- debug:
var: out.stdout
- hosts: localhost
environment: "{{ my_env }}"
tasks:
- command: echo $MY_VAR1
register: out
- debug:
var: out.stdout
gives (abridged)
out.stdout: my variable 1
out.stdout: my variable 1
Q: "Set env vars in a play and use ansible.builtin.env"
A: The lookup plugin env never returns environment set via the keyword environment
- hosts: localhost
environment:
MY_VAR1: my variable 1
tasks:
- debug:
msg: "{{ lookup('ansible.builtin.env', 'SHELL') }}"
- debug:
msg: "{{ lookup('ansible.builtin.env', 'MY_VAR1') }}"
gives (abridged)
msg: /bin/bash
msg: ''
Any lookup plugin is always running on the localhost. The plugin env returns the environment inherited from the shell running the ansible-* utility.
Both command and shell modules do not run the commands in the inherited environment. For example,
shell> cat pb.yml
- hosts: localhost
environment:
MY_VAR1: my variable 1
tasks:
- debug:
msg: "{{ lookup('ansible.builtin.env', 'MY_VAR3') }}"
- command: echo $MY_VAR3 $MY_VAR1
register: out
- debug:
var: out.stdout
gives (abridged)
shell> MY_VAR3='my variable 3' ansible-playbook pb.yml
msg: my variable 3
out.stdout: $MY_VAR3 my variable 1