Skip to content

How To Pass Environment Variables to Ansible Navigator

Avatar photo

https://www.linkedin.com/in/gineesh/ https://twitter.com/GiniGangadharan

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.

Disclaimer:

The views expressed and the content shared in all published articles on this website are solely those of the respective authors, and they do not necessarily reflect the views of the author’s employer or the techbeatly platform. We strive to ensure the accuracy and validity of the content published on our website. However, we cannot guarantee the absolute correctness or completeness of the information provided. It is the responsibility of the readers and users of this website to verify the accuracy and appropriateness of any information or opinions expressed within the articles. If you come across any content that you believe to be incorrect or invalid, please contact us immediately so that we can address the issue promptly.

Avatar photo


https://www.linkedin.com/in/gineesh/ https://twitter.com/GiniGangadharan
Gineesh Madapparambath is the founder of techbeatly and he is the co-author of The Kubernetes Bible, Second Edition. and the author of ๐—”๐—ป๐˜€๐—ถ๐—ฏ๐—น๐—ฒ ๐—ณ๐—ผ๐—ฟ ๐—ฅ๐—ฒ๐—ฎ๐—น-๐—Ÿ๐—ถ๐—ณ๐—ฒ ๐—”๐˜‚๐˜๐—ผ๐—บ๐—ฎ๐˜๐—ถ๐—ผ๐—ป. 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). (aka Gini Gangadharan - iamgini.com)

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.