Different yml tasks files within a role

Hi,

I’m not sure if some else has already asked it because it is quiet necessary but I did not find that thread at least.

The point is have a mysql role, but I need to separate task in different files, currently I have a main.yml file which do all common tasks, but I need to do different things for master and slaves:

setup.yml

  • hosts: databases
    roles:
  • { role: mysql, mysql_root_password: XXXXXX, tags: mysql }

but now I would need do something like:

  • hosts: master
  • include: roles/mysql/tasks/master.yml

I appreciate your time and your tips.

Thanks !

I would create 3 roles

mysql-common
mysql-master
mysql-slave

you can make both master & slave depend on common

Thanks for your answer Brian.

But there is no way to invoke the yml file if it is in the same role? Something like this? (Obviously the code below is not working)

  • hosts: databases
    roles:
  • { role: mysql/main.yml, mysql_root_password: XXXX, tags: mysql }
  • { role: mysql/master.yml, tags: mysql }

I prefer to put this yml file on the same role because I think it is more tidy

Thanks.

‘Include’ can be used no matter where the file is, but it won’t do role magic.

Brian Coca

“But there is no way to invoke the yml file if it is in the same role? Something like this? (Obviously the code below is not working)”

There’s no reason why it shouldn’t, role paths can be relative too.

Thanks Michael for your answer,

I just double check it again an I can confirm it does not work:

#ls /etc/ansible/roles/mysql/tasks/
main.yml master.yml

setup.yml

I’d like to see a github ticket and we can attempt to reproduce it from there.

Thanks Michael,

If anyone has the same problem, the issue is tracked here: https://github.com/ansible/ansible/issues/4876