Skip to main content

Creating an Ansible Controller

Basic requirements, test with any mixture of the following basic Ansible commands.

ansible <host> -m ping
ansible <hostgroup> -m ping
ansible <hostgroup> -m ping -u <user>
ansible <host> -m ping --private-key=/home/user/.ssh/key
ansible <host> -m ping -u <user> --private-key=/home/user/.ssh/key

 

2 hosts

  1. controller
    1. has ansible
    2. create ssh key as the ansible user
      1. ssh-copy-id <worker>
      2. should be able to ssh with no password - ssh <workstation> as ansible user
        1. If the above does not work, create /home/USER/.ssh/config and add IdentityFile /path/to/Private.key, this will pass the key automatically when connecting as USER.
        2. Ensure the host you are connecting to has the connecting key within the ~/.ssh/authorized_keys file.
        3. restart sshd.service - sudo systemctl restart sshd.service
  2. worker
    1. has ansible
    2. has a known password, but can sudo without one.
      1. <user> ALL=(ALL) ALL NOPASSWD:ALL within sudoers
Using Ansible Locally - Vagrant / vBox

Run the following - 

ansible@controller:~/vBox$$ sudo apt -y install vagrant
ansible@controller:~/vBox$$ sudo apt -y install virtualbox

# Create a directory to store our server configurations
ansible@controller:~/vBox$$ mkdir /some/path/vBox
ansible@controller:~/vBox$$ cd /some/path/vBox

# Ensure you are in the directory you created, and continue
ansible@controller:~/vBox$ pwd

# Download the vagrant box (Ubuntu 18.04 is the below box)
ansible@controller:~/vBox$ vagrant box add ubuntu/bionic64
==> box: Loading metadata for box 'ubuntu/bionic64'
    box: URL: https://vagrantcloud.com/ubuntu/bionic64
==> box: Adding box 'ubuntu/bionic64' (v20190813.1.0) for provider: virtualbox
    box: Downloading: https://vagrantcloud.com/ubuntu/boxes/bionic64/versions/20190813.1.0/providers/virtualbox.box
==> box: Successfully added box 'ubuntu/bionic64' (v20190813.1.0) for 'virtualbox'!

# Initialize our server configuration using vagrant init..
ansible@controller:~/vBox$ vagrant init ubuntu/bionic64
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

# Run a quick check to ensure our new vagrant box is up-to-date..
vagrant box update

# Spin up our new box
ansible@controller:~/vBox$ vagrant up

Now, we need to tell vBox where to save our virtual machines, to avoid the default directory, which includes capitalization and spaces, making it difficult to navigate to within a Bash terminal - 

vboxmanage setproperty machinefolder /path/to/directory/

Now, if we already spun up a box using the default directory, we should navigate to the directory containing the Vagrantfile for that box, and run the following..

vagrant destroy
vagrant up

This will destroy all traces of our previous box, and spin up a new one using our new default directory. We should note that it is still necessary to remove the default directory created if we had previously spun up a box using the default directory. Vagrant will not remove these files, since they are associated with the virtualbox machine and not the Vagrant configuration itself. Simply run sudo rm -r /path/to/VirtualBox\ VMs/ and refer to your newly configured directory for all boxes spun up from here forward.