Vagrant provision inline: A Step-by-Step Guide for Developers

Introduction

In this tutorial, How to use vagrant provision inline. Use inline to execute on the remote machine. Explore the essentials of inline provisioning with Vagrant in our latest tutorial. This guide provides a practical walkthrough on setting up and configuring virtual environments directly through Vagrant’s inline commands. Perfect for developers and IT professionals, this tutorial simplifies the process of using inline scripts to manage complex configurations, ensuring a seamless and efficient setup for your development projects.

What is Vagrant Inline Provisioning?

Vagrant Inline Provisioning is a method used in Vagrant for automatically configuring and setting up virtual machines. Here’s a simple explanation in English, presented as a bulleted list:

  • Purpose: Automatically configures and sets up virtual machines.
  • Method: Uses simple scripts written directly in the Vagrantfile.
  • Functionality: Allows users to execute shell commands during the virtual machine setup process.
  • Benefits: Streamlines and automates the configuration of development environments efficiently.

Vagrant provision inline example as below


Vagrant.configure("2") do |config|

  config.vm.define :ansible_controller do |ansible_controller|
    ansible_controller.vm.hostname = "ansible"
    ansible_controller.vm.box = "centos/6.5"
    ENV["LC_ALL"] = "en_US.UTF-8"
    config.ssh.insert_key = false
    ansible_controller.vm.network "private_network", ip: "192.168.3.10", :netmask => "255.255.255.0"
    ansible_controller.vm.provision "shell", inline: <<-SHELL
      echo "hello"
      echo "192.168.3.11 centos8" >> /etc/hosts
      groupadd -g 20000 gadmin
      useradd -d /home/huupv -u 9999 -m huupv
      echo -e "huupv\nhuupv\n" |  passwd huupv
      echo "huupv ALL = (root) NOPASSWD:ALL" | tee /etc/sudoers.d/huupv
      chmod 0440 /etc/sudoers.d/huupv
      su - huupv -c "echo |ssh-keygen -t rsa"
      SHELL
  end

  config.vm.define :centos do |centos|
    centos.vm.hostname = "centos8"
    centos.vm.box = "centos/8"
    ENV["LC_ALL"] = "en_US.UTF-8"
    config.ssh.insert_key = false
    centos.vm.network "private_network", ip: "192.168.3.11", :netmask => "255.255.255.0"
    centos.vm.provision "shell", inline: <<-SHELL
      echo "hello"
      groupadd -g 20000 gadmin
      useradd -d /home/huupv -u 9999 -m huupv
      echo -e "huupv\nhuupv\n" |  passwd huupv
      echo "huupv ALL = (root) NOPASSWD:ALL" | tee /etc/sudoers.d/huupv
      chmod 0440 /etc/sudoers.d/huupv
      useradd -m -d /home/user001 -s  /bin/bash   -g gadmin -u 8889  user001; echo -e "user001\nuser001\n"  |  passwd  user001
	  su - huupv -c "mkdir /home/huupv/.ssh"
      su - huupv -c "touch /home/huupv/.ssh/authorized_keys"
      su - huupv -c "chmod 600 /home/huupv/.ssh/authorized_keys"
      su - huupv -c "chmod 700 /home/huupv/.ssh"
    SHELL
  end
  end
Vagrant provision inline A Step-by-Step Guide for Developers

Conclusion

You have to use a vagrant inline to execute on the remote machine. Mastering Vagrant’s inline provisioning can significantly streamline your development workflow. We’ve covered the key steps to execute scripts within your Vagrantfile, enhancing your capability to manage and automate your virtual environments. Keep experimenting with different configurations and scripts to fully leverage the power of Vagrant in your projects. I hope will this your helpful. Thank you for reading the DevopsRoles page!

, ,

About HuuPV

My name is Huu. I love technology, especially Devops Skill such as Docker, vagrant, git, and so forth. I like open-sources, so I created DevopsRoles.com to share the knowledge I have acquired. My Job: IT system administrator. Hobbies: summoners war game, gossip.
View all posts by HuuPV →

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.