Skip to content

Ansible - ein kurzer Test

Linux
  • Kur vorab, ich denke nicht das ich zum jetzigen Zeitpunkt einen Einblick darin habe, was Ansible alles kann. Aber, da ich hier immer als notiere, was ich so ausprobiere, möchte ich auch das hier festhalten.

    Sollte das jemand lesen, der davon richtig Ahnung hat, Vorsicht hier kann einiges Falsche stehen! Ich bitte in so einem Fall um einen Kommentar, damit ich das ändern oder löschen kann. Vielen Dank!

    Was ist Ansible?

    Ansible ist ein Open-Source Automatisierungs-Werkzeug zur Orchestrierung und allgemeinen Konfiguration und Administration von Computern
    Quelle: https://de.wikipedia.org/wiki/Ansible

    Die Projektseite -> https://www.ansible.com/

    Warum Ansible?

    Um alle meine Server immer schön auf dem aktuellen Stand zu halten, nutze ich zur Zeit ClusterSSH. Wenn es aber etwas umfangreicher und komplizierter werden sollte, scheint es nicht das rechte Tool zu sein.

    Da ich gerne eine erstellte VM auf einen von mir festgelegten Stand bringen möchte, erschien mir dieses Tool als geeignet.

    Installation

    apt install ansible
    

    Kontrolle

    frank@frank-MS-7C37:~$ ansible --version
    ansible 2.9.6
      config file = /etc/ansible/ansible.cfg
      configured module search path = ['/home/frank/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
      ansible python module location = /usr/lib/python3/dist-packages/ansible
      executable location = /usr/bin/ansible
      python version = 3.8.10 (default, Sep 28 2021, 16:10:42) [GCC 9.3.0]
    

    Konfiguration Ansible

    Die findet man unter /etc/ansible/

     frank@frank-MS-7C37:~$ ls -lha /etc/ansible/
        insgesamt 40K
        drwxr-xr-x   2 root root 4,0K Nov 13 15:47 .
        drwxr-xr-x 156 root root  12K Nov 13 15:44 ..
        -rw-r--r--   1 root root  20K Mär  5  2020 ansible.cfg
        -rw-r--r--   1 root root 1005 Nov 13 15:47 hosts
    

    Die ansible.cfg habe ich auf Standard gelassen. In der hosts Datei konfiguriert man die Server, die man administrieren möchte. Also z.B.

    192.168.3.10    
    

    Playbook

    Ansible wird mit YAML Textdateien konfiguriert. Ich hatte erst angefangen, diese mit nano zu editieren habe aber sehr schnell festgestellt das das keine gute Idee ist. YAML ist sehr empfindlich was die Formatierung angeht und das sieht man in einem richtigen Code-Editor einfach besser. Somit habe ich das Playbook in Codium ertsellt und editiert. Dort kann man bei Erstellung auch direkt YAML auswählen.

    Ich habe in meinem Homeordner eine Datei task.yml erstellt. Der Name ist beliebig.

    ---
    - name: My task
      hosts: all
      tasks:
    
    # Update and install the base software
      - name: Update apt package cache.
        apt:
          update_cache: yes
          cache_valid_time: 600
    
      - name: Upgrade installed apt packages.
        apt:
          upgrade: 'yes'
          #register: upgrade
    
      - name: Ensure that a base set of software packages are installed.
        apt:
          pkg:
           # - build-essential
           # - curl
            - fail2ban
           # - firewalld
           # - git
            - htop
           # - needrestart
           # - pwgen
           # - resolvconf
            - restic
            - rsync
           # - sudo
           # - unbound
           # - unzip
           # - vim-nox
          state: latest
    

    In diesem Beispiel wird erst mal alles aktualisiert. Danach wird eine Liste von Tools installiert, die ich gerne hätte.

    Aufruf des Playbooks

    ansible-playbook playbooks/task.yml
    

    Je nach Konfiguration Eurers Servers kann es evt. folgende Ausgabe kommen.

    frank@frank-MS-7C37:~$ ansible-playbook playbooks/task.yml 
    
    PLAY [My task] **************************************************************************************************************************************************************************
    
    TASK [Gathering Facts] ******************************************************************************************************************************************************************
    fatal: [<Server-IP>]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: frank@<Server-IP>: Permission denied (publickey,password).", "unreachable": true}
    
    PLAY RECAP ******************************************************************************************************************************************************************************
    <Server-IP>     : ok=0    changed=0    unreachable=1    failed=0    skipped=0    rescued=0    ignored=0  
    

    Der Grund ist, das zum Login der Public-Key benötigt wird. Also root@<Server-IP>, oben ist das ja mit frank@<Server-IP> erfolgt.

    ansible-playbook -u root playbooks/task.yml
    

    Ein -u root übergibt den Benutzer root und nun klappt der Zugriff. Ausgabe

     frank@frank-MS-7C37:~$ ansible-playbook -u root playbooks/task.yml 
     
     PLAY [My task] **************************************************************************************************************************************************************************
     
     TASK [Gathering Facts] ******************************************************************************************************************************************************************
     [WARNING]: Platform linux on host <Server-IP> is using the discovered Python interpreter at /usr/bin/python3, but future installation of another Python interpreter could
     change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
     ok: [<Server-IP>
     
    
     TASK [Update apt package cache.] ********************************************************************************************************************************************************
     changed: [<Server-IP>]
     
     TASK [Upgrade installed apt packages.] **************************************************************************************************************************************************
     ok: [<Server-IP>]
     
     TASK [Ensure that a base set of software packages are installed.] ***********************************************************************************************************************
     ok: [<Server-IP>]
     
     PLAY RECAP ******************************************************************************************************************************************************************************
     <Server-IP>     : ok=4    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
    

    Fazit

    Sieht nach einem sehr mächtigen Werkzeug aus und auch extrem umfangreich. Aber auch das was ich suche um ein paar Dinge zu erledigen, die ich nicht immer wieder von Hand machen möchte. Beispiele

    • apt update && apt upgrade
    • packages installieren
    • firewall installieren (Mein Standardfile)
    • und viele Dinge, die mir im Moment nicht einfallen 😉

    Fortsetzung folgt...