Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible - environment variables from .env file

I am trying to setup a playbook which will run the command to check status of the service installed in the target machine. The command will only work only if the .env file executed. The command to execute the .env file is .<space>./.env_file_name and the file contains list of environment variables like export JAVA_HOME=/optware/java/jdk/1.2.

I tried to execute the environment file before running the command with the below playbook, but it is not working.

- hosts: name
  tasks: 
    - name: `execute env file`
      command: . ./.env_file_name
      register: result

Is there any playbook to run the executable environment file to set the environments present on the target machine and then run our command??

like image 584
Newbee2 Avatar asked Feb 13 '20 13:02

Newbee2


People also ask

How do I get an environment variable in Ansible?

We can use the ansible_env and lookup function to retrieve the environment variables stored. - name: Ansible playbook to get linux environment variables. The above playbook retrieves all the environment variables inside the unix os. Now we need to retrieve the USER env variable.


1 Answers

First, the . ./.env_file_name syntax is a shell syntax and cannot work with the command module, you need to use the shell module.

Secondly, the shell environment context is reset at every task as each is an ssh command round-trip (so a new shell session), and loading the environment variables in one task will not not make them available for next tasks.

Depending on your context, you have some options:

1. Inventory environment variables

The best option is to have the environment at your inventory side in a variable with different value for each group/host through group_vars/host_vars, then to use it for the environment keyword

# host_vars/my_host.yml
---
env_vars:
  VAR1: key1
  VAR2: key2
- hosts: my_host
  tasks: 
    - name: Display environment variables
      command: env
      environment: "{{ env_vars }}"

Pros:

  • full ansible solution
  • will work for environment of every module

Cons:

  • need to know the environment variables at ansible side

2. Loading environment variables for every tasks

If your tasks are all shell/command (which I don't advise, as it's better to use appropriate ansible module whenever possible), you can simply load the env file every time with shell module

- hosts: my_host
  tasks: 
    - name: Display environment variables
      shell: |
        . ./.env_file_name && env

    - name: Do another action
      shell: |
        . ./.env_file_name && do_something_else

Pros:

  • no need to know the environment variables at ansible side

Cons:

  • limited to tasks with shell module

3. Load environment variables from env_file into ansible fact

This option is to parse the env file once and for all and load it in an ansible fact to use with the environment keyword.

- hosts: my_host
  tasks: 
    - name: Get env file content
      slurp:
        src: ./.env_file_name
      register: env_file_content

    - name: Parse environment
      set_fact:
        env_vars: "{{ ('{' + (env_file_content.content | b64decode).split('\n') | select | map('regex_replace', '([^=]*)=(.*)', '\"\\1\": \"\\2\"') | join(',') + '}') | from_json }}"

    - name: Display environment variables
      command: env
      environment: "{{ env_vars }}"

Or, if the env file need to be executed instead of directly parsed:

- hosts: my_host
  tasks: 
    - name: Get env file content
      shell: . ./.env_file_name && env
      register: env_file_result

    - name: Parse environment
      set_fact:
        env_vars: "{{ ('{' + env_file_result.stdout_lines | map('regex_replace', '([^=]*)=(.*)', '\"\\1\": \"\\2\"') | join(',') + '}') | from_json }}"

    - name: Display environment variables
      command: env
      environment: "{{ env_vars }}"

Pros:

  • will work for environment of every module
  • no need to know the environment variables at ansible side

Cons:

  • could fail on bad formatting of file
like image 103
zigarn Avatar answered Oct 16 '22 14:10

zigarn