Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set environment variables in ansible playbook

Tags:

ansible

I am trying to set environment variables through ansible playbook to install gnucobol with vbisam. But that variables are not getting set while executing 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.

like image 488
anji Avatar asked Oct 09 '17 10:10

anji


People also ask

How do I re-use environment settings in Ansible?

You can re-use environment settings by defining them as variables in your play and accessing them in a task as you would access any stored Ansible variable: You can store environment settings for re-use in multiple playbooks by defining them in a group_vars file: You can set the remote environment at the play level:

How do I create an Ansible playbook for gathering facts?

You must include an explicit gather_facts task in your playbook and set the environment keyword on that task to turn these values into Ansible facts. You can set the environment directly at the task level:

How do I use variables in Ansible?

Using Variables. Ansible uses variables to manage differences between systems. With Ansible, you can execute tasks and playbooks on multiple different systems with a single command. To represent the variations among those different systems, you can create variables with standard YAML syntax, including lists and dictionaries.

Where can I find the ansible facts?

Facts are information derived from speaking with your remote systems. You can find a complete set under the ansible_facts variable, most facts are also ‘injected’ as top level variables preserving the ansible_ prefix, but some are dropped due to conflicts. This can be disabled via the INJECT_FACTS_AS_VARS setting.


1 Answers

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. That is, if you wanted CPPFLAGS, LDFLAGS, and LD_LIBRARY_PATH all set at the same time, you would need to do something like:

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

like image 126
larsks Avatar answered Oct 06 '22 15:10

larsks