Ansible - 环境变量设置 [英] Ansible - Environment variables setting

查看:50
本文介绍了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}

我尝试了下面的剧本来设置环境并注册环境变量,以便在 environment 关键字中使用它们在注册的环境中运行其他命令,但没有奏效.

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.

推荐答案

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

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.

问:如果我不知道 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"
    }

这篇关于Ansible - 环境变量设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆