IT-Wissen/ansible
2024-10-02 18:49:06 +02:00
..
README.md cypher 2024-10-02 18:49:06 +02:00

Table of Contents

Ansible

für Configuration Management

Generell

Playbooks starten Modules und Tasks, sequentiell ausgeführt. Playbooks beinhalten 1 oder mehrere Plays. Diese wiederum Tasks und Handlers. (Handler wird nur bei Erreichen des Endes eines Tasks ausgeführt). Bei grösseren Anwendungen, bestehen Playbooks aus Roles, die im Unterverzeichnis "roles" abgelegt sind. Diese wiederum haben Unterordner wie z.B. Tasks.

Mit Moduls können Tools in Tools verwendet werden, z.B. Powershell oder Python. Es gibt über 450 Module. Liste auf Website. Es gibt Run Commands Modules. Diese gibt es als reine Commands, oder als Shell, wo auch Pipes etc. gebraucht werden können. Script und Raw gibts auch noch.

Inventory = Listen von Ziel-Hosts, auf die Ansible zugreifen soll, mit Gruppen. Für jede Umgebung gibt es ein Inventory. Docuteam hat staging, production, tobiaswildi.

Bei Aufruf von Roles in Playbooks können Tags hinzugefügt werden, sodass später nur einzelne roles ausgeführt werden können, anstatt alle.

Aufruf eines .yml Playbooks:

¨ansible-playbook -i [inventory] filename.yml

Testumgebung Lokal

  • target-server (vagrant ubuntu) starten (nicht über virtualtoolbox starten!)
    • cmd: cd c:\entwicklung\ansible-test-host:
    • cmd: vagrant up
    • um von cmd in die shell zu wechseln (ist aber nicht nötig, ausser man will resultate von ansible befehlen anschauen gehen): cmd: vagrant ssh
  • WSL starten
    • weil key auf host geändert hat, muss neuer key angepasst werden. ssh-keygen -f "/home/roru/.ssh/known_hosts" -R "[127.0.0.1]:2222"
    • ssh 127.0.0.1 -p2222
    • check ob target erreichbar: ansible -i "127.0.0.1," -e "ansible_port=2222 ansible_user=vagrant" -m ping all

Playbook ausführen

ansible-playbook -i "127.0.0.1," -e "ansible_port=2222 ansible_user=vagrant" webserver.yml

oder, wenn eintrag in /etc/ansible/hosts:

ansible-playbook webserver.yml

eintrag wäre z.B.:

[testumgenbung]
local ansible_host=127.0.0.1 ansible_port=2222 ansible_user=vagrant

mit --check --diff am schluss wird Ausführung nur simuliert.

Beispiel Playbook (mit Endung .yml)

---
- hosts: local
  tasks:
    - name: create file
      file:
        path: "~/file.txt"
        state: touch

    - name: create file with content
      copy:
        dest: "~/file-with-content.txt"
        content: |
                dog1
                tiger

    - name: create multiple files
      file:
        path: "{{ item }}"
        state: touch
        mode: 0775
      with_items:
      - file1.txt
      - file2.txt
      - file3.txt

    - name: create multiple files with different attributes
      file:
        path: "{{ item.location }}"
        state: touch
        mode: "{{ item.mode }}"
      with_items:
      - { location: 'file10.txt',mode: '0566' }
      - { location: 'file11.txt',mode: '0766' }
...