Using variables on inaccessible hosts targeted at the playbook

I am trying to write a very flexible playbook that targets nodes based on the environment in which they are located. I use as many variables as possible, so the playbook can be reused for other projects / environments with minimal changes.

I have one application.yml

---
- name: Prepare app-server for "The app"
  hosts: "{{'env'}}_super_app"
  vars:
  vars_files:
  - "environments/{{env}}.yml"
  sudo: yes
  tasks:
    - command: echo {{env}}
  roles:
    - common
    - nginx
    - php5-fpm
    - nodejs
    - newrelic
    - users
    - composer

- name: Install and configure mysql for "The super app"
  hosts:
  - "{{env}}_super_db"
  vars:
  vars_files:
  - "environments/{{env}}.yml"
  sudo: yes
  roles:
    - common
    - mysql
    - newrelic

Here is the directory structure for playback:

β”œβ”€β”€ environments
β”‚   β”œβ”€β”€ prod.yml       << environment specific vars
β”‚   β”œβ”€β”€ stag.yml       << environment specific vars
β”‚   └── uat.yml        << environment specific vars
β”œβ”€β”€ roles
β”‚   β”œβ”€β”€ common
β”‚   β”œβ”€β”€ composer
β”‚   β”œβ”€β”€ mysql
β”‚   β”œβ”€β”€ newrelic
β”‚   β”œβ”€β”€ nginx
β”‚   β”œβ”€β”€ nodejs
β”‚   β”œβ”€β”€ php5-fpm
β”‚   └── users
β”œβ”€β”€ users
β”‚   └── testo.yml
β”œβ”€β”€ prod              << inventory file for production
β”œβ”€β”€ README.md
β”œβ”€β”€ application.yml   << application playbook
β”œβ”€β”€ stag              << inventory file for staging
β”œβ”€β”€ uat               << inventory file for uat

Here is the contents of the uat inventory file:

[uat_super_app]
10.10.10.4
[uat_super_db]
10.10.10.5

When I run my book, I pass the environment as an additional variable:

ansible-playbook -K -i uat application.yml -e="env=uat" --check

Idea: If {{env}} is installed in uat, then /uat.yml environments will be used, and the hosts [uat_super_app] will target {{env}} _ super_app.

If I or someone makes a mistake and tries to run uat vars against production inventory, the hosts will not match and it will be impossible to start the playbook.

ansible-playbook -K -i prod application.yml -e="env=uat" --check

, . , :

ansible-playbook -K -i uat application.yml -e="env=uat" --check -vvvv

SUDO password: 
PLAY [Prepare app-server for "The app"] ******************************* 
skipping: no hosts matched

PLAY [Install and configure mysql for "The app"] ********************** 
skipping: no hosts matched

PLAY RECAP ******************************************************************** 
+4
2
hosts: "{{'env'}}_super_app"

, env, , env_super_app. , :

hosts: "{{ env }}_super_app"
+2

udondan, .

, vars , , .

env playbook, :

---
- name: Prepare app-server for "The app"
  hosts: uat_super_app
  vars:
    - env: uat

, :

ansible-playbook -K -i uat application.yml -e='vars: env=uat'

, -list-hosts:

ansible-playbook -K -i uat application.yml -e='vars: env=uat' --list-hosts

playbook: application.yml

  play #1 (Prepare app-server for "The super app"): host count=1
    10.10.10.4

  play #2 (Install and configure mysql for "The super app"): host count=1
    10.10.10.5
+1

All Articles