I have a simple play
---
- name: nfs.yml
hosts: nfs
become: yes
roles:
- { role: common, tags: ["common"] }
- { role: geerlingguy.nfs, tags: ["nfs"] }
This has always worked for me. Now I am trying to help a user for whom this doesn't work.
To get it to work the user has to rename the role to remove the dot for example to
---
- name: nfs.yml
hosts: nfs
become: yes
roles:
- { role: common, tags: ["common"] }
- { role: geerlingguy_nfs, tags: ["nfs"] }
Ansible seems to skip those type of role without error. What is the rationale for this behavior? How can it be configured? This seems to be a undocumented feature.
As per ansible link https://galaxy.ansible.com/docs/contributing/creating_role.html
The charaters - and . will be converted to "_"
Related
I have a playbook which is basically comprised of roles:
- hosts: master
gather_facts: True
environment:
http_proxy: "{{ lookup('env','http_proxy') }}"
https_proxy: "{{ lookup('env','https_proxy') }}"
roles:
- { role: watchdog, status: 'disabled' }
- { role: use-my-version }
- { role: services-action, action: 'stopped', acme_services: "{{stop_services_before_install}}", become: yes }
... more roles being called ...
- { role: watchdog, status: 'enabled' }
Now, it's possible that one of the roles will fail, which means we won't get to the watchdog-enabled call. I want the watchdog to be enabled even if the playbook fails at any point. I saw rescue, but that's for a block, but that is only for tasks. How would you suggest going about doing this?
One option is to add to the role that you are interested to be executed a tag:
{ role: watchdog, status: 'enabled', tags: 'enable_watchdog' }
and execute again the playbook with that tag if the first execution failed (to the execution add -t enable_watchdog. Avoid to overwrite the logs of the execution of the playbooks, so you'll have a way to troubleshoot the original issue.
I want to set gather_facts to false but then use the setup module to gather facts and run the roles afterwards.
My code looks like:
---
- name: RDS check
hosts: "{{ run_on_node|default('cdh[0]')}}"
gather_facts: False
setup: #not sure about the indentation, but want to execute that before roles.
roles:
- { role: r1, when: "'10.200.1.197' in inventory_hostname" }
setup module has to be executed in a task section. pre_tasks are executed before roles.
Try as below:
---
- name: RDS check
hosts: "{{ run_on_node|default('cdh[0]')}}"
gather_facts: False
pre_tasks:
- name: Gather facts
setup:
roles:
- { role: r1, when: "'10.200.1.197' in inventory_hostname" }
I have been calling a playbook having multiple roles, each roles signifies a TESTCASE. I need to run the playbook without failing it if one of the role fails during execution. For which I am using ignore_errors: yes. However, this truly ignores error, I need to print at the end the name of the ROLES which are failed ? is it possible ?
- hosts: WEB
gather_facts: no
vars:
roles:
- { role: CHECK_CONNECTION, ignore_errors: yes, tags: always }
- { role: CHECK_CPU, ignore_errors: yes, tags: always }
- { role: CHECK_MEM, ignore_errors: yes, tags: always }
Question: How to execute whole playbook and at the end print the failed roles ?
An option would be to use the rescue section of Blocks
Create include_role.yml
- name: include role
block:
- include_role:
name: "{{ item_my_role }}"
rescue:
- set_fact:
failed_roles: "{{ failed_roles|default([]) + [ item_my_role ] }}"
and loop include_tasks. It is not possible to loop blocks.
vars:
my_roles:
- CHECK_CONNECTION
- CHECK_CPU
- CHECK_MEM
tasks:
- include_tasks: include_role.yml
loop: "{{ my_roles }}"
loop_control:
loop_var: item_my_role
- debug:
var: failed_roles|default([])
Use loop_control loop_var and create unique variable, e.g. item_my_role, avoiding potential conflict inside the included role. If the variable is used inside the included role the following rescue section will add the wrong item to the list.
I am using the following site.yml playbook and calling it via
ansible-playbook site.yml
- hosts: some_hosts
vars:
pip_install_packages:
- name: docker
- tasks:
- name: Conditionally include bar vars
include_vars:
file: bar_vars.yml
when: some_condition == "bar"
- name: Conditionally include foo vars
include_vars:
file: foo_vars.yml
when: some_condition == "foo"
roles:
- role1
- role2
environment:
SOME_ENV_VAR: "{{ vault_some_env_var }}"
Call is failing as follows:
ERROR! the field 'hosts' is required but was not set
But as is apparent above, the hosts field has been set!
Any suggestions?
You can mix tasks and roles in a playbook, you can also control when the tasks execute by using "pre_tasks" and "post_tasks".
It looks to me like you have a - on tasks that should not be there, probably considering it to be a new play.
- hosts: some_hosts
vars:
pip_install_packages:
- name: docker
- tasks: <-- This should not have a dash
Example using pre and post tasks to control when tasks execute in relation to a role:
---
- hosts: all
name: Roles with pre and post tasks
vars:
somevar: foobar
roles:
- { role: common, tags: ["common"] }
pre_tasks:
- debug:
msg: I execute before roles
post_tasks:
- debug:
msg: I execute after roles
I have a playbook with multiple plays:
---
- hosts: druid-realtime-1
sudo: true
roles:
- { role: druid-realtime, du_rt_id: 1 }
- hosts: druid-realtime-2
sudo: true
roles:
- { role: druid-realtime, du_rt_id: 2 }
How do I tell ansible to run both plays in parallel instead of one after another?
You could do it this way
In your Ansible inventory, group your servers and assign a host variable:
[druid-realtime]
druid-realtime-1 id=1
druid-realtime-2 id=2
Then reference the variable in the playbook:
- hosts: druid-realtime
sudo: true
roles:
- { role: druid-realtime, du_rt_id: {{ id }} }
Not sure if this was possible when this post was created but I believe this is what you were looking for:
http://docs.ansible.com/ansible/playbooks_async.html
This allows you to stop the task from blocking, ie. waiting for the task to complete before progressing onto the next task.
To run multiple playbooks in parallel I wrote ansible-parallel today.
So for your question, split the plays in different yml files and run:
pip install ansible-parallel
ansible-parallel *.yml
If your plays are isolated, you can split your playbook : 1 play -> 1 playbook. For example:
druid-realtime-1.yml:
- hosts: druid-realtime-1
sudo: true
roles:
- { role: druid-realtime, du_rt_id: 1 }
druid-realtime-2.yml:
- hosts: druid-realtime-2
sudo: true
roles:
- { role: druid-realtime, du_rt_id: 2 }
Keep a main playbook site.yml that includes other playbooks:
- include: druid-realtime-1.yml
- include: druid-realtime-2.yml
With this approach, you can use a terminal for each playbooks and continue to use your main playbook.