Ansible - .env 文件中的环境变量 [英] Ansible - environment variables from .env file

查看:82
本文介绍了Ansible - .env 文件中的环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置一个剧本,该剧本将运行命令来检查安装在目标机器上的服务的状态.该命令仅在 .env 文件 执行后才有效.执行 .env 文件 的命令是 ../.env_file_name 并且该文件包含环境变量列表,例如 export JAVA_HOME=/optware/java/jdk/1.2.

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??

推荐答案

首先,../.env_file_name 语法是 shell 语法,不能与 command 模块,需要使用shell 模块.

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

其次,shell 环境上下文在每个任务中都会重置,因为每个任务都是一个 ssh 命令往返(因此是一个新的 shell 会话),并且在一个任务中加载环境变量不会使它们可用于下一个任务.

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:

最好的选择是通过 group_vars/host_vars,然后将其用于 environment 关键字

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

优点:

  • 完整的 Ansible 解决方案
  • 适用于每个模块的环境

缺点:

  • 需要知道ansible端的环境变量

如果您的任务都是 shell/command(我不建议这样做,因为最好使用适当的 ansible 模块 尽可能地),你可以简单地每次使用 shell 模块加载 env 文件

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

优点:

  • 无需知道 ansible 端的环境变量

缺点:

  • 仅限于带有 shell 模块的任务
  • limited to tasks with shell module

此选项是一劳永逸地解析 env 文件并将其加载到 ansible fact 中以与 environment 关键字一起使用.

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

或者,如果需要执行 env 文件而不是直接解析:

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

优点:

  • 适用于每个模块的环境
  • 无需知道 ansible 端的环境变量

缺点:

  • 可能会因文件格式错误而失败

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

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