1
votes

I've got a problem with Ansible 2.2 in Ubuntu.

My playbook contains the following:

---
- hosts: all
  become: yes
  roles:
    - mysql

I've used the MySQL role from here https://galaxy.ansible.com/geerlingguy/ but renamed all to MySQL locally. In the defaults folder of the playbook is a file which contains the parameter:

mysql_datadir: /var/lib/mysql

With that, Ansible is working fine. But now I want a specific configuration per host as explained here: http://docs.ansible.com/ansible/playbooks_best_practices.html

So I created the file inventories/host_vars/test.yml, removed the one in the defaults folder of the task and updated the playbook to the following:

  ---
- hosts: all
  become: yes
  roles:
    - mysql
  tasks:
  - name: include vars
    include_vars: inventories/host_vars/test.yml

Now Ansible won't accept that; it says that the variable is not set:

fatal: []: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'mysql_datadir' is undefined\n\nThe error appears to have been in '/home/michael/Ansible/Playbooks/roles/mysql/tasks/setup-Debian.yml': line 24, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Delete innodb log files created by apt package after initial install.\n ^ here\n"}

Passing the argument via command line works without problems. How can I fix that?

1
Roles get executed before tasks. But as already mentioned, host_vars work automaticly if used correctly.dgw

1 Answers

4
votes

I believe you have multiple issues there. I would recommend starting with the standard, simpler directory structure.

I would try to do the following:

  • Use the standard directory layout, so move host_vars to the top folder
  • Inside the host_vars directory, put one file for each host, named exactly after the host name defined in your inventory, start the file with "---" and put variables in there. DO NOT add the .yml extension to the file!
  • Do not explicitly "include" anything. It is automatic. No "tasks" in site.yml.