Skip to content

debovema/ansible_setup

Repository files navigation

Ansible setup

Ansible setup playbooks for workstation or server.

Usage

The playbooks are designed to be used with an Ansible inventory to target one or several hosts belonging to one or several groups.

Requirements

  • Python 3
  • A Python virtual environment configured with Ansible and its requirements

Create a Python virtual environment with Ansible

To create a Python virtual environment and install Ansible inside, run:

python3 -m venv .venv
source .venv/bin/activate
pip install --upgrade pip
pip install ansible

Python requirements

To install the required Python packages, run:

pip install -r requirements.txt

Roles and Collections

Before running the actual playboook, it is needed to install required roles and collections. This can be done with a simple command:

ansible-galaxy install -r requirements.yml

Run a Playbook

Workstation

First create your inventory, for instance:

cat > ./inventory.yml <<EOF
all:
  vars:
    timezone: "Europe/Paris"                    # the timezone for the workstation

    # Features
    feature_docker: true                        # add Docker packages (users with docker flag will be able to use it)
    feature_gnome_applications: true            # install Gnome applications (extensions)
    feature_gnome_shell: true                   # customize Gnome shell
    feature_ohmyzsh: true                       # install Oh My Zsh
    oh_my_zsh_theme: ys                         # the theme for Oh My Zsh
    users:                                      # users to create (or update) and their configuration
      - username: root
      - username: mathieu
        docker: true
EOF
# Check run and show diffs
ansible-playbook -i inventory.yml --check --diff -K playbooks/workstation/configure.yml -e "hosts_group=localhost"

ansible-playbook -i inventory.yml -K playbooks/workstation/configure.yml -e "hosts_group=localhost"

Server

First create your inventory, for instance:

cat > ./inventory.yml <<EOF
all:
  vars:
    ansible_private_key_file: ~/.ssh/id_ed25519 # the key used to connect to the hosts, not the one to authorize for users
    ansible_user: root                          # the user to connect to the hosts to configure them, not the one used to connect to them
    hostname: "{{ inventory_hostname }}"        # will use the "<hostname>" defined at the "all/children/<group>/hosts/<hostname>" inventory path
    timezone: "Europe/Paris"                    # the timezone for the server

    # Features
    feature_docker: true                        # add Docker packages (users with docker flag will be able to use it)
    feature_ohmyzsh: true                       # install Oh My Zsh
    oh_my_zsh_theme: ys                         # the theme for Oh My Zsh
    users:                                      # users to create (or update) and their configuration
      - username: root
      - username: mathieu
        ssh_authorized_keys: https://github.com/debovema.keys
        sudoernopassword: true
        docker: true
  children:
    scaleway: # a group to categorize your hosts (e.g. the Cloud provider is 'scaleway')
      hosts:
        devno1-3:
          ansible_host: 163.123.45.67
    hetzner: # a group to categorize your hosts (e.g. the Cloud provider is 'hetzner')
      hosts:
        devno1-4:
          ansible_host: 2a01:4ff:123:456::2
          # Hetzner rescue mode installation (optional)
          hetzner_install_disk_by_id_pattern: "*SAMSUNG*"
          hetzner_install_image: Debian-1202-bookworm-amd64-base.tar.gz
          # Features
          feature_wireguard_4in6_tunnel: true # copy wgclient.conf file in ansible/roles/wireguard_4in6_tunnel/files directory
EOF
# Check run and show diffs
ansible-playbook -i inventory.yml --check --diff ansible/playbooks/server/configure.yml

# Execute the playbook
ansible-playbook -i inventory.yml ansible/playbooks/server/configure.yml

Hetzner server

If using Hetzner hosts with rescue mode enabled, install and configure the hosts automatically:

ansible-playbook -i inventory.yml ansible/playbooks/hetzner/install.yml ansible/playbooks/server/configure.yml --limit 'hetzner'

If a host is not in rescue mode, the installation playbook will be ignored silently

Fully remote usage

  1. Install this collection and its requirements:
ansible-galaxy collection install debovema.ansible_setup
ansible-playbook debovema.ansible_setup.meta.requirements
  1. Retrieve your inventory from a custom Ansible setup inventory repository (for instance: debovema/ansible_setup_inventory), created with the Ansible setup inventory template:
git clone git@github.com:debovema/ansible_setup_inventory.git ~/.ansible_setup_inventory
  1. Execute the server configuration playbook:
cd ~/.ansible_setup_inventory
ansible-playbook debovema.ansible_setup.server.configure all

About

Quickly configure workstation or server with Ansible

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published