1
resposta

Execução para no handler de restar do mysql

Ao rodar a playbook, já com os arquivos do final do curso, a execução para no handler de restart do mysql, fica nisso.

user@localhost:~/projects_ti/cursos/curso-alura-ansible(master)$ ansible-playbook ansible/provisioning.yml -i ansible/hosts

PLAY [database] **********************************************************************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************************************************************************
The authenticity of host '192.168.200.42 (192.168.200.42)' can't be established.
ECDSA key fingerprint is SHA256:UX71GremzUeYI4Hqq2ttRQi/amPP5o4VDBubdPeIcyc.
Are you sure you want to continue connecting (yes/no)? yes
ok: [192.168.200.42]

TASK [mysql : Instalação de pacotes de dependências no S.O.] *************************************************************************************************************************************************
[DEPRECATION WARNING]: Invoking "apt" only once while using a loop via squash_actions is deprecated. Instead of using a loop to supply multiple items and specifying `name: "{{ item }}"`, please use `name: 
['mysql-server-5.6', 'python-mysqldb']` and remove the loop. This feature will be removed in version 2.11. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
changed: [192.168.200.42] => (item=['mysql-server-5.6', 'python-mysqldb'])

TASK [mysql : Criando o banco do MySql] **********************************************************************************************************************************************************************
changed: [192.168.200.42]

TASK [mysql : Adicionando o usuário com permissão à base que será utilizada] *********************************************************************************************************************************
changed: [192.168.200.42] => (item=192.168.200.40)
[WARNING]: The value ******** (type int) in a string field was converted to u'********' (type string). If this does not look like what you expect, quote the entire value to ensure it does not change.
[WARNING]: Module did not set no_log for update_password

TASK [mysql : Copia arquivo de configuração do mysql - my.cnf] ***********************************************************************************************************************************************
changed: [192.168.200.42]

RUNNING HANDLER [mysql : restart mysql] **********************************************************************************************************************************************************************

Não vai para lugar nenhum depois disso.

Alguém passou por algo parecido?

Minha versão do ansible é:

ansible 2.9.6
  config file = None
  configured module search path = ['/home/user/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.6/dist-packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 3.6.9 (default, Nov  7 2019, 10:44:02) [GCC 8.3.0]

A role mysql (roles/mysql/tasks/main.yml):

---
- name: 'Instalação de pacotes de dependências no S.O.'
  apt:
    name: "{{ item }}"
    state: latest
  become: yes
  with_items:
    - mysql-server-5.6
    - python-mysqldb
- name: 'Criando o banco do MySql'
  mysql_db:
    name: "{{ wp_db_name }}"
    login_user: root
    state: present

- name: 'Adicionando o usuário com permissão à base que será utilizada'
  mysql_user:
    login_user: root
    name: "{{ wp_username }}"
    password: "{{ wp_user_password }}"
    priv: "{{ wp_db_name }}.*:ALL"
    state: present
    host: "{{ item }}"
  with_items:
    - "{{ wp_host_ip }}"

- name: 'Copia arquivo de configuração do mysql - my.cnf'
  copy: 
    src: 'files/my.cnf'
    dest: '/etc/mysql/my.cnf'
  become: yes
  notify: 
    - restart mysql
...

O handler da role (roles/mysql/handlers/main.yml):

---
- name: restart mysql
  service:
    name: mysql
    state: restarted
  become: yes

Alguém?

1 resposta

Olá Augusto, tudo bem? Nas versões mais recentes do Ansible houve uma atualização, e o with_items foi depreciado, e não está mais funcionando para algumas coisas, então agora temos que utilizar o apenas informando nomes dos serviços da seguinte forma:

name:
      - mysql-server-5.6
      - python-mysqldb

E para estruturas de repetição (loops) temos utilizar o loop como:

loop:
    - "{{ wp_host_ip }}"

Então tenta utilizar o arquivo main.yml com esse código:

---
- name: 'Instala pacotes de dependencia do sistema operacional'
  apt:
    name:
      - mysql-server-5.6
      - python-mysqldb
    state: latest
  become: yes

- name: 'Cria o banco MySQL'
  mysql_db:
    name: "{{ wp_db_name }}"
    login_user: root
    state: present

- name: 'Cria o usuário do MySQL'
  mysql_user:
    login_user: root
    name: "{{ wp_username }}"
    password: "{{ wp_db_password }}"
    priv: "{{ wp_db_name }}.*:ALL"
    state: present
    host: "{{ item }}"
  loop:
    - "{{ wp_host_ip }}"

- name: 'Configura MySQL para aceitar conexoes remotas'
  copy:
    src: 'files/my.cnf'
    dest: '/etc/mysql/my.cnf'
  become: yes
  notify:
    - restart mysql

E fala pra gente se funcionou!