On Github fntlnz / ansible-milan-kickoff
Lorenzo Fontana
Software Engineer / DevOps at Facile.it
Flexible agentless IT Automation tool that let's you automate everything in all your systems
Batteries included
No remote agents needed, Ansible delivers all modules to remote systems and execute tasks as needed and clean up themeselves when complete.
- name: Install gcc dnf: name=gcc state=latest - name: Install cmake dnf: name=cmake state=latest
Tasks combine an action (a module and its arguments) with a name, and optionally some other keywords
Yep, dnf is a module
- name: Install build dependencies dnf: name={{item}} state=latest with_items: - automake - gcc-c++ - libtool - pkgconfig - unzip - gcc - make - cmake
with_items is one of those other keywords
- name: Clone Neovim repository (nightly) git: repo=https://github.com/neovim/neovim.git dest=/tmp/neovim-checkout version=nightly depth=1
- name: make deps command: make deps args: chdir: /tmp/neovim-checkout
When something happen, do something
Handlers are tasks that do not run unless they are notified
- name: template configuration file template: src=nginx.conf dest=/usr/local/nginx/nginx.conf notify: - restart nginx
handlers: - name: restart nginx service: name=nginx state=restartedWhat is service? a Module!
Roles are unit of organization in Ansible. They contains tasks, handlers, templates, variables etc..
roles/ nginx/ tasks/ main.yml handlers/ main.yml templates/ nginx.conf mysite.conf files/ bar.txt foo.sh vars/ main.yml defaults/ main.yml
A playbooks is a list of plays, a play is a mapping between a set of hosts and the tasks which run on those hosts to define the role that those systems will perform
--- - hosts: webservers vars: http_port: 80 remote_user: root tasks: - name: ensure nginx is at the latest version yum: name=nginx state=latest - name: write the nginx config file template: src=nginx.conf dest=/usr/local/nginx/nginx.conf notify: - restart nginx - name: ensure nginx is running (and enable it at boot) service: name=nginx state=started enabled=yes handlers: - name: restart nginx service: name=nginx state=restarted
Not very reusable/extensible
--- - hosts: webservers vars: http_port: 80 roles: - nginx - php-fpm - hosts: dbservers roles: - mysql
ansible-playbook myplaybook.yml -vvvv
Do something really quick, but don't want to save for later
$ ansible dbservers -m command -a "whoami" db02 | success | rc=0 >> fntlnz db01 | success | rc=0 >> fntlnz db03 | success | rc=0 >> fntlnz
$ ansible dbservers -m ping db02 | success >> { "changed": false, "ping": "pong" } db03 | success >> { "changed": false, "ping": "pong" } db01 | success >> { "changed": false, "ping": "pong" }