MrDuk
MrDuk

Reputation: 18322

How can I structure my playbook to make more sense?

Currently, I have the following playbook:

- hosts: "{{env}}_{{product}}"
  name: "tools"
  sudo: yes
  vars_prompt:
    product: "Which product would you like to deploy [all|proxy|query|rest]?"
    env: "Which environment should we deploy to [dev|qa|test|prod]?"
  roles:
    - { role: proxy, when: "product == 'all' or product == 'proxy'" }
    - { role: query, when: "product == 'all' or product == 'query'" }
    - { role: rest, when: "product == 'all' or product == 'rest'" }

All of my groups are stored in a single ./inventory/all file, e.g.,:

# -----------------------------------------------------------------------------------
# --------------
# ###  DEV  ### 
# --------------
# -----------------------------------------------------------------------------------

[dev_all:children]
dev_redis
dev_query
dev_rest
dev_proxy

[dev_redis]
.hosts

[dev_query]
.hosts

[dev_rest]
.hosts

[dev_proxy:children]
dev_proxy-dc1
dev_proxy-dc2
dev_proxy-dc3

[dev_proxy-dc1]
.hosts

[dev_proxy-dc2]
.hosts

[dev_proxy-dc3]
.hosts


# -----------------------------------------------------------------------------------
# --------------
# ###  PROD  ### 
# --------------
# -----------------------------------------------------------------------------------

[prod_all:children]
prod_redis
prod_query
prod_rest
prod_proxy


[prod_redis]
.hosts

[prod_query]
.hosts

[prod_rest]
.hosts

[prod_proxy:children]
prod_proxy-dc1
prod_proxy-dc2
prod_proxy-dc3

[prod_proxy-dc1]
.hosts

[prod_proxy-dc2]
.hosts

[prod_proxy-dc3]
.hosts

I can't help but feel like I'm making this overly complex though. I'm trying to avoid requiring people to pass in tags or inventory files. But now I'm not sure what the best way to go about allowing deploys for things like redis hosts, which aren't really what we would consider a "product", but still requires it's own group since it's hosted on it's own set of hosts. I could just add it to the current list, [all|proxy|query|rest|redis], but... it seems like there should be a way to specify redis and another product, but at the same time not requiring both... I don't know how though.

I wanted to have something where you could say

"I want to deploy proxy to dev, and let's update redis while we're at it"

-- is this possible with my current setup?

Upvotes: 1

Views: 42

Answers (1)

ydaetskcoR
ydaetskcoR

Reputation: 56997

This doesn't feel like the best way to handle this.

Instead, I'd structure my playbooks individually to target specific roles and I'd equally set my inventories to only cover a certain environment rather than everything.

If you want to make it so that people don't have to pass in the inventory file or the specific playbook then I'd wrap the ansible-playbook command(s) in some form of wrapper script.

This allows people to use your playbooks a lot more granularly and flexible as you need it but still offer the same ability to offer presets through your wrapper script.

Upvotes: 1

Related Questions