ansibleansible-role

How to run an Ansible role for different hosts and parameters


I have an Ansible role to install Nginx config files, and I want to use it to install different files on different sets of hosts. Here's the playbook:

- name: Deploy centralised Nginx services
  hosts: central_web_servers
  roles:
    - role: nginx_setup
      web_services: "{{ central_web_services }}"

- name: Deploy per-site Nginx services
  hosts: common_web_servers
  roles:
    - role: nginx_setup
      web_services: "{{ common_web_services }}"

In accordance with the documentation I've declared web_services to be a parameter in meta/argument_specs.yml:

argument_specs:
  main:
    short_description: Main entry point for the nginx role
    description:
      - Install configuration files for Nginx web services,
        as given by the "web_services" parameter.
    author:
      - Peter Westlake
    options:
      web_services:
        type: "list"
        elements: "str"
        required: true
        description: "The services to run on the given hosts."

But it only runs the first play.

I tried adding meta/main.yml with allow_duplicates: true but that didn't help either.

Putting both role calls into one play won't work because of the different hosts.


Solution

  • I mocked a role and run the playbook, both roles tasks run successfully as they should. If second role doesn't run in your environment I bet it's because first one failed at some point. So playbook is correct, something wrong with the role.