How Do I Conditionally Import a Playbook?

15 views
Skip to first unread message

Chrono 7

unread,
Aug 25, 2019, 12:12:57 PM8/25/19
to Ansible Project
I'd like to know how to conditionally import a playbook.

I have a master playbook which calls several others

The master looks like this:


---

- name: Query user for information
  import_playbook
: query_user.yaml

- name: Car required
  import_playbook
: determine_car_information.yaml
 
when: car_required == True


query_user.yaml looks like this


---

- hosts: 127.0.0.1
  connection
: local

  vars_prompt
:
 
- name: car_required
    prompt
: 'Is a car required (y / n)?'
   
private: no

  tasks
:
   
- name: Register variable from above prompt
      set_fact
:
        car_required
: "{{ car_required }}"


No matter what, the determine_car_information playbook is always run, regardless of what car_required is set to.

I've also tried wrapping this in a tasks / block section as per the following:




---

- name: Query user for information
  import_playbook
: query_user.yaml

- tasks:

 
- name: Gather car info if car is required
  block
:
 
- name: Car required, query for info
  import_playbook
: determine_car_information.yaml
 
when: car_required|lower == "y"




This results in the following:


ERROR! this task 'import_playbook' has extra params, which is only allowed in the following modules: command, win_command, shell, win_shell, script, include, include_vars,
include_tasks
, include_role, import_tasks, import_role, add_host, group_by, set_fact, raw, meta

I'd really like to understand why this is occurring / if there's an alternate solution.

Vladimir Botka

unread,
Aug 25, 2019, 1:43:01 PM8/25/19
to Chrono 7, ansible...@googlegroups.com
On Sun, 25 Aug 2019 09:12:57 -0700 (PDT)
Chrono 7 <chro...@gmail.com> wrote:

> I'd like to know how to conditionally import a playbook.
> [...]
> - name: Car required
> import_playbook: determine_car_information.yaml
> when: car_required == True

It's not possible. "import_playbook" is not a task. Conditions don't apply
here. It's an open issue
Conditionally import_playbook aka include_playbook #34281
https://github.com/ansible/ansible/issues/34281

Cheers,

-vlado

Chrono 7

unread,
Aug 25, 2019, 9:46:28 PM8/25/19
to Ansible Project
Thanks! I figured it was a bit of a limitation at this point. Would be great to have this added at some point
Reply all
Reply to author
Forward
0 new messages