So, my playbook works fine when using this syntax:
---
- hosts: all
become: true
become_user: root
gather_facts: false
connection: local
vars_files:
- roles/docker_setup/vars/docker_vars.yml
- roles/docker_setup/vars/aws_cred.yml
vars:
instance_ids:
- "i-xxxxxxxxxxx"
tasks:
- include_role:
name: docker_setup
tasks_from: docker_tasks.yml
But I read that you can use this syntax which looks much easier to handle once I want to add new roles to the playbook:
---
- hosts: all
become: true
become_user: root
gather_facts: false
connection: local
vars_files:
- roles/docker_setup/vars/docker_vars.yml
- roles/docker_setup/vars/aws_cred.yml
vars:
instance_ids:
- "i-xxxxxxxxxxx"
roles:
- docker_setup
The difference is:
roles:
- docker_setup
instead of
tasks:
- include_role:
name: docker_setup
tasks_from: docker_tasks.yml
But once I try to run the playbook nothing happens:
sudo ansible-playbook docker_setup.yml --ask-vault-pass -i hosts --user devops --key-file /home/devops/.ssh/id_rsa
Vault password:
PLAY [all] *************************************************************************************************************************************************
PLAY RECAP *************************************************************************************************************************************************
Why is it that? Here is my tree of roles:
.
├── docker_setup
│ ├── handlers
│ │ └── main.yml
│ ├── tasks
│ │ └── docker_tasks.yml
│ └── vars
│ ├── aws_cred.yml
│ └── docker_vars.yml
└── user_host_create
├── handlers
│ └── user_handlers.yml
├── tasks
│ └── user_tasks.yml
└── vars
├── user_password_vault.txt
├── user_password.yml
└── user_vars.yml
and hosts file:
[webservers]
172.31.31.223
Thank you!
The difference between the two options below is what file with the tasks is executed.
tasks:
- include_role:
name: docker_setup
tasks_from: docker_tasks.yml
roles:
- docker_setup
In the first case, it is explicitly roles/docker_setup/tasks/docker_tasks.yml (see tasks_from).
In the second case, it is roles/docker_setup/tasks/main.yml (see Role directory structure). This file is missing in the role. As a result, nothing is executed. You can fix it and create the file e.g.
shell> cat roles/docker_setup/tasks/main.yml
- ansible.builtin.import_tasks: docker_tasks.yml
This would make the two options equivalent as to what tasks are executed. However, there will be other differences e.g. the scope of the variables, or inheritance of the tags. For details see Using Roles.