ansible / proposals

Repository for sharing and tracking progress on enhancement proposals for Ansible.
Creative Commons Zero v1.0 Universal
92 stars 19 forks source link

active group #162

Closed pijpe00 closed 5 years ago

pijpe00 commented 5 years ago

Proposal: active group of the play

Author: Edwin

Date: 2018-04-09

Motivation

I want to know which group the playbook is running (ansible_play_group).

Problems

What problems exist that this proposal will solve?

Solution proposal

- name: some play
    hosts: France

    roles:
    - webserver
- name: include webserver info certain location
    include_vars:
    dir: '../defaults/{{ ansible_play_group }}'
    name: webinfo

directory structure: . roles roles/webserver roles/webserver/defaults roles/webserver/defaults/France roles/webserver/defaults/France/info1.yml roles/webserver/defaults/France/info2.yml roles/webserver/defaults/Greece roles/webserver/defaults/Greece/info1.yml roles/webserver/defaults/Greece/info2.yml

agaffney commented 5 years ago

There is no such thing as an "active group". When you use something like hosts: some_group (or even hosts: some_group:another_group or hosts: some_host) in your play, that is flattened into a list of hosts and the original host pattern (which may not have even contained a group) is "lost".

There's already a variable called group_names, which contains a list of all inventory groups that the current host is a member of, regardless of how that host was selected. You can use that variable similarly to your proposed ansible_play_group variable.

As a side note, it's "wrong" to use include_vars on a file in defaults/, even though it technically works. The vars file will be treated like any other vars file, rather than having the normal precedence used for role defaults.

pijpe00 commented 5 years ago

Hello @agaffney ,

I want to use seperate yaml files to organise my variables. Is there a way I can include variables from the group_vars/ file?

What I want to do is: the variable 'app' consistes of the information "

/*.yml" The is different for different groups.

bcoca commented 5 years ago

As explained above, there is no such thing as 'active group', consider the following hosts expression:

hosts: vmhosts:dc1:!webservers

In which plenty of servers are both in vmhosts and dc1 groups, which one would you consider "active"?

This proposal is based on several incorrect assumptions:

As such this is not a possible proposal w/o changing the fundamental way Ansible works (centered on Host and Task).

bcoca commented 5 years ago

@pijpe00

The group_vars are ALWAYS read for ALL the groups your host is part of, doing an include of specific group_vars is redundant and shows incorrect assumptions in your variable organization.

Do not use the same variable across groups that needs different values depending on group targeted, Ansible does not work that way (we know other tools do, its a valid approach, but not the one we take).

pijpe00 commented 5 years ago

Hi Andrew (@agaffney) , Brian (@bcoca), The problem I have is: I made yaml playbooks I want to use for

development, test, production I get information from several teams for the each desig phase. for example: development --> team1 --> several variables like database locations, datasources etc development --> team2 --> several variables like database locations, datasources etc test --> team1 --> several variables like database locations, datasources etc test --> team2 --> several variables like database locations, datasources etc production --> team1 --> several variables like database locations, datasources etc production --> team2 --> several variables like database locations, datasources etc

I want to have the variables from the teams in seperate yaml files To use this variables I use the variable: 'app' like: apps: team1: var1: xxx var2: yyy

   varn: zzz

team2: var1: axx var2: ayy

   varn: azz

Depends on what stage I am the variables has other values What is the right direction to implement this without?

agaffney commented 5 years ago

This is not a forum for user support. You can come over to the #ansible channel on Freenode or post to the ansible-project mailing list.