DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Deploy a Session Recording Solution Using Ansible and Audit Your Bastion Host
  • Running Ansible From Windows Using Virtualization
  • Automation, Ansible, AI
  • Mastering Go-Templates in Ansible With Jinja2

Trending

  • Effective Tips for Debugging Complex Code in Java
  • 5 Web3 Trends to Follow in 2023
  • Analyzing Stock Tick Data in SingleStoreDB Using LangChain and OpenAI's Whisper
  • Five Free AI Tools for Programmers to 10X Their Productivity
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Environment Variables with Vagrant and Ansible

Environment Variables with Vagrant and Ansible

How to configure Ansible and Vagrant to use a web proxy server.

Alan Hohn user avatar by
Alan Hohn
·
Nov. 17, 15 · Tutorial
Like (4)
Save
Tweet
Share
10.56K Views

Join the DZone community and get the full member experience.

Join For Free

One of the joys of working in a big corporate environment is the use of a Web proxy server for connection to the Internet. When provisioning a virtual machine with Vagrant and Ansible, this means that sometimes the VM has to go through the proxy and sometimes it doesn’t. I’d rather this was as seamless a transition as possible. The solution I came up with can be extended to other cases where different Ansible behavior is needed at different times.

The first step is to configure Ansible to use the environment variable. This is done using the environment attribute on a task in a playbook. For example, a task to update an Ubuntu box looks like this:

- name: update
    apt: upgrade=yes
    environment: proxy_env

The proxy_env variable could be declared in a vars section within the Ansible playbook. But it’s more flexible to pass it through from the Vagrantfile. This means a provisioning section like this:

config.vm.provision "ansible" do |ansible|
    ansible.playbook = "playbook.yml"
    ansible.extra_vars = {
      proxy_env: {
        http_proxy: proxy
      }
    }
  end

This in turn sets the http_proxy to a proxy variable in the Vagrantfile. Theproxy variable is set elsewhere in the Vagrantfile, like this:

proxy = ENV['http_proxy'] || ""

So if the http_proxy environment variable is set on the host, it will be set in the Vagrantfile, which will cause it to be passed to Ansible, which will cause Ansible to set it in the VM before it runs the apt-get update task.

Ansible (software)

Opinions expressed by DZone contributors are their own.

Related

  • Deploy a Session Recording Solution Using Ansible and Audit Your Bastion Host
  • Running Ansible From Windows Using Virtualization
  • Automation, Ansible, AI
  • Mastering Go-Templates in Ansible With Jinja2

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: