Can't get the ability to recognize group variables

I'm trying to set up environment variables for specific ones (e.g. production, staging, development). For some reason, it is not possible to exclude variables in group_vars / [environment].

I am using unsible 1.9.1

Here is a stripped down example of what I am trying to do.

Directory structure:

.
├── group_vars
│   └── staging
├── hosts
│   └── staging
└── site.yml

      

group_vars / staging:

test_var: "this is a test"

      

site.yml:

---
- hosts: localhost
  tasks:
  - debug: msg="test variable = {{ test_var }}"

      

hosts / staging - empty file

Launch output ansible-playbook -i hosts/staging site.yml

:

PLAY [localhost] **************************************************************

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

TASK: [debug msg="test variable = {{ test_var }}"] ****************************
fatal: [localhost] => One or more undefined variables: 'test_var' is undefined

FATAL: all hosts have already failed -- aborting

PLAY RECAP ********************************************************************
           to retry, use: --limit @/Users/jcowley/site.retry

localhost                  : ok=1    changed=0    unreachable=1    failed=0

      

If I move group_vars / staging to group_vars / all it works as expected and outputs the value of test_var. But I am trying to figure out how I can separate environments into documentation in Ansible Best Practices

+3


source to share


1 answer


EDIT:

For a more accurate answer to your question, check out this github project . This, I think, is what you were trying to do. I may be wrong, but I think the problem is coming from your inventory file. You actually named your files group_vars

( staging

) after your inventory name ( staging

). But you have to name it after the section inside your inventory file that I assume is localhost

looking at your workbook.

So this is what you need:

hosts / staging:

[staging]
X.X.X.X

      

Here is a more viable solution for organizing your project in my opinion. It is based on roles .

Directory structure:

.
├── group_vars
│   └── all
├── hosts
│   └── local
│   └── staging
│   └── prod
├── roles
│   └── exemple
│       └── tasks
│       └── vars
│           └── local.yml
│           └── staging.yml
│           └── prod.yml
└── site.yml

      



group_vars/all

can have env variable:

# The application environment
# Possible values are : prod, staging or local
env: local

# Other global variables
...

      

Your inventory file:

[local]
X.X.X.X

[staging]
X.X.X.X

[prod]
X.X.X.X

      

Then your playbook sites.yml

might look like this:

---
- name: Server(s) configuration
  hosts: "{{env}}"
  roles:
    - exemple
  vars_files:
    - "roles/example/vars/{{env}}.yml"

      

Doing it this way gives you several advantages:

  • you can reuse the env variable anywhere in your project, in jinja templates, or as a condition in very practical tasks;
  • your project is split into separate roles. Clearing it this way for a large project (you can have apache role, ssh role, etc.);
  • you can create env variables in separate files in the directory roles/exemple/vars/

    .
+3


source







All Articles