Ansible Übung

Ansible Übung

sudo apt update
sudo apt install ansible

mkdir -p ansible/wordpress-setup/roles/web/tasks
mkdir -p ansible/wordpress-setup/roles/db/tasks
cd ansible/wordpress-setup

ssh-keygen
cat ~/.ssh/*.pub

Den public key in die ~/.ssh/authorized_keys auf den anderen Instanzen kopieren. Dann testweise einloggen:

ssh debian@debian-floating-ip
ssh ubuntu@ubuntu-floating-ip

Files anlegen: (IP-Adressen ändern nicht vergessen!)
hosts.ini
playbook.yml
roles/db/tasks/main.yml
roles/web/tasks/main.yml

ansible-playbook -i hosts.ini playbook.yml

hosts.ini

[db]
debian ansible_host=debian-floating-ip ansible_user=debian

[web]
ubuntu ansible_host=ubuntu-floating-ip ansible_user=ubuntu

playbook.yml

- name: Setup MariaDB auf Debian
  hosts: db
  become: yes
  roles:
    - db

- name: Setup Apache + WordPress auf Ubuntu
  hosts: web
  become: yes
  roles:
    - web

roles/db/tasks/main.yml

- name: Installiere benötigtes Python-Modul für MySQL (PyMySQL)
  apt:
    name: python3-pymysql
    state: present
    update_cache: yes

- name: Installiere MariaDB-Server
  apt:
    name: mariadb-server
    state: present
    update_cache: yes

- name: Starte und aktiviere MariaDB
  service:
    name: mariadb
    state: started
    enabled: yes

- name: Setze MariaDB root Passwort
  command: mysql -e "ALTER USER 'root'@'localhost' IDENTIFIED BY 'rootpass123';"
  args:
    creates: /root/.mariadb_root_configured
  register: set_root_pw

- name: Markiere Passwort gesetzt
  file:
    path: /root/.mariadb_root_configured
    state: touch
  when: set_root_pw is changed

- name: Erstelle Datenbank für WordPress
  mysql_db:
    name: wordpress
    state: present
    login_user: root
    login_password: rootpass123
    login_host: localhost

- name: Erstelle DB-User für WordPress
  mysql_user:
    name: wpuser
    password: wppass123
    priv: 'wordpress.*:ALL'
    host: '%'
    state: present
    login_user: root
    login_password: rootpass123
    login_host: localhost

- name: Erlaube externen Zugriff auf MariaDB
  lineinfile:
    path: /etc/mysql/mariadb.conf.d/50-server.cnf
    regexp: '^bind-address'
    line: 'bind-address = 0.0.0.0'

- name: Starte MariaDB neu
  service:
    name: mariadb
    state: restarted

roles/web/tasks/main.yml

- name: Stelle sicher, dass python3-six installiert ist
  become: yes
  apt:
    name: python3-six
    state: present
    update_cache: yes

- name: Installiere Apache2 und PHP
  apt:
    name:
      - apache2
      - php
      - php-mysql
      - libapache2-mod-php
      - unzip
      - curl
    state: present
    update_cache: yes

- name: Lösche Standard Apache index.html
  file:
    path: /var/www/html/index.html
    state: absent

- name: Lade WP-CLI herunter
  get_url:
    url: https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
    dest: /usr/local/bin/wp
    mode: '0755'

- name: Lade Wordpress herunter
  command: wp core download --path="/var/www/html" --force --allow-root

- name: Erstelle wp-config.php mit WP-CLI
  command: wp config create --dbname=wordpress --dbuser=wpuser --dbpass=wppass123 --dbhost=interne-dbhost-ip --path=/var/www/html --allow-root
  args:
    creates: /var/www/html/wp-config.php

- name: Führe WordPress-Installation durch
  command: wp core install --url=http://floating-ip-webhost --title="Meine tolle WP-Seite" --admin_user=admin --admin_password=SuperSicher123 --admin_email=meine@email.com --path=/var/www/html --allow-root

- name: Installiere de_AT
  command: wp language core install de_AT --path="/var/www/html" --url=http://floating-ip-webhost --allow-root

- name: Aktiviere de_AT
  command: wp language core activate de_AT --path="/var/www/html" --url=http://floating-ip-webhost --allow-root

- name: Setze Besitzrechte für Apache
  file:
    path: /var/www/html
    state: directory
    recurse: yes
    owner: www-data
    group: www-data

- name: Aktiviere Apache und starte
  service:
    name: apache2
    state: started
    enabled: yes