Solucionado (ver solução)
Solucionado
(ver solução)
2
respostas

Erro ao criar o banco de dados MySQL

Estou no capítulo 04 aula 02, criando o banco de dados através do playbook. Como eu só tenho windows, criei duas máquinas ubuntu 20.04lts para fazer. Uma sendo meu host ansible e a segunda a máquina controlada. O erro que está dando

fatal: [192.168.10.11]: FAILED! => {"changed": false, "msg": "unable to find /home/vagrant/.my.cnf. Exception message: (1698, \"Access denied for user 'root'@'localhost'\")"}

Como eu já tinha feito o curso de vagrant e lá foi usada outra abordagem dessa comunicação, tentei seguir o mesmo padrão alterando o arquivo mysqld.cnf . Mas ainda sim não obtive sucesso. meu vagrant file

$ansible_script = <<-SCRIPT
apt-get update && \
apt-get install -y ansible && \
apt-get upgrade -y
cp /vagrant/asnk /home/vagrant && \
chmod 600 /home/vagrant/asnk && \
chown vagrant:vagrant /home/vagrant/asnk
SCRIPT

Vagrant.configure("2") do |config|
    config.vm.box = "ubuntu/focal64"

    config.vm.define "ansible" do |ansible|
        ansible.vm.provider "virtualbox" do |v|
            v.name = "VM Ansible"
        end
        ansible.vm.network "private_network", ip: "192.168.10.10"
        ansible.vm.synced_folder "./config", "/home/vagrant/config"
        ansible.vm.provision "shell", inline: $ansible_script
    end

    config.vm.define "vmone" do |vmone|
        vmone.vm.provider "virtualbox" do |v|
            v.name = "VM one"
        end
        vmone.vm.network "private_network", ip: "192.168.10.11"
        vmone.vm.synced_folder "./config", "/home/vagrant/config"
        vmone.vm.provision "shell",
            inline: "apt-get update && \
                     apt-get upgrade -y && \
                     cat /vagrant/asnk.pub >> .ssh/authorized_keys"
    end
end

E esse é o playbook

---
- hosts: all
  tasks:
    - name: 'Install dependencies packages'
      apt:
        name:
          - php7.4
          - apache2
          - libapache2-mod-php7.4
          - php7.4-gd
          - php-ssh2
          - mysql-server
          - python3-mysqldb
          - php7.4-mysql
        state: latest
      become: yes

    - name: 'Creating Database MySQL'
      mysql_db:
        name: wordpress_db
        login_user: root
        login_password: 123456
        state: present

e meu arquivo hosts

[vmone]
192.168.10.11

[vmone:vars]
ansible_user=vagrant
ansible_ssh_private_key_file=/home/vagrant/asnk
ansible_python_interpreter=/usr/bin/python3
ansible_ssh_common_args='-o StrictHostKeyChecking=no'
2 respostas
solução!

Vinicius, atualizei para Ubuntu 20.04, PHP 8.01, MySQL 8.0.

Vagrantfile

Vagrant.configure("2") do |config|

    config.vm.box = "peru/ubuntu-20.04-server-amd64"

    config.vm.provider "virtualbox" do |v|
        v.memory = 1024
        v.gui = false
    end

    config.vm.define "wordpress" do |m|
        m.vm.network "public_network", ip: "10.0.0.105"
    end
end

my.cnf

#
# The MySQL database server configuration file.
#
# You can copy this to one of:
# - "/etc/mysql/my.cnf" to set global options,
# - "~/.my.cnf" to set user-specific options.
# 
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

#
# * IMPORTANT: Additional settings that can override those from this file!
#   The files must end with '.cnf', otherwise they'll be ignored.
#

!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/

[mysqld]
default-authentication-plugin=mysql_native_password

hosts

[wordpress]
10.0.0.105 ansible_user=vagrant ansible_ssh_private_key_file=".vagrant/machines/wordpress/virtualbox/private_key"

[wordpress:vars]
ansible_python_interpreter=/usr/bin/python3

provisioning.yml

---
- hosts: wordpress

  handlers:
    - name: RestartApache
      service:
        name: apache2
        state: restarted
      become: yes

  tasks:

    - name: 'Preparar S.O. para receber novo apt repository'
      shell: 'apt update && sudo apt install software-properties-common -y'
      become: yes

    - name: 'Adicionar repositorio do PHP ao ubuntu'
      ansible.builtin.apt_repository:
        repo: 'ppa:ondrej/php'
        state: present
      become: yes

    - name: 'Adicionar repositorio do ubuntu 18.04'
      ansible.builtin.apt_repository:
        repo: 'deb http://archive.ubuntu.com/ubuntu bionic main'
        state: present
      become: yes

    - name: 'Instalar pacotes de dependencia no S.O.'
      apt:
        name: 
          - php8.1
          - apache2
          - libapache2-mod-php8.1
          - php8.1-gd
          - php-ssh2
          - php-mcrypt
          - mysql-server-8.0
          - php8.1-mysql
          - python3-pip
        state: latest
      become: yes

    - name: 'Instalar PyMysql'
      become: true
      pip:
        name: pymysql
        state: present

    - name: 'Atualizar o arquivo /etc/mysql/my.cnf'
      template:
        src: my.cnf
        dest: /etc/mysql/my.cnf        
      become: yes

    - name: 'Reiniciar o mysql para novas configurações'
      service:
        name: mysql
        state: restarted
        enabled: yes
      become: yes

    - name: 'Criar banco de dados wordpress_db no mysql'
      command: mysql -u root --execute="CREATE DATABASE IF NOT EXISTS wordpress_db;"
      become: yes

    - name: 'Criar usuário wordpress_user no mysql'  
      command: mysql -u root --execute="CREATE USER IF NOT EXISTS 'wordpress_user'@'%' IDENTIFIED BY '12345';"
      become: yes

    - name: 'Atribuir permissões totais ao wordpress_user para o banco wordpress_db no mysql'  
      command: mysql -u root --execute="GRANT ALL PRIVILEGES ON wordpress_db.* TO 'wordpress_user'@'%'; FLUSH PRIVILEGES;"
      become: yes
      ignore_errors: yes

    - name: 'Fazer Download da última versão do Wordpress'
      get_url:
        url: 'https://wordpress.org/latest.tar.gz'
        dest: '/tmp/wordpress.tar.gz'

    - name: 'Descompactar o arquivo do Wordpress'
      unarchive:
        src: '/tmp/wordpress.tar.gz'
        dest: '/var/www/'
        remote_src: yes
      become: yes

    - name: 'Copiar arquivo de configuração de exemplo'
      copy:
        src: '/var/www/wordpress/wp-config-sample.php'  
        dest: '/var/www/wordpress/wp-config.php' 
        remote_src: yes
      become: yes

    - name: 'Configurar wp-config com as entradas do banco de dados'
      replace:
        path: '/var/www/wordpress/wp-config.php'
        regexp: '{{ item.regex }}'
        replace: '{{ item.value }}' 
      with_items:
        - { regex: 'database_name_here', value: 'wordpress_db'}
        - { regex: 'username_here', value: 'wordpress_user'}
        - { regex: 'password_here', value: '12345'}
      become: yes

    - name: 'Configurar o Apache para servir o Wordpress'
      copy:
        src: 'files/000-default.conf'
        dest: '/etc/apache2/sites-available/000-default.conf'
      become: yes
      notify:
        - RestartApache

Resumidamente (não são soluções boas):

  • mudei o /etc/mysql/my.cnf (volta ao padrão pré-php8)
  • usei 'command' para criar banco e senha

Espero que essas dicas sejam úteis!!

Muuito obrigado \o