7
respostas

Erro ao executar arquivos do group-vars

Ao executar o playbook para de acordo com a linha abaixo, recebi um erro o qual não consigo resolver. Fatos: -Antes de substituir pelas variáveis estava Ok -Deu erro de ip host mas add na linha do playbook.

erro:

wordpress_com_ansible % ansible-playbook -i 746-aula1/hosts --extra-vars 'wp_host_ip=172.17.177.40 wp_db_ip=172.17.177.42' 746-aula5/provisioning.yml

PLAY [database] **

TASK [Gathering Facts] *** ok: [172.17.177.42]

TASK [Instala pacotes de dependencia do sistema operacional] * ok: [172.17.177.42]

TASK [Cria o banco no MySQL] * ok: [172.17.177.42]

TASK [Cria o usuário no MySQL] *** fatal: [172.17.177.42]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'wp_username' is undefined\n\nThe error appears to be in '/Users/anderson/wordpress_com_ansible/746-aula5/provisioning.yml': line 23, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n state: present\n - name: 'Cria o usuário no MySQL'\n ^ here\n"}

PLAY RECAP ***

172.17.177.42 : ok=3 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0

Código:

---
- hosts: database
  handlers:
    - name: restart mysqld
      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 no MySQL'
      mysql_db:
        name: wordpress_db
        login_user: root
        state: present
    - name: 'Cria o usuário no MySQL'
      mysql_user:
        login_user: root
        name: "{{ wp_username }}"
        password: "{{ 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 conexões remotas'
      copy:
        src: '/Users/anderson/wordpress_com_ansible/my.cnf'
        dest: '/etc/mysql/my.cnf'
      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-fpm', 'apache2', 'libapache2-mod-php5', 'php5-gd', 'php5-mcrypt', 'python-mysqldb', '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'
        mode: 0440

    - 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: 'passaword_here', value: "{{ wp_user_password }}"}
          - { regex: 'localhost', value: "{{ wp_db_ip }}"}
        become: yes

    - name: 'Configura Apache para servir o Wordpress'
      copy:
        src: 'files/000-default.conf'
        dest: '/etc/apache2/sites-available/000-default.conf'

      notify:
      - restart apache
      become: yes
7 respostas

Olá Anderson, tudo bem? Peço desculpas mas eu não entendi muito bem o comando que você indicou:

wordpress_com_ansible % ansible-playbook -i 746-aula1/hosts --extra-vars 'wp_host_ip=172.17.177.40 wp_db_ip=172.17.177.42' 746-aula5/provisioning.yml

Poderia verificar esse comando novamente? Outra coisa, poderia verificar esse outro tópico em que o Anderson Almeida estava com um problema semelhante.

Fico aguardando retorno!

Boa tarde Jonilson. Eu vi o tópico do Anderson sim. Cheguei a tentar com o --extra-vars . O resultado foi este:

TASK [Cria usuario no MySQL] *************************************************************************************************************************************************
fatal: [172.17.177.42]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'wp_username' is undefined\n\nThe error appears to be in '/Users/anderson/wordpress_com_ansible/746-aula5/provisioning.yml': line 24, column 7, 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 usuario no MySQL'\n      ^ here\n"}

PLAY RECAP *******************************************************************************************************************************************************************
172.17.177.42              : ok=3    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

Eu cheguei a criar um var no mesmo nolde

---
wp_username: wordpress_user
wp_db_name: wordpress_db
wp_user_password: 12345
wp_installation_dir: '/var/www/wordpress'
wp_host_ip: '172.17.177.40'
wp_db_ip: '172.17.177.42'

O resultado foi este:

TASK [Cria usuario no MySQL] *************************************************************************************************************************************************
fatal: [172.17.177.42]: FAILED! => {"msg": "'wp_host_ip' is undefined"}

PLAY RECAP *******************************************************************************************************************************************************************
172.17.177.42              : ok=3    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0  

Aparentemente tem um erro de sintaxe. O qual não consigo entender pra ajustar.

Solucionado. Pessoal, pensando sobre ele não achar a variável definida, eu fiz uma cópia da pasta para a mesma onde estava exercício e ele leu corretamente as variáveis.

Sinceramente não sei se este era o motivo, a estrutura de diretórios que tinha criado antes.

Eu já tinha a idéia de recriar esta estrutura pra ficar mais organizada.

Resultado:

anderson@MacBook-Air-de-Anderson wordpress_com_ansible % ansible-playbook -i 746-aula1/hosts 746-aula6/provisioning.yml

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

TASK [Gathering Facts] ***********************************************************************************************************************************************************
ok: [172.17.177.42]

TASK [Instala pacotes de dependencia do sistema operacional] *********************************************************************************************************************
ok: [172.17.177.42]

TASK [Cria o banco no MySQL] *****************************************************************************************************************************************************
ok: [172.17.177.42]

TASK [Cria usuario no MySQL] *****************************************************************************************************************************************************
ok: [172.17.177.42] => (item=None)
ok: [172.17.177.42] => (item=None)
ok: [172.17.177.42] => (item=None)
ok: [172.17.177.42]

TASK [Configura MySQL para aceitar conexões remotas] *****************************************************************************************************************************
ok: [172.17.177.42]

PLAY [wordpress] *****************************************************************************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************************************************************
ok: [172.17.177.40]

TASK [Instala pacotes de dependencia do sistema operacional] *********************************************************************************************************************
ok: [172.17.177.40]

TASK [Baixa o arquivo de instalacao do Wordpress] ********************************************************************************************************************************
changed: [172.17.177.40]

TASK [Descompacta o wordpress] ***************************************************************************************************************************************************
ok: [172.17.177.40]

TASK [copy] **********************************************************************************************************************************************************************
changed: [172.17.177.40]

TASK [Configura o wp-config com as entradas do banco de dados] *******************************************************************************************************************
changed: [172.17.177.40] => (item={'regex': 'database_name_here', 'value': 'wordpress_db'})
changed: [172.17.177.40] => (item={'regex': 'username_here', 'value': 'wordpress_user'})
changed: [172.17.177.40] => (item={'regex': 'password_here', 'value': '12345'})
changed: [172.17.177.40] => (item={'regex': 'localhost', 'value': '172.17.177.42'})

TASK [Configura Apache para servir o Wordpress] **********************************************************************************************************************************
ok: [172.17.177.40]

PLAY RECAP ***********************************************************************************************************************************************************************
172.17.177.40              : ok=7    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
172.17.177.42              : ok=5    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

Que legal que conseguiu identificar o problema e mais legal ainda que compartilhou a solução aqui na comunidade, tenho certeza que vai ajudar muitas pessoas com essa sua solução. Continue assim e bons estudos!