Skip to content

Instantly share code, notes, and snippets.

@odyssey4me
Last active October 24, 2016 10:35
Show Gist options
  • Save odyssey4me/17c2da743454549f9be8dac03489de39 to your computer and use it in GitHub Desktop.
Save odyssey4me/17c2da743454549f9be8dac03489de39 to your computer and use it in GitHub Desktop.
Ansible 2.1.1 experimentation with conditional include handling
#!/bin/bash
apt-get update && \
apt-get purge -y nano && \
apt-get install -y git vim tmux fail2ban build-essential python2.7 python-dev libssl-dev libffi-dev
curl --silent --show-error --retry 5 https://bootstrap.pypa.io/get-pip.py | sudo python2.7
pip install -U ansible==2.1.1
ansible-playbook -i 01-inventory.ini 02-playbook.yml -vv
[all]
localhost1 ansible_connection=local
localhost2 ansible_connection=local
localhost3 ansible_connection=local
localhost4 ansible_connection=local
localhost5 ansible_connection=local
[group1]
localhost1
localhost2
localhost3
localhost4
localhost5
---
- name: Test conditional include (using inventory/group)
hosts: group1
gather_facts: false
tasks:
- include: 03-included-task.yml
static: no
when: inventory_hostname == groups['group1'][0]
vars:
include_me: "{{ inventory_hostname == groups['group1'][0] }}"
- name: Test conditional include (using vars)
hosts: group1
gather_facts: false
tasks:
- include: 03-included-task.yml
static: no
when: include_me | bool
vars:
include_me: "{{ inventory_hostname == groups['group1'][0] }}"
---
- debug:
msg: "included-task -> include_me: {{ include_me }}"
when: include_me is defined
- debug:
msg: "included-task -> arbitrary message"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment