简体   繁体   English

如何在ansible playbook中设置环境变量

[英]How to set environment variables in ansible playbook

I am trying to set environment variables through ansible playbook to install gnucobol with vbisam. 我试图通过ansible playbook设置环境变量来安装带有vbisam的gnucobol。 But that variables are not getting set while executing playbook. 但是在执行playbook时没有设置变量。

 name: Setting variables for CPPFLAGS
 shell: "echo $CPPFLAGS"
 environment:
  CPPFLAGS: -I/opt/vbisam-2.0/include

 name: Setting variables for LDFLAGS
 shell: "echo $LDFLAGS"
 environment:
  LDFLAGS: -L/opt/vbisam-2.0/lib

 name: Setting variables for LD_LIBRARY_PATH
 shell: "echo $LD_LIBRARY_PATH"
 environment:
  LD_LIBRARY_PATH: /opt/vbisam-2.0/lib:${LD_LIBRARY_PATH}

Can some one help me to fix the issue. 有人可以帮我解决这个问题。

Your environment variables are definitely getting set. 您的环境变量肯定会被设置。 Your existing tasks don't contain any attempt to verify this, so let's add one. 您现有的任务不包含任何验证此操作的尝试,所以让我们添加一个。 For example, if we run this playbook: 例如,如果我们运行此剧本:

- hosts: localhost
  tasks:
    -  name: Setting variables for CPPFLAGS
       shell: "echo $CPPFLAGS"
       environment:
         CPPFLAGS: -I/opt/vbisam-2.0/include
       register: cppflags

    - debug:
        var: cppflags.stdout

We see as output: 我们看作输出:

PLAY [localhost] *******************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************
ok: [localhost]

TASK [Setting variables for CPPFLAGS] **********************************************************************************************************
changed: [localhost]

TASK [debug] ***********************************************************************************************************************************
ok: [localhost] => {
    "cppflags.stdout": "-I/opt/vbisam-2.0/include"
}

PLAY RECAP *************************************************************************************************************************************
localhost                  : ok=3    changed=1    unreachable=0    failed=0   

As @techraf hinted in a comment, it's important to understand that setting environment variables using the environment on a task sets them only for that task. 正如@techraf在评论中暗示的那样,重要的是要理解在任务上使用environment设置环境变量仅为该任务设置环境变量。 That is, if you wanted CPPFLAGS , LDFLAGS , and LD_LIBRARY_PATH all set at the same time, you would need to do something like: 也就是说,如果你想同时设置CPPFLAGSLDFLAGSLD_LIBRARY_PATH ,你需要做类似的事情:

    -  name: Setting variables for CPPFLAGS
       shell: "echo $CPPFLAGS"
       environment:
         CPPFLAGS: -I/opt/vbisam-2.0/include
         LDFLAGS: -L/opt/vbisam-2.0/lib
         LD_LIBRARY_PATH: /opt/vbisam-2.0/include
       register: cppflags

If you need those variables set on multiple tasks, you would need to either apply the same environment keyword to each task, or set environment on the play instead of individual tasks. 如果需要在多个任务上设置这些变量,则需要对每个任务应用相同的environment关键字,或者在播放时设置environment而不是单个任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM