Created
November 30, 2022 06:19
-
-
Save frogstarr78/f0b56daaeba8188a2db8ece632af00d0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- hosts: localhost | |
gather_facts: no | |
vars: | |
site: example.com | |
user: bob | |
scheme: https | |
env_path: "{{ playbook_dir }}/tmp/environment" | |
ansible_connection: local | |
tasks: | |
- name: Create /etc/environment | |
copy: | |
dest: "{{ env_path }}" | |
content: | | |
SITE={{ site }} | |
USER={{ user }} | |
SCHEME={{ scheme }} | |
- name: | | |
Print site variable using environment | |
According to the documentation: https://docs.ansible.com/ansible/devel/collections/ansible/builtin/env_lookup.html#synopsis the env lookup ONLY works for variables that exist when you called the playbook. | |
So this will fail unless you call it like this: | |
SITE=example.edu ansible-playbook t.yml | |
Remember too, using lookup ONLY works on the controller | |
debug: | |
msg: "{{ lookup('env', 'SITE') }}" | |
environment: | |
SITE: "{{ site }}" | |
- name: Print site variable using jinja | |
debug: | |
msg: "{{ site }}" | |
- name: Use variable in shell using environment | |
shell: "echo $SCHEME://$SITE" | |
environment: | |
SCHEME: "{{ scheme }}" | |
SITE: "{{ site }}" | |
- name: Use variable in shell using jinja | |
shell: "echo {{ scheme }}://{{ site }}" | |
- name: Display set HOME variable (just to know it's working) | |
debug: | |
msg: "{{ lookup('env', 'HOME') }}" | |
- name: Display unset SITE variable | |
debug: | |
msg: "{{ lookup('env', 'SITE') }}" | |
- block: | |
- name: Parse and display /etc/environment variables as a dictionary | |
debug: | |
msg: "{{ parsed_environment }}" | |
- name: | | |
Parse the /etc/environment variables and display only the SITE variable (but ONLY if you're running this on localhost because lookups ALWAYS run on localhost and not the remote machine) | |
According to the documentation: https://docs.ansible.com/ansible/devel/collections/ansible/builtin/env_lookup.html#synopsis the env lookup ONLY works for variables that exist when you called the playbook. | |
So this will fail unless you call it like this: | |
SITE=example.edu ansible-playbook t.yml | |
Remember too, using lookup ONLY works on the controller | |
debug: | |
msg: "{{ lookup('env', 'SITE') }}" | |
environment: | | |
{{ parsed_environment }} | |
- name: Parse the /etc/environment variables and use the SITE variable in a shell call using environment | |
shell: "echo $SITE" | |
environment: | | |
{{ parsed_environment }} | |
- name: use the site variable in a shell call using jinja | |
shell: "echo {{ site }}" | |
environment: | | |
{{ parsed_environment }} | |
vars: | |
parsed_environment: "{{ lookup('file', env_path).split('\n') | map('split', '=') | list | community.general.dict }}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment