Ansible ======= Przygotowanie do zajęć ---------------------- Instalacja Ansible w salach: :command:`zypper install ansible`. Plik `Vagrantfile`: .. code-block:: ruby # -*- mode: ruby -*- # vi: set ft=ruby : Vagrant.configure(2) do |config| config.vm.box = "ubuntu/trusty64" config.vm.network "forwarded_port", guest: 80, host: 8080 end .. code-block:: bash vagrant up Ansible ------- Inwentarz serwerów: .. code-block:: bash cat inventory .. code-block:: bash 127.0.0.1 ansible_ssh_user=vagrant ansible_ssh_port=2222 Moduł pingowania, tryb komendowy ansible: .. code-block:: bash ansible all -i inventory --module-name ping --ask-pass ansible all -i inventory -m command -a "uptime" --ask-pass ansible all -i inventory -m command -a "ifconfig" --ask-pass Fakty: .. code-block:: bash ansible all -i inventory --module-name setup --ask-pass .. code-block:: yaml "ansible_facts": { "ansible_all_ipv4_addresses": [ "10.0.2.15" :command:], "ansible_all_ipv6_addresses": [ "fe80::a00:27ff:fe70:2a9d" ], "ansible_architecture": "x86_64", "ansible_bios_date": "12/01/2006", "ansible_bios_version": "VirtualBox", "ansible_cmdline": { "BOOT_IMAGE": "/boot/vmlinuz-3.13.0-68-generic", "console": "ttyS0", "ro": true, "root": "UUID=9abe6d68-619a-4f01-a0e3-583a1c02fe1d" }, "ansible_date_time": { "date": "2015-12-16", "day": "16", "epoch": "1450264997", "hour": "11", "iso8601": "2015-12-16T11:23:17Z", "iso8601_micro": "2015-12-16T11:23:17.970765Z", "minute": "23", "month": "12", "second": "17", "time": "11:23:17", "tz": "UTC", "tz_offset": "+0000", "weekday": "Wednesday", "year": "2015" }, "ansible_default_ipv4": { "address": "10.0.2.15", "alias": "eth0", "gateway": "10.0.2.2", "interface": "eth0", "macaddress": "08:00:27:70:2a:9d", "mtu": 1500, "netmask": "255.255.255.0", "network": "10.0.2.0", "type": "ether" }, Moduły: https://docs.ansible.com/ansible/modules_by_category.html Zainstalujmy coś: .. code-block:: bash ansible all -i inventory -m apt -a name=linuxlogo state=installed --ask-pass --sudo # odpamy to jeszcze raz ansible all -i inventory -m apt -a name=linuxlogo state=installed update_cache=yes --ask-pass --sudo Playbook (`plyabook.yml1`): .. code-block:: yaml - hosts: all tasks: - name: Install git apt: name=git state=present - name: Install linuxlogo apt: name=linuxlogo - name: Install some other packages apt: name={{ item }} with_items: - netris - sc - sl - nginx - name: Start nginx service: name: nginx state: started enabled: true .. code-block:: yaml ansible-playbook playbook1.yml -i inventory --ask-pass --sudo .. note:: export ANSIBLE_NOCOWS=0 Playbook inny: .. code-block:: yaml - hosts: all vars: ziom: "Studenci" tasks: - name: Install git apt: name=git state=present - name: Install linuxlogo apt: name=linuxlogo - name: Install some other packages apt: name={{ item }} with_items: - netris - sc - sl - nginx - name: Start nginx service: name: nginx state: started enabled: true - name: Deploy code to server template: src=myportal.j2 dest=/usr/share/nginx/html/index.html mode=0755 notify: - restart nginx handlers: - name: restart nginx service: name=nginx state=restarted enabled=true .. code-block:: html Hello {{ ziom }} .. code-block:: bash ansible-playbook playbook2.yml -i inventory --ask-pass --sudo vagrant ssh Idempotencja: .. code-block:: yaml - name: Start nginx service: name: nginx state: started enabled: true register: nginxinstalled - name: Deploy code to server template: src=myportal.j2 dest=/usr/share/nginx/html/index.html mode=0755 notify: - restart nginx when: nginxinstalled|success Zmieńmy `ziom: "Studenci"`: .. code-block:: bash ansible-playbook playbook2.yml -i inventory --ask-pass --sudo vagrant ssh curl localhost:8080 Role ---- .. code-block:: bash . ├── inventory ├── playbook.yml └── roles ├── app │   ├── tasks │   │   └── main.yml │   └── templates │   └── myportal.j2 ├── common_packages │   └── tasks │   └── main.yml └── nginx ├── handlers │   └── main.yml └── tasks └── main.yml playbook.yml: .. code-block:: yaml - hosts: all sudo: true vars: ziom: - "Darek" common_packages: - git - vim - mc # vars_files: # - vars/packages.yml roles: #- common_packages: - { role: common_packages, tags: ["common"] } - { role: nginx, tags: ["www"] } - { role: app, tags: ["www", "deploy"] } app/tasks/main.yml: .. code-block:: yaml - name: Deploy code to server template: src=myportal.j2 dest=/usr/share/nginx/html/index.html mode=0755 notify: - restart nginx app/templates/myportal.j2: .. code-block:: html This is {{ app }} portal on {{ ansible_hostname }}. Hello {{ ziom }} common_packages/tasks/main.yml: .. code-block:: yaml - name: Update apt cache apt: update_cache=yes - name: Install some common packages apt: name={{ item }} with_items: common_packages when: common_packages is defined .. code-block:: bash ansible-playbook playbook.yml -i inventory --ask-pass -e "app=appka1" Zadanie: 1. 3 maszyny na AWS, 2x nginx + 1x haproxy