cancel
Showing results for 
Search instead for 
Did you mean: 
spurs
Flight Engineer
Flight Engineer
  • 433 Views

group_vars vs roles/role-name/vars/

Jump to solution

Hi guys, in this lab. group_vars was used over roles/role-name/vars.

Q1. Is it because of to selection of the specific group? At first, I thought of moving the selinux.yml file to the roles/role-name/vars, but is it wrong? 

 

Q2. using group_vars and the target is web_developers. However, there are no vars_files: group_vars/web_developers.yml. Is it normally omitted?

 

The apache.developer_configs role will process a list of users defined in a variable named web_developers. The web_developers.yml file in the project directory defines the web_developers user list variable. Review this file and use it to define the web_developers variable for the development web server host group.

  1. Review the web_developers.yml file.

    ---
    web_developers:
      - username: jdoe
        name: John Doe
        user_port: 9081
      - username: jdoe2
        name: Jane Doe
        user_port: 9082

    A name, username, user_port is defined for each web developer.

  2. Place the web_developers.yml in the group_vars/dev_webserver subdirectory.

    [student@workstation role-review]$ mkdir -pv group_vars/dev_webserver mkdir: created directory 'group_vars' mkdir: created directory 'group_vars/dev_webserver' [student@workstation role-review]$ mv -v web_developers.yml \ > group_vars/dev_webserver/ renamed 'web_developers.yml' -> 'group_vars/dev_webserver/web_developers.yml'

    ---
    - name: Configure Dev Web Server
      hosts: dev_webserver
      force_handlers: yes
      roles:
        - apache.developer_configs
      pre_tasks:
Labels (1)
2 Solutions

Accepted Solutions
morbius
Flight Engineer
Flight Engineer
  • 362 Views

Not sure what confuses you about Q2. Looks to me like instructions state that web_developers.yml should be placed in group_vars/dev_webserver/

View solution in original post

0 Kudos
bonnevil
Starfighter Starfighter
Starfighter
  • 319 Views

For folks following along, I think we're talking about RH294 chapter 7 lab activity (ch07s11) "Lab: Simplifying Playbooks with Roles and Ansible Content Collections", step 7 of the solution.

So, substep 7.1 shows us that web_developers.yml contains definitions for a web_developers variable, which has as a value a list of dictionaries that contain user account information.  But the file is at the top level of the project directory (~/role-review/web_developers.yml) where nothing is going to read its variable in by default.

Substep 7.2 moves it to a more useful location: ~/role-review/group_vars/dev_webserver/web_developers.yml.  Because the playbook is ~/role-review/web_dev_server.yml, this is inside a playbook group_vars directory at least.  It might also be an inventory group_vars directory if the inventory file is in ~/role-review, but because ~/role-review/group_vars/dev_webserver/ is definitely a playbook group_vars directory, that doesn't matter.

Meanwhile, the play in the ~/role-review/web_dev_server.yml playbook uses the group dev_webserver in its hosts directive (we've been told that's a group in the inventory in the instructions to set the play up in step 3). 

When that playbook is run, Ansible is going to look for playbook group_vars variables in ~/role-review/group_vars/dev_webserver -- if that was a file, it'd read those variables in.  But it's not a file, it's a directory.  So it reads in all the YAML files contained in the ~/role-review/group_vars/dev_webserver/ directory: that'll read in ~/role-review/group_vars/dev_webserver/web_developers.yml at that point.

 

 

View solution in original post

6 Replies
morbius
Flight Engineer
Flight Engineer
  • 399 Views

It doesn't really matter where you place the variables, as long as yaml parser going through the playbook can find them, you're good. The only issue comes if they have the same name, then it depends what place has a priority, but if they all have different names, you don't have to worry about that.

spurs
Flight Engineer
Flight Engineer
  • 373 Views
Great! I'll keep that in mind when it has problem (same name). Could you answer Q2 as well?
0 Kudos
morbius
Flight Engineer
Flight Engineer
  • 363 Views

Not sure what confuses you about Q2. Looks to me like instructions state that web_developers.yml should be placed in group_vars/dev_webserver/

0 Kudos
spurs
Flight Engineer
Flight Engineer
  • 345 Views

How playbook read variables without mentioning vars_files:? 

If it was the var file under roles/, i get it, but this case is group_vars, so i don't get it

0 Kudos
bonnevil
Starfighter Starfighter
Starfighter
  • 320 Views

For folks following along, I think we're talking about RH294 chapter 7 lab activity (ch07s11) "Lab: Simplifying Playbooks with Roles and Ansible Content Collections", step 7 of the solution.

So, substep 7.1 shows us that web_developers.yml contains definitions for a web_developers variable, which has as a value a list of dictionaries that contain user account information.  But the file is at the top level of the project directory (~/role-review/web_developers.yml) where nothing is going to read its variable in by default.

Substep 7.2 moves it to a more useful location: ~/role-review/group_vars/dev_webserver/web_developers.yml.  Because the playbook is ~/role-review/web_dev_server.yml, this is inside a playbook group_vars directory at least.  It might also be an inventory group_vars directory if the inventory file is in ~/role-review, but because ~/role-review/group_vars/dev_webserver/ is definitely a playbook group_vars directory, that doesn't matter.

Meanwhile, the play in the ~/role-review/web_dev_server.yml playbook uses the group dev_webserver in its hosts directive (we've been told that's a group in the inventory in the instructions to set the play up in step 3). 

When that playbook is run, Ansible is going to look for playbook group_vars variables in ~/role-review/group_vars/dev_webserver -- if that was a file, it'd read those variables in.  But it's not a file, it's a directory.  So it reads in all the YAML files contained in the ~/role-review/group_vars/dev_webserver/ directory: that'll read in ~/role-review/group_vars/dev_webserver/web_developers.yml at that point.

 

 

bonnevil
Starfighter Starfighter
Starfighter
  • 312 Views

I should add that it doesn't matter that dev_webserver is in the hosts directive of the play, but it does ensure that there are hosts in the play that will be affected by settings for the group dev_webserver.  All of the playbook group_vars are going to be read in and applied to hosts that are in those groups, I believe.

0 Kudos
Join the discussion
You must log in to join this conversation.