How to conditionally include other playbooks?

54 views
Skip to first unread message

Tristan Muntsinger

unread,
Sep 21, 2016, 6:23:42 PM9/21/16
to Ansible Project
I tried this and it isn't working as expected:

test.yml
---
- include: 1.yml
 
when: type == '1'
- include: 2.yml
 
when: type == '2'


1.yml
---
- name: "1"
  hosts
: localhost
  tasks
:
   
- debug: msg="1"


2.yml
---
- name: "2"
  hosts
: localhost
  tasks
:
   
- debug: msg="2"

Command:
ansible-playbook test.yml -e "type=1"

Output:
PLAY [1] **********************************************************************


GATHERING FACTS
***************************************************************
ok
: [localhost]


TASK
: [debug msg="1"] *********************************************************
ok
: [localhost] => {
   
"msg": "1"
}


PLAY
[2] **********************************************************************


GATHERING FACTS
***************************************************************
ok
: [localhost]


TASK
: [debug msg="2"] *********************************************************
ok
: [localhost] => {
   
"msg": "2"
}


PLAY RECAP
********************************************************************
localhost                  
: ok=4    changed=0    unreachable=0    failed=0  

-Tristan

Brian Coca

unread,
Sep 21, 2016, 8:08:35 PM9/21/16
to ansible...@googlegroups.com
playbooks cannot be included conditionally.


----------
Brian Coca

Tristan Muntsinger

unread,
Sep 22, 2016, 12:36:57 PM9/22/16
to Ansible Project
Is there an alternative solution to get the behavior I'd like?

-Tristan

trey perrone

unread,
Sep 23, 2016, 9:05:24 AM9/23/16
to Ansible Project
I've done includes for playbooks based on facts typically OS

- name: include OS specific tasks
  include: "{{ ansible_os_family|lower }}.yml" 

if there was a specifc fact you could use such OS, RAM, CPU, write a custom module to make that fact. I guess at that point why not target machines based on inventory groups. 

-tp

Brian Coca

unread,
Sep 23, 2016, 10:13:44 AM9/23/16
to ansible...@googlegroups.com
That will work with task includes, not play includes.

 
----------
Brian Coca
Reply all
Reply to author
Forward
0 new messages