Ansible-环境变量设置

Ansible-环境变量设置

问题描述:

我需要在目标计算机上设置环境.环境变量存在于名为.env337的文件中.该文件中有几个变量,例如

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

还有其他解决方法吗?

Q:设置环境并注册环境变量,以便在环境关键字中使用它们来运行其他命令."

A:在 shell 命令中设置的环境变量不能持久化.执行命令后,shell进程将终止.例如

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

给予

    "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"
    ]

按预期,变量在第二个 shell 任务中丢失.

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

Q:如果我不知道env文件中是否存在所有变量,该怎么办.还有其他方法可以打印所有变量而不使用echo."

A:简短答案:使用环境变量创建字典 env_dict_add .在 shell 模块中使用它来创建环境 environment:"{{env_dict_add}}" .

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 }}".

详细信息

1)创建未知变量列表.例如

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

给予

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

2)使用环境创建字典.例如

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)使用字典中的任何环境变量.例如,通过获取文件 .env337

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 }}"

给予

    "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)创建仅包含附加环境变量的字典.例如

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

给予

    "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)使用字典 env_dict_add 在shell命令中创建环境变量.例如

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]')) }}"

给予

    "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"
    }