run ansible role with specific playbook

857 Views Asked by At

I have one ansible role with this structure:

── prepare-workstation.yaml
── group_vars
└── roles
    ├── build-nginx-proxy
    │   ├── defaults
    │   ├── tasks
    │   └── templates
    │   └── nginx.yml
    │
    ├── ca
    │   ├── defaults
    │   ├── handlers
    │   ├── tasks
    │   └── templates

and prepare-workstation.yaml content is:

---
- name: Prepare workstation
  hosts:
    - workstation
  roles:
    - { role: ca}                    
    - { role: build-nginx-proxy }    

I want build-nginx-proxy role run with nginx.yml not tasks/main.yml.

how can I pass index.yml to build-nginx-proxy role?

something like this:

---
- name: Prepare workstation
  hosts:
    - workstation
  roles:
    - { role: ca}                    
    - { role: build-nginx-proxy, 'nginx.yml' }    
2

There are 2 best solutions below

0
On

Try this

#if nignx.yml is placed in build-nginx-proxy/tasks/nignx.yml
- name: Run tasks/nignx.yaml instead of 'build-nginx-proxy'
  include_role:
    name: build-nginx-proxy
    tasks_from: nignx.yml

or try include_tasks

- name: Include tasks from /roles/build-nginx-proxy/tasks/nignx.yml
  include_tasks: /roles/build-nginx-proxy/tasks/nignx.yml

here is the roles documentation

0
On

if you want something parametric, use a local var:

  roles:
    - role: ca                    
    - role: build-nginx-proxy
      vars:
        mod: ../nginx.yml    

and in main.yml, you do

- include_tasks: "{{ mod }}"

its better to put the task nginx.yml in folder tasks, no need to add ../