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.
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 setup.yml with multiple roles:
setup.yml:
- hosts: localhost
roles:
- { role: file-download, tags: files }
- { role: setup-nginx, tags: nginx}
- { role: restart-vm, tags: restartvm }
- { role: file-upload, tags: upload}
- { role: intall-vm, tags: installvm}
- { role: create-backup, tags: backup}
From command line I can run:
ansible-playbook -i inventory setup.yml --tags=nginx
ansible-playbook -i inventory setup.yml --tags=restartvm
How can I import_playbooks to new.yml playbook and run only roles which have tag nginx and restartvm?
new.yml:
- import_playbook: setup.yml --tags=nginx
- import_playbook:setup.yml --tags=restartvm
I get following error:
ERROR! Invalid variable name in vars specified for PlaybookInclude: '--tags' is not a valid variable name
Thanks for help
This will not work. You can feed import_playbook only with a yaml file name that contains a playbook.
From the doc:
The name of the imported playbook is specified directly without any other option.
But if in your new.yml, you simply have import_playbook: setup.yml and then you specify the tag as ansible-playbook option, it will do the work:
ansible-playbook -i inventory new.yml --tags=my_tag
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 "_"
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