Difference between revisions of "Environment Variables with Ansible"

From Knowledge Center
Jump to: navigation, search
(Created page with "==Environment Variables Across Tasks== You can set environment variables across tasks, and let your tasks inherit the variables: <pre> - hosts: localhost vars: var_mys...")
 
(No difference)

Latest revision as of 02:43, 20 March 2019

Environment Variables Across Tasks

You can set environment variables across tasks, and let your tasks inherit the variables:

- hosts: localhost
  vars:
    var_mysecret: secret123

  tasks:
    - name: echo my env var
      environment:
        MYNAME: "{{var_mysecret}}"
      shell: "echo hello $MYNAME > /tmp/bla.txt"
      args:
        creates: /tmp/bla.txt

Run the task:

$ ansible-playbook -i inventory.ini -u ruan task.yml

Check the output:

$ cat /tmp/bla.txt
hello secret123

Environment Variables Per Task

You can set environment variables per task:

- hosts: dev
  tasks:
    - name: echo my env var
      environment:
        MYNAME: "RUAN"
      shell: "echo hello $MYNAME > /tmp/bla2.txt"
      args:
        creates: /tmp/bla2.txt

Run the task:

$ ansible-playbook -i inventory.ini -u ruan task.yml

Check the output:

$ cat /tmp/bla2.txt
hello RUAN