Changeset - d76a67a203e0
[Not reviewed]
0 1 0
Branko Majic (branko) - 2 years ago 2023-12-20 22:25:05
branko@majic.rs
MAR-189: Switch some of the usage instructions to be based on Debian Bullseye.
1 file changed with 9 insertions and 6 deletions:
0 comments (0 inline, 0 general)
docs/usage.rst
Show inline comments
 
.. _usage:
 

	
 
Usage
 
=====
 

	
 
Majic Ansible Roles are targeted at sysadmins who wish to deploy services for
 
their own, small-scale use. This chapter gives a simple tutorial-like set of
 
instructions for using all of the roles available.
 

	
 
.. contents:: :local:
 

	
 

	
 
Overview
 
--------
 

	
 
There is a number of different roles that can prove useful for setting-up a
 
small infrastructure of your own.
 

	
 
Some roles are suited for one-off operations during installation, like the
 
``preseed`` and ``bootstrap``, while some are better suited for periodic runs
 
for maintaining the users and integrity of the system.
 

	
 
By the end of following the instructions, you will have the following:
 

	
 
* Ansible server, used as controller for configuring and managing the
 
  remaining servers.
 
* Communications server, providing the LDAP, mail, and XMPP services.
 
* Web server, providing the web services.
 
* Backup server, used for storing all of the backups.
 

	
 
.. warning::
 
   Majic Ansible Roles support *only* Python 3 - both on the
 
   controller side and on the managed servers side.
 

	
 
   It is important to make sure that both the controller Python
 
   virtual environment used for Ansible *and* the interpreter for
 
   remote servers are *both* set-up to use Python 3.
 

	
 
   Python 3 is specified explicitly during virtual environment
 
   creation and in ``ansible.cfg`` configuration file
 
   (``interpreter_python`` option under ``defaults`` section).
 

	
 

	
 

	
 
Pre-requisites
 
--------------
 

	
 
For the set-up outlined in this usage guide you'll need the following:
 

	
 
* One server where Ansible will be installed at. Debian Buster will
 
* One server where Ansible will be installed at. Debian Bullseye will
 
  be installed on top of this server. The server will be set-up
 
  manually (this is currently out of scope for the *Majic Ansible
 
  Roles* automated set-up).
 
* Three servers where the services will be set-up. All servers must be able to
 
  communicate over network with each-other, the Ansible servers, and with
 
  Internet. Debian Buster will be installed on top of this server as part of the
 
  Internet. Debian Bullseye will be installed on top of this server as part of the
 
  usage instructions.
 
* Debian Buster network install CD.
 
* Debian Bullseye network install CD.
 
* All servers should be on the same network.
 
* IP addresses for all servers should be known.
 
* Netmask for all servers should be known.
 
* Gateway for all servers should be known.
 

	
 
In case of the servers listed above, it might be safest to have them as VMs -
 
this is cheapest thing to do, and simplest (who wants to deal with pesky hardware anyway?).
 

	
 
Usage instructions assume the following:
 

	
 
* Domain used for all servers is ``example.com``. If you wish to use a different
 
  domain, adjust the instructions accordingly.
 
* Server hostnames are ``ansible``, ``comms``, ``www``, and ``bak`` (for Ansible
 
  server, communications server, web server, and backup server, respectively).
 

	
 

	
 
Installing the OS on Ansible server
 
-----------------------------------
 

	
 
Start-off by installing the operating system on the Ansible server:
 

	
 
1. Fire-up the ``ansible`` server, and boot from the network installation CD.
 

	
 
2. Select the **Install** option.
 

	
 
3. Pick **English** as language.
 

	
 
4. Pick the country you are living in (or whatever else you want).
 

	
 
5. Pick the **en_US.UTF-8** locale.
 

	
 
6. Pick the **American English** keymap.
 

	
 
7. Configure the network if necessary.
 

	
 
8. Set the hostname to ``ansible``.
 

	
 
9. Set the domain to ``example.com``.
 

	
 
10. Set the root password.
 

	
 
11. Create a new user. For simplicity, call the user **Ansible user**, with
 
    username **ansible**.
 

	
 
12. Set-up partitioning in any way you want. You can go for **Guided - use
 
    entire disk** if you want to keep it simple and are just testing things.
 

	
 
13. Wait until the base system has been installed.
 

	
 
14. Pick whatever Debian archive mirror is closest to you.
 

	
 
15. If you have an HTTP proxy, provide its URL.
 

	
 
16. Pick if you want to participate in package survey or not.
 

	
 
17. Make sure that at least the **standard system utilities** and **SSH server**
 
    options are selected on task selection screen.
 

	
 
18. Wait for packages to be installed.
 

	
 
19. Install the GRUB boot loader on MBR.
 

	
 
20. Finalise the server install, and remove the installation media from server.
 

	
 

	
 
Installing required packages
 
----------------------------
 

	
 
With the operating system installed, it is necessary to install a couple of
 
packages, and to prepare the environment a bit on the Ansible server:
 

	
 
1. Install the necessary system packages (using the ``root`` account)::
 

	
 
     apt-get install -y virtualenv virtualenvwrapper git python3-pip python3-dev libffi-dev libssl-dev
 

	
 
2. Set-up loading of ``virtualenvwrapper`` via Bash completions (using the ``root`` account)::
 

	
 
2. Set-up the virtual environment (using the ``ansible`` account):
 
     ln -s /usr/share/bash-completion/completions/virtualenvwrapper /etc/bash_completion.d/virtualenvwrapper
 

	
 
3. Set-up the virtual environment (using the ``ansible`` account):
 

	
 
   .. warning::
 
      If you are already logged-in as user ``ansible`` in the server, you will
 
      need to log-out and log-in again in order to be able to use
 
      ``virtualenvwrapper`` commands!
 

	
 
   ::
 

	
 
     mkdir ~/mysite/
 
     mkvirtualenv -p /usr/bin/python3 -a ~/mysite/ mysite
 
     pip install -U pip setuptools
 
     pip install 'ansible~=2.9.0' dnspython
 

	
 
.. warning::
 
   The ``dnspython`` package is important since it is used internally via
 
   ``dig`` lookup plugin.
 

	
 

	
 
Cloning the *Majic Ansible Roles*
 
---------------------------------
 

	
 
With most of the software pieces in place, the only missing thing is the Majic
 
Ansible Roles:
 

	
 
1. Clone the git repository::
 

	
 
     git clone https://code.majic.rs/majic-ansible-roles ~/majic-ansible-roles
 

	
 
2. Checkout the correct version of the roles::
 

	
 
     cd ~/majic-ansible-roles/
 
     git checkout -b 7.0-dev 7.0-dev
 

	
 

	
 
Preparing the basic site configuration
 
--------------------------------------
 

	
 
Phew... Now that was a bit tedious and boring... But at least you are now ready
 
to set-up your own site :)
 

	
 
First of all, let's set-up some basic directory structure and configuration:
 

	
 
1. Create Ansible configuration file.
 

	
 
   .. warning::
 
      Since Ansible 2.x has introduced much stricter controls over security of
 
      deployed Python scripts, it is recommended (as in this example) to use the
 
      ``pipelining`` option (which should also improve performance). This is in
 
      particular necessary in cases where the SSH user connecting to remote
 
      machine is *not* ``root``, but there are tasks that use ``become`` with
 
      non-root ``become_user`` (which is the case in Majic Ansible Roles). See
 
      `official documentation
 
      <https://docs.ansible.com/ansible/latest/become.html#becoming-an-unprivileged-user>`_
 
      and other alternatives to this.
 

	
 
   :file:`~/mysite/ansible.cfg`
 

	
 
   ::
 

	
 
     [defaults]
 

	
 
     roles_path=/home/ansible/majic-ansible-roles/roles:/home/ansible/mysite/roles
 
     force_handlers = True
 
     inventory = /home/ansible/mysite/hosts
 
     interpreter_python = /usr/bin/python3
 

	
 
     [ssh_connection]
 
     pipelining = True
 

	
 
2. Create directory where retry files will be stored at (so they woudln't
 
   pollute your home directory)::
 

	
 
     mkdir ~/mysite/retry
 

	
 
3. Create the inventory file.
 

	
 
   :file:`~/mysite/hosts`
 

	
 
   ::
 

	
 
     [preseed]
 
     localhost ansible_connection=local
 

	
 
     [communications]
 
     comms.example.com
 

	
 
     [web]
 
     www.example.com
 

	
 
     [backup]
 
     bak.example.com
 

	
 
4. Create a number of directories for storing playbooks, group
 
   variables, SSH keys, X.509 artefacts (for TLS), and GnuPG keyring
 
   (we'll get to this later)::
 

	
 
@@ -332,360 +335,360 @@ So, let's set this up for start:
 
      ---
 

	
 
      - hosts: preseed
 
        roles:
 
          - preseed
 

	
 
2. Now we need to configure the role. Two parameters are mandatory -
 
   one that specifies where the preseed files are to be stored, and
 
   one that specifies the public key that should be used to
 
   pre-populate the SSH authorized keys for the ``root`` account. This
 
   is required for the initial bootstrap of servers because Debian
 
   GNU/Linux does not by default allow the ``root`` user to
 
   authenticate via SSH using a password. We will use the SSH public
 
   key generated earlier via the ``ssh-keygen`` command. Create the
 
   configuration file:
 

	
 
   :file:`~/mysite/group_vars/preseed.yml`
 

	
 
   ::
 

	
 
      ---
 

	
 
      # Public key used to authenticate remote logins via SSH for the
 
      # root account.
 
      ansible_key: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}"
 
      # Directory where the preseed files will be output to.
 
      preseed_directory: "~/mysite/preseed_files/"
 

	
 
3. Now we can generate the pressed files::
 

	
 
     workon mysite && ansible-playbook playbooks/preseed.yml
 

	
 
4. If all went well, you should have the following files created:
 

	
 
   * :file:`~/mysite/preseed_files/comms.example.com.cfg`
 
   * :file:`~/mysite/preseed_files/www.example.com.cfg`
 
   * :file:`~/mysite/preseed_files/bak.example.com.cfg`
 

	
 
5. You can have a look at them, but you might notice the settings in the file
 
   might not be to your liking. In particular, it could be using wrong timezone,
 
   defaulting to DHCP for network configuration etc. Let's concentrate on making
 
   the network configuration changes - this is the main thing that will probably
 
   differ in your environment. Update the preseed configuration file:
 

	
 
   :file:`~/mysite/group_vars/preseed.yml`
 

	
 
   ::
 

	
 
      ---
 

	
 
      # Public key used to authenticate remote logins via SSH for the
 
      # root account.
 
      ansible_key: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}"
 
      # Directory where the preseed files will be output to.
 
      preseed_directory: "~/mysite/preseed_files/"
 

	
 
      # Set your default (initial) root password.
 
      preseed_root_password: changeit
 
      # Use manual network configuration (no DHCP).
 
      preseed_network_auto: no
 
      # Set the gateway for all servers.
 
      preseed_gateway: 10.32.64.1
 
      # Set the netmask for all servers.
 
      preseed_netmask: 255.255.255.0
 
      # Set the DNS for all servers.
 
      preseed_dns: 10.32.64.1
 
      # Set the domain for all servers.
 
      preseed_domain: example.com
 
      # Set the server-specific options.
 
      preseed_server_overrides:
 
        comms.example.com:
 
          hostname: comms
 
          ip: 10.32.64.19
 
        www.example.com:
 
          hostname: www
 
          ip: 10.32.64.20
 
        bak.example.com:
 
          hostname: bak
 
          ip: 10.32.64.23
 

	
 
6. Now re-run the preseed playbook::
 

	
 
     workon mysite && ansible-playbook playbooks/preseed.yml
 

	
 
7. The preseed files should have been updated now, and you should have the new
 
   customised configuration files in the ``preseed_files`` directory. You can
 
   now use these to install the servers.
 

	
 

	
 
Installing the servers with preseed files
 
-----------------------------------------
 

	
 
You have your preseed files now, so you can go ahead and install the
 
servers ``comms.example.com``, ``www.example.com``, and
 
``bak.example.com`` using them with network install CD. Have a look at
 
`Debian instructions
 
<https://www.debian.org/releases/buster/amd64/apb.en.html>`_ for
 
<https://www.debian.org/releases/bullseye/amd64/apb.en.html>`_ for
 
more details.
 

	
 
If you need to, you can easily serve the preseed files from the Ansible server
 
with Python's built-in HTTP server::
 

	
 
  cd ~/mysite/preseed_files/
 
  python3 -m http.server 8000
 

	
 
Then you can point installer to the preseed file selecting the
 
``Advanced options -> Automated install`` (don't press ``ENTER`` yet),
 
then pressing ``TAB``, and appending the following at the end (just
 
fill-in the correct hostname - ``comms``, ``www``, or ``bak``)::
 

	
 
  url=http://ansible.example.com:8000/HOSTNAME.example.com.cfg
 

	
 

	
 
Bootstrapping servers for Ansible set-up
 
----------------------------------------
 

	
 
In order to effectively use Ansible, a small initial bootstrap always has to be
 
done for managed servers. This mainly involves set-up of Ansible users on the
 
destination machine, and distributing the SSH public keys for authorisation.
 

	
 
When you use the preseed configuration files to deploy a server, you get the
 
benefit of having the authorized_keys set-up for the root operating system user,
 
making it easier to bootstrap the machines subsequently via Ansible.
 

	
 
Let's bootstrap our machines now:
 

	
 
1. For start, create a dedicated playbook for the bootstrap process.
 

	
 
   :file:`~/mysite/playbooks/bootstrap.yml`
 

	
 
   ::
 

	
 
      ---
 

	
 
      - hosts:
 
          - communications
 
          - web
 
          - backup
 
        remote_user: root
 
        roles:
 
          - bootstrap
 

	
 
2. The ``bootstrap`` role has only one parameter - an SSH key which
 
   should be deployed for the Ansible user on managed server (in the
 
   ``authorized_keys`` file). Since this role is applied against all
 
   servers, we will use the same value everywhere. Configure the role:
 

	
 
   :file:`~/mysite/group_vars/all.yml`
 

	
 
   ::
 

	
 
      ---
 

	
 
      ansible_key: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}"
 

	
 
3. SSH into all machines at least once from the Ansible server in order to
 
   store the SSH fingerprints into known hosts file::
 

	
 
     ssh root@comms.example.com date && \
 
     ssh root@www.example.com date && \
 
     ssh root@bak.example.com date
 

	
 
4. Now, simply run the bootstrap role against the servers::
 

	
 
     workon mysite && ansible-playbook playbooks/bootstrap.yml
 

	
 
6. At this point you won't be able to ssh into the machines with the
 
   ``root`` account anymore. You will be able to ssh into the machines
 
   using the ``ansible`` user (from the Ansible server). The
 
   ``ansible`` user will also be granted ability to run the ``sudo``
 
   commands without providing password.
 

	
 
7. Now you can finally move on to configuring what you really want -
 
   common configuration and services for your site.
 

	
 

	
 
Common server configuration
 
---------------------------
 

	
 
Each server needs to share some common configuration in order to be functioning
 
properly. This includes set-up of some shared accounts, perhaps some hardening
 
etc.
 

	
 
.. note::
 
   Should you ever need to limit what hosts can connect to a server
 
   for some kind of maintenance or upgrade purposes, the ``common``
 
   role comes with ``maintenance`` and ``maintenance_allowed_hosts``
 
   parameters. See :ref:`rolereference` for more information.
 

	
 
Let's take care of this common configuration right away:
 

	
 
1. Create playbook for the communications server:
 

	
 
   :file:`~/mysite/playbooks/communications.yml`
 
   ::
 

	
 
      ---
 

	
 
      - hosts: communications
 
        remote_user: ansible
 
        become: yes
 
        roles:
 
          - common
 

	
 
2. Create playbook for the web server:
 

	
 
   :file:`~/mysite/playbooks/web.yml`
 
   ::
 

	
 
      ---
 

	
 
      - hosts: web
 
        remote_user: ansible
 
        become: yes
 
        roles:
 
          - common
 

	
 
3. Create playbook for the backup server:
 

	
 
   :file:`~/mysite/playbooks/backup.yml`
 
   ::
 

	
 
      ---
 

	
 
      - hosts: backup
 
        remote_user: ansible
 
        become: yes
 
        roles:
 
          - common
 

	
 
4. Create the global site playbook:
 

	
 
   :file:`~/mysite/playbooks/site.yml`
 
   ::
 

	
 
      ---
 

	
 
      - import_playbook: preseed.yml
 
      - import_playbook: communications.yml
 
      - import_playbook: web.yml
 
      - import_playbook: backup.yml
 

	
 
5. Time to create configuration for the role. Since this role is supposed to
 
   set-up a common base, we'll set-up the variables file that applies to all
 
   roles:
 

	
 
   :file:`~/mysite/group_vars/all.yml`
 
   ::
 

	
 
      ---
 

	
 
      ansible_key: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}"
 

	
 
      os_users:
 
        - name: admin
 
          uid: 1000
 
          additional_groups:
 
            - sudo
 
          authorized_keys:
 
            - "{{ lookup('file', '~/.ssh/id_rsa.pub') }}"
 
          password: "{{ 'admin' | password_hash('sha512') }}"
 

	
 
      common_packages:
 
        - emacs25-nox
 
        - emacs-nox
 

	
 
      ca_certificates:
 
        truststore: "{{ lookup('file', '~/mysite/tls/truststore.pem') }}"
 

	
 
   .. note::
 
      The ``common`` role comes with ability to set-up time
 
      synchronisation using NTP. This is not done by default. For
 
      details see the role parameter ``ntp_servers``.
 

	
 
   .. note::
 
      The ``ca_certificates`` parameter lets us deploy custom CA
 
      certificates on servers. The name we pick (in this case
 
      ``truststore``) can be set to anything. In this particular case,
 
      we want to deploy our own CA certificate for use as truststore,
 
      since this is what the services will use to validate server
 
      certificates when connecting to each-other.
 

	
 
6. That's all for configuration, time to apply the changes::
 

	
 
     workon mysite && ansible-playbook playbooks/site.yml
 

	
 
7. After this you should be able to *ssh* from Ansible server onto the
 
   managed servers as user ``admin`` using the *SSH* private key of
 
   the ``ansible`` user on controller machine. The ``admin`` user's
 
   password has also been set to ``admin``, and the user will be
 
   member of ``sudo`` group.
 

	
 
   .. note::
 
      Remote logins over SSH using password authentication are
 
      explicitly disabled as part of common set-up/hardening.
 

	
 

	
 
Introducing LDAP
 
----------------
 

	
 
Since some of the services actually depend on LDAP, we'll go ahead and set that
 
one up first. This includes both the LDAP *server* and *client* configuration.
 

	
 
1. Update the playbook for communications server to include the LDAP client and
 
   server roles (``ldap_client`` and ``ldap_server``, respectively).
 

	
 
   :file:`~/mysite/playbooks/communications.yml`
 
   ::
 

	
 
      ---
 

	
 
      - hosts: communications
 
        remote_user: ansible
 
        become: yes
 
        roles:
 
          - common
 
          - ldap_client
 
          - ldap_server
 

	
 
2. Update the playbook for web server to include the LDAP client role
 
   (``ldap_client``). You never know when it might come in handy :)
 

	
 
   :file:`~/mysite/playbooks/web.yml`
 
   ::
 

	
 
      ---
 

	
 
      - hosts: web
 
        remote_user: ansible
 
        become: yes
 
        roles:
 
          - common
 
          - ldap_client
 

	
 
3. Time to configure the roles. For start, let us configure the LDAP
 
   server role. Keep in mind that there is a lot of default variables
 
   set-up by the role itself, making our config rather short. The
 
   ``ldap_server_domain`` parameter will be used to form the base DN
 
   of the LDAP directory (resulting in ``dc=example,dc=com``).
 

	
 
   :file:`~/mysite/group_vars/communications.yml`
 
   ::
 

	
 
      ---
 

	
 
      ldap_admin_password: admin
 

	
 
      ldap_server_domain: example.com
 
      ldap_server_organization: "Example Inc."
 
      ldap_server_tls_certificate: "{{ lookup('file', '~/mysite/tls/comms.example.com_ldap.pem') }}"
 
      ldap_server_tls_key: "{{ lookup('file', '~/mysite/tls/comms.example.com_ldap.key') }}"
 

	
 
4. Phew. That was... Well, actually, easy :) Technically, only the
 
   LDAP admin password, domain, and TLS certificate/key *must* be set,
 
   but it is nice to have organisation explicitly specified as well
 
   (instead of using whatever Debian picks as default). Let us add the
 
   LDAP client configuration next. We will start off with global LDAP
 
   client configuration. In case of the LDAP client role, we have got
 
   to be a bit more explicit.
 

	
 
   :file:`~/mysite/group_vars/all.yml`
0 comments (0 inline, 0 general)