Table of Contents

Variáveis no Ansible

Tipos de variáveis

Diretiva vars

vars-adduser.yml
---
- hosts: local
  vars:
    - usuario: linus
  tasks:
   - name: Adicionar usuario
     user: name={{ usuario }} state=present shell=/bin/bash password=$1$i5CwO/2J$JIaH55NqG10CDpYLqLAZf/
$ sudo ansible-playbook vars-adduser.yml

Diretiva vars_files

vars_files-pacotes.yml
---
- hosts: local
  vars_files:
    - vars.yml
  tasks:
   - name: Instala pacotes atraves de variaveis
     apt: name={{ pacotes }} state=present update_cache=true
vars.yml
pacotes:
  - elinks
  - wget
  - curl
  - htop
  - vim
$ sudo ansible-playbook --syntax-check vars_files-pacotes.yml
$ sudo ansible-playbook vars_files-pacotes.yml

Diretiva register

register-ntp.yml
---
- hosts: local
  tasks:
   - name: Instala pacote NTP
     apt: name=ntp state=present update_cache=true
     register: ntp_installed
   - name: Define o arquivo de configuracao do servidor NTP
     when: ntp_installed is succeeded
     copy: src=ntp.conf dest=/etc/ntp.conf owner=root group=root mode=0644
ntp.conf
driftfile /var/lib/ntp/ntp.drift
leapfile /usr/share/zoneinfo/leap-seconds.list
statistics loopstats peerstats clockstats
filegen loopstats file loopstats type day enable
filegen peerstats file peerstats type day enable
filegen clockstats file clockstats type day enable
pool a.ntp.br iburst
pool b.ntp.br iburst
pool c.ntp.br iburst
pool d.ntp.br iburst
restrict -4 default kod notrap nomodify nopeer noquery limited
restrict -6 default kod notrap nomodify nopeer noquery limited
restrict 172.16.0.0 mask 255.255.0.0
restrict ::1
restrict source notrap nomodify
#restrict source notrap nomodify noquery

Flag --extra-vars

create-dir.yml
---
- hosts: local
  tasks:
   - name: Adicionar estrutura de diretórios atraves de variavel
     file: dest={{ diretorios }} state=directory recurse=yes owner=root group=root mode=775
$ sudo ansible-playbook --syntax-check create-dir.yml
$ sudo ansible-playbook --extra-vars "diretorios=/tmp/var/www/html/intranet" create-dir.yml

Fatos

fatos-apache.yml
---
- hosts: webservers
  tasks:
   - name: Instala Apache no Debian/Ubuntu
     when: ansible_os_family == "Debian"
     apt: name=apache2 state=present update_cache=true
     register: apache2_installed
   - name: Instala Apache no CentOS
     when: ansible_os_family == "RedHat"
     yum:  name=httpd state=present
     register: httpd_installed
$ sudo ansible-playbook --syntax-check fatos-apache.yml
$ sudo ansible-playbook fatos-apache.yml
$ sudo ansible webserver1 -m shell -a 'dpkg -l | grep apache2'
$ sudo ansible webserver2 -m shell -a 'rpm -qa | grep httpd'