Cyber Deals - Get up to 65% off on CKA, CKAD, CKS, KCNA, KCSA exams and courses!

How To Pass Environment Variables to Ansible Navigator

How To Pass Environment Variables to Ansible Navigator

ansible-navigator is the new command line utility (CLI) introduced in Ansible Automation Platform 2, for running and developing Ansible automation content. ansible-navigator can be used to execute a playbook as follows.

With ansible-navigator it is possible to test the Ansible playbooks inside the execution environment containers which will help to get the same result when you execute on Ansible Controller or Ansible Automation Platform. But when you have a dependency on credentials, then it is not a direct method to simulate the containerized playbook execution.

For example, I have a playbook to automate some of the VMWare operations and in the Ansible controller, I have created a VMWare vCenter credential and configured it in the job template. But when I want to test the same playbook using ansible-navigator , I need to find a way to pass the VMWare credential to the playbook. I can achieve this using temporary variables (with Ansible Vault of course) for the credential but again I need to change the same before pushing to the Ansible controller as I have credentail already configured there.

In my case, I know the VMWare vCenter credential is exposing the following variables for the playbook to pick up.

  • VMWARE_HOST
  • VMWARE_USER
  • VMWARE_PASSWORD

So, when I run the playbook using ansible-navigator , I just need to ensure that, the environment variables are passed to the execution environment; let’s see how to do that.

Export the environment variables

First, I have exported the environment variables as follows.

$  export VMWARE_HOST=vcenter.example.com
$  export VMWARE_USER=ansible-service-user
$  export VMWARE_PASSWORD=mycomplexpassword

Remember, this is in our local workstation where you run ansible-navigator and not inside the execution environment container yet.

Pass environment variable to the execution environment

While executing the playbook with ansible-navigator , use --penv to pass the environment variable as follows.

$  ansible-navigator run vmware-vm-provisioning.yaml -m stdout  --penv VMWARE_HOST --penv VMWARE_USER --penv VMWARE_PASSWORD

That’s it, the playbook can now pickup the environment variables inside the container and use it during execution.

Also check the Ansible Navigator cheatsheet .

Gineesh Madapparambath

Gineesh Madapparambath

Gineesh Madapparambath is the founder of techbeatly. He is the co-author of The Kubernetes Bible, Second Edition and the author of Ansible for Real Life Automation. He has worked as a Systems Engineer, Automation Specialist, and content author. His primary focus is on Ansible Automation, Containerisation (OpenShift & Kubernetes), and Infrastructure as Code (Terraform). (Read more: iamgini.com)


Disclaimer: Views expressed in articles are those of the respective authors and do not necessarily reflect the views of their employers or this site. While we aim for accuracy, please verify information before relying on it, and let us know if you spot an error.
Share:

Related Posts

Build Your Own Git Server using Gogs

Build Your Own Git Server using Gogs

When it comes to VCS (Version Control System), we will have confusion as we have many products in the market with almost the same capabilities....

Introducing the Event-Driven Ansible & Demo

Introducing the Event-Driven Ansible & Demo

In AnsibleFest 2022, Red Hat announced an exciting new developer preview for Event-Driven Ansible. Event-Driven Ansible is a new way to enhance and …

Use Terraform to Create a FREE Ansible Lab in AWS

Use Terraform to Create a FREE Ansible Lab in AWS

Introduction When it comes to create an Ansible Lab, you can easily spin-up one using Terraform on AWS, GCP or Azure. Even though we are...

Dynamic Scheduler in Ansible Automation Platform

Dynamic Scheduler in Ansible Automation Platform

This blog starts with one patching automation workshop. The customer’s procedure is that the end user raises a patching request. The request includes …

Configure Your Windows Host to be Managed by Ansible

Configure Your Windows Host to be Managed by Ansible

Ansible is only available for Linux (or Unix); yes that is true (Ansible is not natively available for Windows yet) but you know, you can use Ansible …

Ansible

Ansible Collections [LIVE]

Ansible Collection is a great way of getting content contributions from various Ansible Developers. We will learn what is Ansible Collection and how …