Escolar Documentos
Profissional Documentos
Cultura Documentos
● Push de configurações
Host 1
(ssh)
Inventário
Host 2
Host de
(ssh)
Gestão
(ansible)
Playbook
Host N
(ssh)
Requisitos
Hosts geridos:
[datacenter:vars]
● syntax INI ou YAML
ansible_ssh_user=pjs
ansible_become=yes
● possibilidade de inventários dinâmicos (p.e.
https://docs.ansible.com/ansible/latest/modules/modules_by_category.html
Execução Ad-Hoc
Execução de comandos:
ansible <hosts> -i <inventario> -m <modulo> [-a <args>]
https://docs.ansible.com/ansible/latest/cli/ansible.html
Playbooks ---
- hosts: webservers
vars:
myuser: pjs
tasks:
● escrito em YAML - name: install nginx
package: name=nginx state=present
- name: create user
● compostos por uma ou mais plays user:
name: "{{ myuser }}"
state: present
● cada play tem: home: "/srv/{{ myuser }}"
- hosts: dbs
○ hosts onde é executada (...)
○ lista de variáveis
Exemplo:
- name: test if hostname is bad
shell: grep -c bad.domain /etc/hostname
register: my_grep
Exemplo:
- name: install nagios-plugins
package: name="{{ item }}" state=present
loop:
- nagios-plugins-http
- nagios-plugins-disk
- nagios-plugins-procs
- nagios-plugins-load
- nagios-plugins-tcp
- nagios-plugins-ping
- nagios-plugins-users
- nagios-plugins-swap
Playbooks - handler
handler - tasks que só são executadas uma vez e quando houver alterações
Exemplo:
handlers:
- name: Restart ssh
service: name=sshd state=restarted
tasks:
- name: Disable root login with password
lineinfile: dest=/etc/ssh/sshd_config regexp="^PermitRootLogin" line="PermitRootLogin without-password"
notify: Restart ssh
- name: Force ssh loglevel
lineinfile: dest=/etc/ssh/sshd_config regexp="^LogLevel" line="LogLevel VERBOSE"
notify: Restart ssh
Templates - template:
src: templates/listen.j2
dest: /etc/httpd/conf.d/listen.conf
owner: apache
group: apache
● templates em formato Jinja2 mode: 0640
$ cat templates/listen.j2
# {{ ansible_managed }}
(http://jinja.pocoo.org/docs/templates/)
# http port
Listen {{ http_port }}
● variáveis de ansible usadas dentro dos {% if https_port is defined %}
# https port
Listen {{ https_port }}
{% endif %}
templates
ficheiros
Roles
https://docs.ansible.com/ansible/latest/index.html
https://www.ansible.com/resources
https://www.udemy.com/ansible-essentials-simplicity-in-automation/
● Ansible em Windows
● Tower / AWX