How to set environmental variables using Ansible

前端 未结 3 1906
广开言路
广开言路 2020-12-02 22:56

I need to set the variables like JAVA_HOME and update PATH. There are a number of ways of doing this. One way is to update the /etc/environm

相关标签:
3条回答
  • 2020-12-02 23:22

    Update to the lineinfile approach. The JAVA_HOME value should not include the bin directory. The following worked for centos:

    - name: Set JAVA_HOME
      lineinfile:
        dest: /etc/environment
        state: present
        regexp: '^JAVA_HOME'
        line: 'JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk'
    
    0 讨论(0)
  • 2020-12-02 23:33

    I found that a workaround to do this was to use the lineinfile command in Ansible:

    - name: Set JAVA_HOME
      lineinfile: dest=/etc/environment state=present regexp='^JAVA_HOME' >
         line='JAVA_HOME=/opt/jre1.8.0_51/bin'
    

    While this is not ideal, it allows you to create new environmental variables. Of course, you should use variables to construct your directory path. I have included the explicit path to simplify my example.

    0 讨论(0)
  • 2020-12-02 23:35

    Yes, there is a cleaner way. You can set environment variables per task:

      tasks:
      - shell: echo JAVA_HOME is $JAVA_HOME
        environment:
          JAVA_HOME: /usr/java/jre1.8.0_51
        register: shellout
      - debug: var=shellout
    

    Output:

    TASK: [shell echo JAVA_HOME is $JAVA_HOME] ********************************** 
    changed: [localhost]
    
    TASK: [debug var=shellout] **************************************************** 
    ok: [localhost] => {
        "var": {
            "shellout": {
                "changed": true, 
                "cmd": "echo JAVA_HOME is \"$JAVA_HOME\"", 
                "delta": "0:00:00.005797", 
                "end": "2015-08-07 06:32:47.295061", 
                "invocation": {
                    "module_args": "echo JAVA_HOME is \"$JAVA_HOME\"", 
                    "module_name": "shell"
                }, 
                "rc": 0, 
                "start": "2015-08-07 06:32:47.289264", 
                "stderr": "", 
                "stdout": "JAVA_HOME is /usr/java/jre1.8.0_51", 
                "stdout_lines": [
                    "JAVA_HOME is /usr/java/jre1.8.0_51"
                ], 
                "warnings": []
            }
        }
    }
    

    If you set the environment variable like above in a task, it is only available for this specific task. In subsequent tasks it does not exist unless you define it again.

    Though you can define env vars per play as well:

    - hosts:
      - localhost
      gather_facts: no
      environment:
        JAVA_HOME: /usr/java/jre1.8.0_51
      tasks:
         ...
    

    Now it's gonna be available for all tasks of this play.

    See Setting the Environment and FAQ: How can I set the PATH or any other environment variable for a task or entire playbook? in the docs.


    Another example with a script task:

      tasks:
      - script: /tmp/script.sh
        environment:
          JAVA_HOME: /usr/java/jre1.8.0_51
        register: shellout
      - debug: var=shellout
    

    Where the script simply has this content:

    #!/bin/sh
    
    echo JAVA_HOME is $JAVA_HOME
    
    0 讨论(0)
提交回复
热议问题