Joshua Cruz

Setup & Provision Fedora Server using Ansible

February 23, 2020

Introduction

This tutorial walks you through how to setup & provision a Fedora Server using Vagrant and Ansible. Ansible will be used to automatically install and configure a Vagrant box (virtual machine) for us.

Prerequisites

EBS local helloworld

Writing our Vagrantfile

1
2
3
4
5
6
7
8
9
10
11
# Vagrantfile
Vagrant.configure("2") do |config|
    config.vm.box = "roboxes/fedora28"

    config.vm.define 'fedora' do |node|
        node.vm.hostname = 'fedora.local'
        node.vm.provision 'ansible' do |ansible|
            ansible.playbook = 'main.yml'
        end
    end
end

Writing our Playbook

Playbooks is what Ansible uses for configuration management.

The yaml code below is a simple playbook that installs a Fedora Server, then installs a python package in order to disable SELinux.

1
2
3
4
5
6
7
8
9
10
11
12
# main.yml
---
- hosts: Fedora # name of the box - aka a *play* (multiple hosts refers to *plays*)
  become: true # everything will become sudo
  tasks: # represents things - a calls to an ansible module
  - name: Install libselinux-python # name of first task
    dnf: # fedora uses dnf package manager to install modules 
      name: "libselinux-python" # name of package we are installing
      state: present # ensures the desired package is installed
  - name: Disable selinux # name of second task
    selinux: # defining it
      state: disabled # disabling it

This .yml is specficially runs the Fedora Server in headless mode, which is handy for testing purposes.

Final output

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
➜  vagrant-ansible vagrant up
==> vagrant: A new version of Vagrant is available: 2.2.7 (installed version: 2.2.6)!
==> vagrant: To upgrade visit: https://www.vagrantup.com/downloads.html

Bringing machine 'fedora' up with 'virtualbox' provider...
==> fedora: Box 'roboxes/fedora28' could not be found. Attempting to find and install
    fedora: Box Provider: virtualbox
    fedora: Box Version: >= 0
==> fedora: Loading metadata for box 'roboxes/fedora28'
    fedora: URL: https://vagrantcloud.com/roboxes/fedora28
==> fedora: Adding box 'roboxes/fedora28' (v2.0.6) for provider: virtualbox
    fedora: Downloading: https://vagrantcloud.com/roboxes/boxes/fedora28/versions
    /2.0.6/providers/virtualbox.box
    fedora: Download redirected to host:
    vagrantcloud-files-production.s3.amazonaws.com
==> fedora: Successfully added box 'roboxes/fedora28' (v2.0.6) for 'virtualbox'!
==> fedora: Importing base box 'roboxes/fedora28'...
==> fedora: Matching MAC address for NAT networking...
==> fedora: Checking if box 'roboxes/fedora28' version '2.0.6' is up to date...
==> fedora: Setting the name of the VM: vagrant-ansible_fedora_1582472543600_21194
==> fedora: Pruning invalid NFS exports. Administrator privileges will be required...
...
...
...
    fedora: Running ansible-playbook...

PLAY [fedora] ******************************************************************

TASK [Gathering Facts] *********************************************************
ok: [fedora]

TASK [Install libselinux-python] ***********************************************
changed: [fedora]

TASK [Disable selinux] *********************************************************
[WARNING]: SELinux state temporarily changed from 'enforcing' to 'permissive'.
State change will take effect next reboot.

changed: [fedora]

PLAY RECAP *********************************************************************
fedora    : ok=3  changed=2  unreachable=0  failed=0  skipped=0  rescued=0  ignored=0

So when we open the newly created Fedora Vagrant box we can see it’s ran without a GUI interface.

Configuring Fedora to become a workstation

Now that our headless Fedora Server is working correctly, we can rewrite our .yml file to enable a graphical interface, as well as install a few packages.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# main.yml (revised)
---
- hosts: fedora
  become: true
  tasks:
  - name: Set default target to graphical
    shell: systemctl set-default graphical # enable graphical interface
  - name: Install lxde & sundry development tools # name of new task
    dnf:
      name: "" # for loop to install items below
      state: present
    with_items: # list of items dnf will install
      - '@Lxde desktop'
      - awscli
      - jq
      - docker
      - npm

After using vagrant provision, we can reboot our vm, and be greeted with a graphical interface.

Graphical Fedora

Enabling the Docker daemon

Next, we’re going to add additional plays to our Playbook but installing the Docker daemon onto our Fedora server.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# main.yml (revised)
---
- hosts: fedora
  become: true
  tasks:
  - name: Set default target to graphical
    shell: systemctl set-default graphical
  - name: Install lxde & sundry development tools
    dnf:
      name: ""
      state: present
    with_items:
      - '@Lxde desktop'
      - awscli
      - jq
      - docker
      - npm
  - name: Enable the Docker daemon
    systemd:
      name: docker
      state: started
      enabled: yes
  - name: Ensure the docker group exists
    group:
      name: docker
      state: present
  - name: Add vagrant user to Docker group
    user:
      name: vagrant
      append: yes
      group: docker

Voila, we can now see any list of docker containers.

Docker Fedora

Conlusion

This is a simple walk-through on how to use Ansible to spin up a Vagrant box. We wrote a basic playbook that doesn’t do all that much other than enabling services, and installing a bunch of packages.

References

Ansible Official Documentation

Vagrant Official Documentation