User Tools

Site Tools


ansible_module

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

ansible_module [2025/07/26 17:09] – - Imported by DokuWiki Advanced Plugin wikiadmansible_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/ansible.builtin.apt_repository/apt ===== 
- 
-<file yaml docker.yaml> 
---- 
-- hosts: local 
-  tasks: 
-   - name: Adiciona chave GPG para o repositório oficial do Docker 
-     ansible.builtin.apt_key: 
-       url: https://download.docker.com/linux/ubuntu/gpg 
-       state: present 
-     register: apt_key_add 
-   - name: Adiciona repositório do Docker 
-     when: apt_key_add is succeeded 
-     ansible.builtin.apt_repository: 
-       repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable 
-       state: present 
-     register: add_repo_docker 
-   - name: Instala o Docker 
-     when: add_repo_docker is succeeded 
-     apt: 
-       name: docker-ce 
-       state: present 
-       update_cache: true 
-</file> 
- 
-<file bash> 
-$ sudo ansible-playbook --syntax-check docker.yml 
-$ sudo ansible-playbook docker.yml 
-</file> 
- 
-===== Módulos apt/copy/service ===== 
-<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 
-       state: present 
-    register: nginx_installed 
-    notify: 
-      - Start Nginx 
-  - name: Define o arquivo de configuracao do servidor Nginx 
-    when: nginx_installed is succeeded 
-    copy: 
-      src: /home/suporte/modulos/files/nginx/nginx.conf 
-      dest: /etc/nginx/nginx.conf 
-      owner: root 
-      group: root 
-      mode: 0644 
-    notify: 
-      - Restart Nginx 
-  post_tasks: 
-  - name: Testa a instalação 
-    uri: 
-      url: http://172.16.0.200 
- 
-  handlers: 
-   - name: Start Nginx 
-     service: name=nginx state=started enabled=yes 
-   - name: Restart Nginx 
-     service: name=nginx state=restarted 
-</file> 
- 
-  * **pre_tasks:** Tarefa de pré-requisitos de verificação, antes de realizar a tarefa principal. 
-  * **post_tasks:** Tarefa de pós-validação, após realizar a tarefa principal. Exemplo: Testar acesso ao servidor nginx. 
- 
-===== 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 
-</file> 
- 
-  * 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. 
- 
-**''//--start-at-task//''** 
-Permite começar a executar seu playbook a partir de uma tarefa específica, sendo muito útil para realizar depurações. 
- 
-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="Testa a instalação" 
-</file> 
- 
-**''//--step//''** 
-Permite executar um playbook de modo interativo. 
-Como exemplo vamos aplicar o playbook do nginx: 
- 
-<file bash> 
-$ sudo ansible-playbook nginx.yml --ste 
-</file> 
- 
-**''//include_tasks//''** 
-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. 
- 
-**''//loop//''** 
-Permite executar uma tarefa várias vezes. 
-<file yaml apache.yaml> 
---- 
-- hosts: webservers 
-  tasks: 
-   - name: Inclui task de instalação do Apache 
-     include_tasks: 3.1-install-apache.yaml 
-   - name: Inclui task de configuração do Apache 
-     include_tasks: 3.2-configure-apache.yaml 
- 
-  handlers: 
-    - name: Restart Apache 
-      when: ansible_os_family == "Debian" 
-      ansible.builtin.service: 
-        name: apache2 
-        state: restarted 
- 
-    - name: Restart Httpd 
-      when: ansible_os_family == "RedHat" 
-      ansible.builtin.service: 
-        name: httpd 
-        state: restarted 
-        enabled: yes 
-</file> 
- 
-<file yaml install-apache.yaml> 
---- 
-- name: Instala pacotes para suporte a PHP no Apache em Distribuições Debian/Ubuntu 
-  when: ansible_os_family == "Debian" 
-  apt: 
-    name: "{{ item }}" 
-    state: present 
-    update_cache: true 
-  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/CentOS 
-  when: ansible_os_family == "RedHat" 
-  dnf: 
-    name: "{{ item }}" 
-    state: present 
-  loop: 
-    - epel-release 
-    - httpd 
-    - php 
-    - php-mysqlnd 
-  register: php_packages_centos_installed 
-  notify: 
-    - Restart Httpd 
-</file> 
- 
-<file yaml> 
-$ cat 3.2-configure-apache.yaml 
---- 
-- name: Remove o arquivo index.html 
-  when: 
-    - ansible_os_family == "Debian" 
-    - php_packages_ubuntu_installed is succeeded 
-  file: 
-    path: /var/www/html/index.html 
-    state: absent 
-- name: Define o arquivo de configuração para testar suporte PHP em Distribuições Debian/Ubuntu 
-  when: 
-    - ansible_os_family == "Debian" 
-    - php_packages_ubuntu_installed is succeeded 
-  copy: 
-    src: /home/suporte/modulos/files/web/index.php 
-    dest: /var/www/html/index.php 
-    owner: www-data 
-    group: www-data 
-    mode: 0644 
-- name: Define o arquivo de configuração para testar o suporte PHP em Distribuições RedHat/CentOS 
-  when: 
-    - ansible_os_family == "RedHat" 
-    - php_packages_centos_installed is succeeded 
-  copy: 
-    src: /home/suporte/modulos/files/web/index.php 
-    dest: /var/www/html/index.php 
-    owner: apache 
-    group: apache 
-    mode: 0644 
-</file> 
- 
-<file bash> 
-$ sudo ansible-playbook --syntax-check apache.yml 
-$ sudo ansible-playbook apache.yml 
-</file> 
ansible_module.1753560543.txt.gz · Last modified: by wikiadm