How to set an Ansible role's variables file relative to the host?

11,893

Solution 1

Malo,

You should use "host_vars" and not hosts_vars

 /host_vars/play1/mongodb.yml

Also, play1 should match the name of the host that you have configured in your hosts inventory.

Solution 2

According to http://docs.ansible.com/playbooks_best_practices.html#directory-layout the recommended layout is as follows:

production                # inventory file for production servers
stage                     # inventory file for stage environment

group_vars/
   group1                 # here we assign variables to particular groups
   group2                 # ""
host_vars/
   hostname1              # if systems need specific variables, put them here
   hostname2              # ""

library/                  # if any custom modules, put them here (optional)
filter_plugins/           # if any custom filter plugins, put them here (optional)

site.yml                  # master playbook
webservers.yml            # playbook for webserver tier
dbservers.yml             # playbook for dbserver tier

roles/
    common/               # this hierarchy represents a "role"
        tasks/            #
            main.yml      #  <-- tasks file can include smaller files if warranted
        handlers/         #
            main.yml      #  <-- handlers file
        templates/        #  <-- files for use with the template resource
            ntp.conf.j2   #  <------- templates end in .j2
        files/            #
            bar.txt       #  <-- files for use with the copy resource
            foo.sh        #  <-- script files for use with the script resource
        vars/             #
            main.yml      #  <-- variables associated with this role
        defaults/         #
            main.yml      #  <-- default lower priority variables for this role
        meta/             #
            main.yml      #  <-- role dependencies

    webtier/              # same kind of structure as "common" was above, done for the webtier role
    monitoring/           # ""
    fooapp/               # ""

Notice the host_vars/ directory. There you can have include host-specific variables that your role can use later on.

Share:
11,893
Admin
Author by

Admin

Updated on June 15, 2022

Comments

  • Admin
    Admin almost 2 years

    Here is the detail of my playbook:

    Playbook tree

    ├─ devops
    |  ├─ roles
    |  |  ├─ mongodb
    |  |  ├─ haproxy
    |  |  ├─ monit
    |  |  |  ├─ vars
    |  |  |  |  └─ main.yml
    |  |  |  └─ ...
    |  |  └─ ...
    |  ├─ hosts
    |  ├─ play1.yml
    |  └─ play2.yml
    

    hosts

    [play1]
    ...instructions...
    
    [play2]
    ...instructions...
    

    play1.yml

    ---
    - hosts: play1
      user: root
      roles:
        - haproxy
        - monit
    

    play2.yml

    ---
    - hosts: play2
      user: root
      roles:
        - mongodb
        - monit
    

    Question

    I would like to use a different variables file for monit depending on the host (play1.yml or play2.yml). How I can do the trick?

    Many thanks