skip to Main Content

An Ansible role supports Debian Stretch and Buster.

It is not able to do the job on Jessie or older versions.

Which is the best way to tell the user that the role cannot be used on a given old version?

  • Do nothing in main.yml file (controlling the distro version using when: declarations)
  • Let the role explicitly fail using the fail module
  • Do not check for a supported distro version and let tasks fail themselves

2

Answers


  1. Developers should place the supported/tested versions in the Readme. Then users should always read the Readme. Then, common sense should be used.
    But we all know that’s not the case.

    You could configure the host(s) which are too old skip to the role, to ensure the hosts do not execute any command for that role. But the way to go would be to built another role, or update that role, to let that playbook support that OS version.

    This method is the least desired one: Do not check for a supported distro version and let tasks fail themselves. Because when you go down this path, then some unsupported tasks are executed on the host and then you can’t guarantee the state of the system anymore. In short; you’ll create a mess.

    To simply prevent the nightmare, indeed, let the play fail:

    - name: fail when using older version
      fail:
        msg: "You fail because reason, woohoo"
      when: ansible_distribution is Ubuntu and ansible_distribution_version is 10.04
    
    Login or Signup to reply.
  2. Q: “What is the best way to manage unsupported distros in an Ansible role?”

    A: It’s a good idea to end the host or play when the platform and version is not supported. In most cases, this means such a platform and version hasn’t been tested yet. It’s up to the user to add a new platform and version to the metadata, test it and optionally contribute to the development.

    In a role, it’s possible to read the variable galaxy_info from the role’s file meta/main.yml and test the supported platforms and versions.

    $ cat roles/role_1/meta/main.yml
    galaxy_info:
      author: your name
      description: your role description
      company: your company (optional)
      license: license (GPL-2.0-or-later, MIT, etc)
      min_ansible_version: 2.9
      platforms:
        - name: Ubuntu
          versions:
            - bionic
            - cosmic
            - disco
            - eoan
      galaxy_tags: []
    dependencies: []
    

    For example the tasks in the role below

    $ cat roles/role_1/tasks/main.yml
    ---
    
    - name: Print OS and distro Ansible variables collected by setup
      debug:
        msg:
          - "ansible_os_family: {{ ansible_os_family }}"
          - "ansible_distribution: {{ ansible_distribution }}"
          - "ansible_distribution_major_version: {{ ansible_distribution_major_version }}"
          - "ansible_distribution_version: {{ ansible_distribution_version }}"
          - "ansible_distribution_release: {{ ansible_distribution_release }}"
    
    - name: Include roles' meta data
      include_vars:
        file: "{{ role_path }}/meta/main.yml"
    
    - name: Test the distribution is supported. End the host if not.
      set_fact:
        supported_distributions: "{{ galaxy_info.platforms|json_query('[].name') }}"
    - debug:
        var: supported_distributions
    - block:
        - debug:
            msg: "{{ ansible_distribution }} not supported. End of host."
        - meta: end_host
      when: ansible_distribution not in supported_distributions
    
    - name: Test the release is supported. End the host if not.
      set_fact:
        supported_releases: "{{ (galaxy_info.platforms|
                                selectattr('name', 'match', ansible_distribution)|
                                list|first).versions }}"
    - debug:
        var: supported_releases
    - block:
        - debug:
            msg: "{{ ansible_distribution_release}} not supported. End of host."
        - meta: end_host
      when: ansible_distribution_release not in supported_releases
    
    - name: The distribution and release is supported. Continue play.
      debug:
        msg: "{{ ansible_distribution }} {{ ansible_distribution_release }} is supported. Continue play."
    

    with the playbook

    - hosts: localhost
      gather_facts: true
      roles:
        - role_1
    

    give

        "msg": [
            "ansible_os_family: Debian", 
            "ansible_distribution: Ubuntu", 
            "ansible_distribution_major_version: 19", 
            "ansible_distribution_version: 19.04", 
            "ansible_distribution_release: disco"
        ]
    
        "supported_distributions": [
            "Ubuntu"
        ]
    
        "supported_releases": [
            "bionic", 
            "cosmic", 
            "disco", 
            "eoan"
        ]
    
        "msg": "Ubuntu disco is supported. Continue play."
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search