Ansible - Environment variables setting

心已入冬 提交于 2021-01-29 08:46:52

问题


I need to set the environment in the target machine. The environment variables are present in the file called .env337. There are several variables inside that file like

export AB_HOME=/tl/dev/abinitio/abinitio-V3  #/gcc3p32   # for 32-bit
export PATH=${AB_HOME}/bin:${PATH}

I have tried the below playbook to set the environment and register the environment variables in order to use them in the environment keyword to run the other commands in the registered environment, but it didn't worked.

- hosts: dev
  gather_facts: false
  tasks:
    - name: To set the environment
      shell: . ./.env337
      register: output

Is there any other way to resolve this.


回答1:


Q: "Set the environment and register the environment variables in order to use them in the environment keyword to run the other commands."

A: The environment variables set in the shell command can not be persistent. The shell process will be terminated after the execution of the command(s). For example

    - shell: |
        cat ./.env337
        . ./.env337
        echo "AB_HOME = $AB_HOME"
        echo "PATH = $PATH"
        exit 0
      register: result

    - debug:
        var: result.stdout_lines

    - shell: |
        echo "AB_HOME = $AB_HOME"
        echo "PATH = $PATH"
        exit 0
      register: result

    - debug:
        var: result.stdout_lines

give

    "result.stdout_lines": [
        "export AB_HOME=/tl/dev/abinitio/abinitio-V3  #/gcc3p32   # for 32-bit", 
        "export PATH=${AB_HOME}/bin:${PATH}", 
        "", 
        "AB_HOME = /tl/dev/abinitio/abinitio-V3", 
        "PATH = /tl/dev/abinitio/abinitio-V3/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/home/admin/bin"
    ]

    "result.stdout_lines": [
        "AB_HOME = ", 
        "PATH = /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/home/admin/bin"
    ]

As expected the variables are missing in the second shell task.


Q: "what if I don't know what are all the variables present inside the env file. Is there any other way to print all variables instead of using echo."

A: Short answer: Create a dictionary env_dict_add with the environment variables. Use it in the shell module to create the environment environment: "{{ env_dict_add }}".

Details

1) Create a list of unknown variables. For example

    - shell: cat ./.env337
      register: result
    - set_fact:
        env_list: "{{ env_list|default([]) +
                      [item.split('=').0.split(' ').1|trim] }}"
      loop: "{{ result.stdout_lines }}"
    - debug:
        var: env_list

gives

    "env_list": [
        "AB_HOME", 
        "PATH"
    ]

2) Create a dictionary with the environment. For example

    - shell: |
        . ./.env337
        set
      register: result
    - set_fact:
        env_dict: "{{ env_dict|default({})|
                      combine({my_key: my_value}) }}"
      vars:
        my_key: "{{ item.split('=').0 }}"
        my_value: "{{ item.split('=').1|default('') }}"
      loop: "{{ result.stdout_lines }}"

3) Use any environment variable from the dictionary. For example, print whatever variables have been exported by sourcing the file .env337

    - debug:
        msg: "var: {{ item }} value: {{ env_dict[item] }}"
      loop: "{{ env_list }}"

gives

    "msg": "var: AB_HOME value: /tl/dev/abinitio/abinitio-V3"

    "msg": "var: PATH value: /tl/dev/abinitio/abinitio-V3/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/home/admin/bin"

4) Create a dictionary with the additional environment variables only. For example

    - set_fact:
        env_dict_add: "{{ env_dict_add|default({})|
                          combine({item: env_dict[item]}) }}"
      loop: "{{ env_list }}"
    - debug:
        var: env_dict_add

gives

    "env_dict_add": {
        "AB_HOME": "/tl/dev/abinitio/abinitio-V3", 
        "PATH": "/tl/dev/abinitio/abinitio-V3/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/home/admin/bin"
    }

5) Use the dictionary env_dict_add to create environment variables in the shell command. For example

    - shell: echo ${{ item }}
      loop: "{{ env_list }}"
      register: result
      environment: "{{ env_dict_add }}"
    - debug:
        msg: "{{ dict(result.results|json_query('[].[item, stdout]')) }}"

give

    "msg": {
        "AB_HOME": "/tl/dev/abinitio/abinitio-V3", 
        "PATH": "/tl/dev/abinitio/abinitio-V3/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/home/admin/bin"
    }


来源:https://stackoverflow.com/questions/60276242/ansible-environment-variables-setting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!