2
respostas

Erro na identificação da variável de banco de dados

ASK [Cria o banco do MySQL] *** fatal: [172.17.177.42]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'wp_db_name' is undefined\n\nThe error appears to be in '/home/pablomoreira/ansible/.vagrant/provisioning.yml': line 20, column 9, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: 'Cria o banco do MySQL'\n ^ here\n"}

2 respostas

  • hosts: database handlers:

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

    tasks:

    • name: 'Instala pacotes de dependencia do sistema operacional' apt: name:

      - mysql-server-5.6
      - python-mysqldb

      state: latest become: yes

    • name: 'Cria o banco do 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 }}" passoword: "{{wp_user_password}}" priv: "{{ wp_db_name }}.*:ALL" state: present host: "{{ item }}" with_items:

      • 'localhost'
      • '127.0.0.1'
      • "{{ wp_host_ip }}"
    • name: 'Configura MySQL para aceitar conexoes remotas' copy: src: '/home/pablomoreira/ansible/files/my.cnf' dest: '/etc/mysql/my.conf' become: yes notify:

      • restart mysql
  • hosts: wordpress

    handlers:

    • name: restart apache service: name: apache2 state: restarted become: yes

    tasks:

    • name: 'Instala pacotes de dependencia do sistema operacional' apt: name:

      - php5
      - apache2
      - libapache2-mod-php5
      - php5-gd
      - libssh2-php
      - php5-mcrypt
      - php5-mysql

      state: latest become: yes

    • name: 'Baixa o arquivo de instalacao do Wordpress' get_url: url: 'https://wordpress.org/wordpress-5.0.tar.gz' dest: '/tmp/wordpress.tar.gz'

    • name: 'Descompacta o wordpress' unarchive: src: '/tmp/wordpress.tar.gz' dest: /var/www/ remote_src: yes become: yes

    • copy: src: "{{ wp_installation_dir }}/wp-config-sample.php" dest: "{{ wp_installation_dir }}wp-config.php" remote_src: yes become: yes

    • name: 'Configura o wp-config com as entradas do banco de dados'replace: path: "{{ wp_installation_dir }}/wp-config.php" regexp: "{{ item.regex }}" replace: "{{ item.value }}" backup: yes with_items:

      • { regex: 'database_name_here', value: "{{wp_db_name}}"}

      • { regex: 'username_here', value: "{{ wp_username }}"}

      • { regex: 'password_here', value: "{{ wp_user_password }}"}

      • { regex: 'local_host', value: "{{ wp_db_ip }} "} become: yes

        • name: 'Configura Apache para servir o Wordpress' copy: src: '/home/pablomoreira/ansible/files/000-default.conf' dest: '/etc/apache2/sites-available/000-default.conf' become: yes notify:
        • restart apache

Opa!

Você precisa configurar o nome do banco de dados que foi criado. Na aula tem um script que cria o usuário no banco, verifica o nome do banco que foi criado.

se não tiver criado o banco, você pode adicionar a criação do banco no script.

$script_mysql = <<-SCRIPT echo I am provisioning... apt-get update && apt-get install -y mysql-server-5.7 && mysql -e "CREATE DATABASE ..." && mysql -e "create user 'usuario'@'%' identified by 'pa$$w0rd' ;" SCRIPT