Accessing build environment variables from Playbook using Ansible Plugin

273 views
Skip to first unread message

Ricardo Hernandez

unread,
Jul 12, 2016, 12:15:47 PM7/12/16
to Jenkins Users
Hi,

Starting from version 0.5 of the Ansible Plugin the build environment variables are available during ansible execution. 
I'm currently trying to get the job workspace path inside my playbook (which I provide using job DSL) the following way:

---
- hosts: all
  tasks
:
   
- command: echo "$PATH"
     
register: default_path
   
- command: echo "$WORKSPACE"
     
register: jenkins_job_workspace


However, WORKSPACE doesn't seem to be defined, as shown in the output:

TASK [command] *****************************************************************
changed
: [10.100.198.9] => {"changed": true, "cmd": ["echo", "$PATH"], "delta": "0:00:00.002428", "end": "2016-07-12 10:49:02.252685", "rc": 0, "start": "2016-07-12 10:49:02.250257", "stderr": "", "stdout": "/usr/local/bin:/usr/bin", "stdout_lines": ["/usr/local/bin:/usr/bin"], "warnings": []}
TASK
[command] *****************************************************************
changed
: [10.100.198.9] => {"changed": true, "cmd": ["echo", "$WORKSPACE"], "delta": "0:00:00.002479", "end": "2016-07-12 10:49:02.470880", "rc": 0, "start": "2016-07-12 10:49:02.468401", "stderr": "", "stdout": "$WORKSPACE", "stdout_lines": ["$WORKSPACE"], "warnings": []}

Is this the correct way to access them?

jcsirot

unread,
Jul 14, 2016, 5:21:19 AM7/14/16
to Jenkins Users
Hello,

Yes, the variables are available as env vars inside the process running ansible. If you want to access these env vars within an ansible playbook you should use the lookup plugin as showed here in the ansible FAQ: http://docs.ansible.com/ansible/faq.html#how-do-i-access-shell-environment-variables

You should then adapt your playbook (I tested and it works for me)

---
- hosts: all
  tasks
:

    
- command: echo "{{ lookup('env','PATH') }}"
      
register: default_path
    
- command: echo "{{ lookup('env','WORKSPACE') }}"
      
register: jenkins_job_workspace

Hope it helps.

Ricardo Hernandez

unread,
Jul 14, 2016, 12:41:14 PM7/14/16
to Jenkins Users
Thank you for pointing it out. It was really helpful.
Reply all
Reply to author
Forward
0 new messages