vagrant.rst 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. .. _getting-started-with-vagrant:
  2. ============================
  3. Getting Started With Vagrant
  4. ============================
  5. The Vagrant driver is a new, experimental driver for spinning up a VagrantBox
  6. virtual machine, and installing Salt on it.
  7. Dependencies
  8. ============
  9. The Vagrant driver itself has no external dependencies.
  10. The machine which will host the VagrantBox must be an already existing minion
  11. of the cloud server's Salt master.
  12. It must have Vagrant_ installed, and a Vagrant-compatible virtual machine engine,
  13. such as VirtualBox_.
  14. (Note: The Vagrant driver does not depend on the salt-cloud VirtualBox driver in any way.)
  15. .. _Vagrant: https://www.vagrantup.com/
  16. .. _VirtualBox: https://www.virtualbox.org/
  17. \[Caution: The version of Vagrant packaged for ``apt install`` in Ubuntu 16.04 will not connect a bridged
  18. network adapter correctly. Use a version downloaded directly from the web site.\]
  19. Include the Vagrant guest editions plugin:
  20. ``vagrant plugin install vagrant-vbguest``.
  21. Configuration
  22. =============
  23. Configuration of the client virtual machine (using VirtualBox, VMware, etc)
  24. will be done by Vagrant as specified in the Vagrantfile on the host machine.
  25. Salt-cloud will push the commands to install and provision a salt minion on
  26. the virtual machine, so you need not (perhaps **should** not) provision salt
  27. in your Vagrantfile, in most cases.
  28. If, however, your cloud master cannot open an SSH connection to the child VM,
  29. you may **need** to let Vagrant provision the VM with Salt, and use some other
  30. method (such as passing a pillar dictionary to the VM) to pass the master's
  31. IP address to the VM. The VM can then attempt to reach the salt master in the
  32. usual way for non-cloud minions. Specify the profile configuration argument
  33. as ``deploy: False`` to prevent the cloud master from trying.
  34. .. code-block:: yaml
  35. # Note: This example is for /etc/salt/cloud.providers file or any file in
  36. # the /etc/salt/cloud.providers.d/ directory.
  37. my-vagrant-config:
  38. minion:
  39. master: 111.222.333.444
  40. provider: vagrant
  41. Because the Vagrant driver needs a place to store the mapping between the
  42. node name you use for Salt commands and the Vagrantfile which controls the VM,
  43. you must configure your salt minion as a Salt smb server.
  44. (See `host provisioning example`_ below.)
  45. Profiles
  46. ========
  47. Vagrant requires a profile to be configured for each machine that needs Salt
  48. installed. The initial profile can be set up at ``/etc/salt/cloud.profiles``
  49. or in the ``/etc/salt/cloud.profiles.d/`` directory.
  50. Each profile requires a ``vagrantfile`` parameter. If the Vagrantfile has
  51. definitions for `multiple machines`_ then you need a ``machine`` parameter,
  52. .. _`multiple machines`: https://www.vagrantup.com/docs/multi-machine/
  53. Salt-cloud uses SSH to provision the minion. There must be a routable path
  54. from the cloud master to the VM. Usually, you will want to use
  55. a bridged network adapter for SSH. The address may not be known until
  56. DHCP assigns it. If ``ssh_host`` is not defined, and ``target_network``
  57. is defined, the driver will attempt to read the address from the output
  58. of an ``ifconfig`` command. Lacking either setting,
  59. the driver will try to use the value Vagrant returns as its ``ssh_host``,
  60. which will work only if the cloud master is running somewhere on the same host.
  61. The ``target_network`` setting should be used
  62. to identify the IP network your bridged adapter is expected to appear on.
  63. Use CIDR notation, like ``target_network: '2001:DB8::/32'``
  64. or ``target_network: '192.0.2.0/24'``.
  65. Profile configuration example:
  66. .. code-block:: yaml
  67. # /etc/salt/cloud.profiles.d/vagrant.conf
  68. vagrant-machine:
  69. host: my-vhost # the Salt id of the virtual machine's host computer.
  70. provider: my-vagrant-config
  71. cwd: /srv/machines # the path to your Vagrantfile.
  72. vagrant_runas: my-username # the username who defined the Vagrantbox on the host
  73. # vagrant_up_timeout: 300 # (seconds) timeout for cmd.run of the "vagrant up" command
  74. # vagrant_provider: '' # option for "vagrant up" like: "--provider vmware_fusion"
  75. # ssh_host: None # "None" means try to find the routable IP address from "ifconfig"
  76. # ssh_username: '' # also required when ssh_host is used.
  77. # target_network: None # Expected CIDR address range of your bridged network
  78. # force_minion_config: false # Set "true" to re-purpose an existing VM
  79. The machine can now be created and configured with the following command:
  80. .. code-block:: bash
  81. salt-cloud -p vagrant-machine my-id
  82. This will create the machine specified by the cloud profile
  83. ``vagrant-machine``, and will give the machine the minion id of
  84. ``my-id``. If the cloud master is also the salt-master, its Salt
  85. key will automatically be accepted on the master.
  86. Once a salt-minion has been successfully installed on the instance, connectivity
  87. to it can be verified with Salt:
  88. .. code-block:: bash
  89. salt my-id test.version
  90. .. _host provisioning example:
  91. Provisioning a Vagrant cloud host (example)
  92. ===========================================
  93. In order to query or control minions it created, each host
  94. minion needs to track the Salt node names associated with
  95. any guest virtual machines on it.
  96. It does that using a Salt sdb database.
  97. The Salt sdb is not configured by default. The following example shows a
  98. simple installation.
  99. This example assumes:
  100. - you are on a large network using the 10.x.x.x IP address space
  101. - your Salt master's Salt id is "bevymaster"
  102. - it will also be your salt-cloud controller
  103. - it is at hardware address 10.124.30.7
  104. - it is running a recent Debian family Linux (raspbian)
  105. - your workstation is a Salt minion of bevymaster
  106. - your workstation's minion id is "my_laptop"
  107. - VirtualBox has been installed on "my_laptop" (apt install is okay)
  108. - Vagrant was installed from vagrantup.com. (not the 16.04 Ubuntu apt)
  109. - "my_laptop" has done "vagrant plugin install vagrant-vbguest"
  110. - the VM you want to start is on "my_laptop" at "/home/my_username/Vagrantfile"
  111. .. code-block:: yaml
  112. # file /etc/salt/minion.d/vagrant_sdb.conf on host computer "my_laptop"
  113. # -- this sdb database is required by the Vagrant module --
  114. vagrant_sdb_data: # The sdb database must have this name.
  115. driver: sqlite3 # Let's use SQLite to store the data ...
  116. database: /var/cache/salt/vagrant.sqlite # ... in this file ...
  117. table: sdb # ... using this table name.
  118. create_table: True # if not present
  119. Remember to re-start your minion after changing its configuration files...
  120. ``sudo systemctl restart salt-minion``
  121. .. code-block:: ruby
  122. # -*- mode: ruby -*-
  123. # file /home/my_username/Vagrantfile on host computer "my_laptop"
  124. BEVY = "bevy1"
  125. DOMAIN = BEVY + ".test" # .test is an ICANN reserved non-public TLD
  126. # must supply a list of names to avoid Vagrant asking for interactive input
  127. def get_good_ifc() # try to find a working Ubuntu network adapter name
  128. addr_infos = Socket.getifaddrs
  129. addr_infos.each do |info|
  130. a = info.addr
  131. if a and a.ip? and not a.ip_address.start_with?("127.")
  132. return info.name
  133. end
  134. end
  135. return "eth0" # fall back to an old reliable name
  136. end
  137. Vagrant.configure(2) do |config|
  138. config.ssh.forward_agent = true # so you can use git ssh://...
  139. # add a bridged network interface. (try to detect name, then guess MacOS names, too)
  140. interface_guesses = [get_good_ifc(), 'en0: Ethernet', 'en1: Wi-Fi (AirPort)']
  141. config.vm.network "public_network", bridge: interface_guesses
  142. if ARGV[0] == "up"
  143. puts "Trying bridge network using interfaces: #{interface_guesses}"
  144. end
  145. config.vm.provision "shell", inline: "ip address", run: "always" # make user feel good
  146. # . . . . . . . . . . . . Define machine QUAIL1 . . . . . . . . . . . . . .
  147. config.vm.define "quail1", primary: true do |quail_config|
  148. quail_config.vm.box = "boxesio/xenial64-standard" # a public VMware & Virtualbox box
  149. quail_config.vm.hostname = "quail1." + DOMAIN # supply a name in our bevy
  150. quail_config.vm.provider "virtualbox" do |v|
  151. v.memory = 1024 # limit memory for the virtual box
  152. v.cpus = 1
  153. v.linked_clone = true # make a soft copy of the base Vagrant box
  154. v.customize ["modifyvm", :id, "--natnet1", "192.168.128.0/24"] # do not use 10.x network for NAT
  155. end
  156. end
  157. end
  158. .. code-block:: yaml
  159. # file /etc/salt/cloud.profiles.d/my_vagrant_profiles.conf on bevymaster
  160. q1:
  161. host: my_laptop # the Salt id of your virtual machine host
  162. machine: quail1 # a machine name in the Vagrantfile (if not primary)
  163. vagrant_runas: my_username # owner of Vagrant box files on "my_laptop"
  164. cwd: '/home/my_username' # the path (on "my_laptop") of the Vagrantfile
  165. provider: my_vagrant_provider # name of entry in provider.conf file
  166. target_network: '10.0.0.0/8' # VM external address will be somewhere here
  167. .. code-block:: yaml
  168. # file /etc/salt/cloud.providers.d/vagrant_provider.conf on bevymaster
  169. my_vagrant_provider:
  170. driver: vagrant
  171. minion:
  172. master: 10.124.30.7 # the hard address of the master
  173. Create and use your new Salt minion
  174. -----------------------------------
  175. - Typing on the Salt master computer ``bevymaster``, tell it to create a new minion named ``v1`` using profile ``q1``...
  176. .. code-block:: bash
  177. sudo salt-cloud -p q1 v1
  178. sudo salt v1 network.ip_addrs
  179. [ you get a list of IP addresses, including the bridged one ]
  180. - logged in to your laptop (or some other computer known to GitHub)...
  181. \[NOTE:\] if you are using MacOS, you need to type ``ssh-add -K`` after each boot,
  182. unless you use one of the methods in `this gist`_.
  183. .. _this gist: https://github.com/jirsbek/SSH-keys-in-macOS-Sierra-keychain
  184. .. code-block:: console
  185. ssh -A vagrant@< the bridged network address >
  186. # [ or, if you are at /home/my_username/ on my_laptop ]
  187. vagrant ssh quail1
  188. - then typing on your new node "v1" (a.k.a. quail1.bevy1.test)...
  189. .. code-block:: console
  190. password: vagrant
  191. # [ stuff types out ... ]
  192. ls -al /vagrant
  193. # [ should be shared /home/my_username from my_laptop ]
  194. # you can access other network facilities using the ssh authorization
  195. # as recorded in your ~.ssh/ directory on my_laptop ...
  196. sudo apt update
  197. sudo apt install git
  198. git clone ssh://git@github.com/yourID/your_project
  199. # etc...