ansible_module
Differences
This shows you the differences between two versions of the page.
| ansible_module [2025/07/26 17:09] – - Imported by DokuWiki Advanced Plugin wikiadm | ansible_module [Unknown date] (current) – removed - external edit (Unknown date) 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Gerenciar Módulos no Ansible ====== | ||
| - | ===== Módulos ansible.builtin.apt_key/ | ||
| - | |||
| - | <file yaml docker.yaml> | ||
| - | --- | ||
| - | - hosts: local | ||
| - | tasks: | ||
| - | - name: Adiciona chave GPG para o repositório oficial do Docker | ||
| - | | ||
| - | url: https:// | ||
| - | | ||
| - | | ||
| - | - name: Adiciona repositório do Docker | ||
| - | when: apt_key_add is succeeded | ||
| - | | ||
| - | repo: deb [arch=amd64] https:// | ||
| - | | ||
| - | | ||
| - | - name: Instala o Docker | ||
| - | when: add_repo_docker is succeeded | ||
| - | apt: | ||
| - | name: docker-ce | ||
| - | | ||
| - | | ||
| - | </ | ||
| - | |||
| - | <file bash> | ||
| - | $ sudo ansible-playbook --syntax-check docker.yml | ||
| - | $ sudo ansible-playbook docker.yml | ||
| - | </ | ||
| - | |||
| - | ===== Módulos apt/ | ||
| - | <file yaml nginx.yaml> | ||
| - | - hosts: balancer | ||
| - | pre_tasks: | ||
| - | - name: Instala dependências | ||
| - | dnf: | ||
| - | name: epel-release | ||
| - | state: present | ||
| - | tasks: | ||
| - | - name: Instala servidor Nginx | ||
| - | dnf: | ||
| - | name: nginx | ||
| - | | ||
| - | register: nginx_installed | ||
| - | notify: | ||
| - | - Start Nginx | ||
| - | - name: Define o arquivo de configuracao do servidor Nginx | ||
| - | when: nginx_installed is succeeded | ||
| - | copy: | ||
| - | src: / | ||
| - | dest: / | ||
| - | owner: root | ||
| - | group: root | ||
| - | mode: 0644 | ||
| - | notify: | ||
| - | - Restart Nginx | ||
| - | post_tasks: | ||
| - | - name: Testa a instalação | ||
| - | uri: | ||
| - | url: http:// | ||
| - | |||
| - | handlers: | ||
| - | - name: Start Nginx | ||
| - | | ||
| - | - name: Restart Nginx | ||
| - | | ||
| - | </ | ||
| - | |||
| - | * **pre_tasks: | ||
| - | * **post_tasks: | ||
| - | |||
| - | ===== Handlers ===== | ||
| - | |||
| - | Handlers executará uma determinada ação quando uma tarefa termina com um estado alterado. | ||
| - | Handlers são muito poderosos e úteis, uma vez que não são executados cada vez que eles são chamados. Em vez disso, a ação necessária pelo handler será executado apenas uma vez no fim do PlayBook. | ||
| - | |||
| - | <file bash> | ||
| - | $ sudo ansible-playbook --syntax-check nginx.yml | ||
| - | $ sudo ansible-playbook nginx.yml | ||
| - | </ | ||
| - | |||
| - | * Caso seja concluída a tarefa de instalar o pacote nginx, notifique (notify) o Handler Start Nginx; | ||
| - | * Caso seja concluída a tarefa de configurar o arquivo nginx.conf, notifique (notify) o Handler Restart Nginx. | ||
| - | |||
| - | **''// | ||
| - | Permite começar a executar seu playbook a partir de uma tarefa específica, | ||
| - | |||
| - | Como exemplo, vamos aplicar o playbook do nginx, iniciando pelo final, onde testamos o acesso a página: | ||
| - | <file bash> | ||
| - | $ sudo ansible-playbook nginx.yml --start-at-task=" | ||
| - | </ | ||
| - | |||
| - | **''// | ||
| - | Permite executar um playbook de modo interativo. | ||
| - | Como exemplo vamos aplicar o playbook do nginx: | ||
| - | |||
| - | <file bash> | ||
| - | $ sudo ansible-playbook nginx.yml --ste | ||
| - | </ | ||
| - | |||
| - | **''// | ||
| - | Permite incluir uma coleção de playbooks em um único arquivo. O objetivo desse módulo é: | ||
| - | * Incluir uma task de instalação de pacotes para suporte a PHP no Apache em Distribuições Ubuntu e Centos; | ||
| - | * Incluir uma task para criar o arquivo index.php, e assim testar o suporte PHP em Distribuição Ubuntu e Centos; | ||
| - | * Incluir handlers para reiniciar o serviço do Apache em Distribuição Ubuntu e Centos. | ||
| - | |||
| - | **''// | ||
| - | Permite executar uma tarefa várias vezes. | ||
| - | <file yaml apache.yaml> | ||
| - | --- | ||
| - | - hosts: webservers | ||
| - | tasks: | ||
| - | - name: Inclui task de instalação do Apache | ||
| - | | ||
| - | - name: Inclui task de configuração do Apache | ||
| - | | ||
| - | |||
| - | handlers: | ||
| - | - name: Restart Apache | ||
| - | when: ansible_os_family == " | ||
| - | ansible.builtin.service: | ||
| - | name: apache2 | ||
| - | state: restarted | ||
| - | |||
| - | - name: Restart Httpd | ||
| - | when: ansible_os_family == " | ||
| - | ansible.builtin.service: | ||
| - | name: httpd | ||
| - | state: restarted | ||
| - | enabled: yes | ||
| - | </ | ||
| - | |||
| - | <file yaml install-apache.yaml> | ||
| - | --- | ||
| - | - name: Instala pacotes para suporte a PHP no Apache em Distribuições Debian/ | ||
| - | when: ansible_os_family == " | ||
| - | apt: | ||
| - | name: "{{ item }}" | ||
| - | state: present | ||
| - | update_cache: | ||
| - | loop: | ||
| - | - apache2 | ||
| - | - php7.4 | ||
| - | - php7.4-mysql | ||
| - | register: php_packages_ubuntu_installed | ||
| - | notify: | ||
| - | - Restart Apache | ||
| - | - name: Instala pacotes para suporte a PHP no Apache em Distribuições RedHat/ | ||
| - | when: ansible_os_family == " | ||
| - | dnf: | ||
| - | name: "{{ item }}" | ||
| - | state: present | ||
| - | loop: | ||
| - | - epel-release | ||
| - | - httpd | ||
| - | - php | ||
| - | - php-mysqlnd | ||
| - | register: php_packages_centos_installed | ||
| - | notify: | ||
| - | - Restart Httpd | ||
| - | </ | ||
| - | |||
| - | <file yaml> | ||
| - | $ cat 3.2-configure-apache.yaml | ||
| - | --- | ||
| - | - name: Remove o arquivo index.html | ||
| - | when: | ||
| - | - ansible_os_family == " | ||
| - | - php_packages_ubuntu_installed is succeeded | ||
| - | file: | ||
| - | path: / | ||
| - | state: absent | ||
| - | - name: Define o arquivo de configuração para testar suporte PHP em Distribuições Debian/ | ||
| - | when: | ||
| - | - ansible_os_family == " | ||
| - | - php_packages_ubuntu_installed is succeeded | ||
| - | copy: | ||
| - | src: / | ||
| - | dest: / | ||
| - | owner: www-data | ||
| - | group: www-data | ||
| - | mode: 0644 | ||
| - | - name: Define o arquivo de configuração para testar o suporte PHP em Distribuições RedHat/ | ||
| - | when: | ||
| - | - ansible_os_family == " | ||
| - | - php_packages_centos_installed is succeeded | ||
| - | copy: | ||
| - | src: / | ||
| - | dest: / | ||
| - | owner: apache | ||
| - | group: apache | ||
| - | mode: 0644 | ||
| - | </ | ||
| - | |||
| - | <file bash> | ||
| - | $ sudo ansible-playbook --syntax-check apache.yml | ||
| - | $ sudo ansible-playbook apache.yml | ||
| - | </ | ||
ansible_module.1753560543.txt.gz · Last modified: by wikiadm
