Output Variables for Ansible
Configure output variables to export data from Ansible playbooks to other pipeline steps.
Output variables allow you to export data from Ansible playbook execution to other steps in the stage or anywhere in the pipeline. This page explains how to configure output variables for Ansible playbooks in Harness.
Export variables from an Ansible playbook
To export variables from an Ansible playbook, write key-value pairs to the DRONE_OUTPUT file using the ansible.builtin.lineinfile module.
Example playbook
---
- name: Fetch system info from remote host and export to Harness
hosts: all
gather_facts: true
tasks:
- name: Show remote OS info
ansible.builtin.debug:
msg: "Remote host {{ inventory_hostname }} is running {{ ansible_distribution }} {{ ansible_distribution_version }}"
- name: Export OS to Harness output variable
ansible.builtin.lineinfile:
path: "{{ lookup('ansible.builtin.env', 'DRONE_OUTPUT') }}"
line: "REMOTE_OS={{ ansible_distribution }} {{ ansible_distribution_version }}"
create: true
insertafter: EOF
delegate_to: localhost
run_once: trueKey points
DRONE_OUTPUT path: Use
lookup('ansible.builtin.env', 'DRONE_OUTPUT')to get the output file path.Format: Write variables as
VARIABLE_NAME=value.delegate_to: Use
delegate_to: localhostto write to the file on the control node, not remote hosts.run_once: Use
run_once: truewhen exporting from multiple hosts to avoid duplicate writes.
Export multiple variables
You can export multiple variables by writing additional lines to DRONE_OUTPUT:
Each variable will be available as a separate output variable in Harness.
Reference output variables
Once exported, output variables can be referenced in subsequent pipeline steps using Harness expressions.
Within the same stage
Anywhere in the pipeline
When step is inside a step group
To find the expression to reference your output variables, find the step in the pipeline execution, and click the Output tab.
Example usage
After running the playbook above, you can reference the REMOTE_OS variable in a subsequent step:
Related concepts
Go to Managing large outputs to understand output truncation and file export.
Last updated
Was this helpful?