Moving to ansible_facts['distribution']

Software

I always add conditions to my Ansible playbooks to check the target OS and distribution. Until recently I did this:

- name: Install FreeBSD package (legacy)
  community.general.pkgng:
      name: "cowsay"
      state: present
  when: ansible_distribution == "FreeBSD"

This is also useful for differentiating between different distributions of the same OS, such as Debian or Fedora.

Reddit: Preferable to use ansible_facts or ansible_distribution?

According to akasivel on this Ansible thread, this will soon be formally deprecated in favour of ansible_facts:

- name: Install FreeBSD package
  community.general.pkgng:
      name: "cowsay"
      state: present
  when: ansible_facts['distribution'] == "FreeBSD"

Author bio and support

Me!

Ruben Schade is a technical writer and infrastructure architect in Sydney, Australia who refers to himself in the third person. Hi!

The site is powered by Hugo, FreeBSD, and OpenZFS on OrionVM, everyone’s favourite bespoke cloud infrastructure provider.

If you found this post helpful or entertaining, you can shout me a coffee or send a comment. Thanks ☺️.