skip to Main Content

I’m writing an Ansible role that is supposed to run on Debian and RedHat.

In my module, the default values for Red Hat and Debian are different, so I thought I could just create Debian.yml and RedHat.yml in the default folder, but this did not work out of the box.

Where can I set the appropriate values so this works just like for vars?

I tried it in the same way with:

- name: Include distribution specific variables
  include_vars: "{{ ansible_os_family }}.yml"
  tags: module

2

Answers


  1. By default, the main.yml files are loaded automatically, files Debian.yml and RedHat.yml in defaults directory aren’t loaded automatically. You could load them with task "Include distribution specific variable" that you wrote, this task must be the first task in main.yml file in your role as below

    - name: Include distribution specific variables
      include_vars: "defaults/{{ ansible_os_family }}.yml"
      tags: module
    
    Login or Signup to reply.
  2. Unfortunately Ansible doesn’t let you include defaults.

    What you can do tho, is include vars like this:

    - name: "Include distribution specific vars"
      include_vars: "{{ item }}"
      with_first_found: "{{ ansible_facts['distribution'] }}.yml"
    

    then override included vars when needed, with:

    - name: "Override vars"
      set_fact:
        var: "{{ override_var }}"
      when: override_var is defined
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search