Ansible: Ubuntu alternatives using the community.general collection

In a previous article, I showed how to manually setup Alternatives so that different versions of a binary could co-exist on a target machine.

In that step-by-step example, we used the Terraform binary as an example, and placed two independent versions in /usr/local/bin, and then set the priority so that terraform14 was preferred.

To do the same thing using Ansible we first need to install the Ansible Galaxy ‘community.general’ collection.

ansible-galaxy collection install community.general

Which allows use Ansible tasks like below.

- name: terraform12 option
  community.general.alternatives:
    name: terraform
    path: /usr/local/bin/terraform12
    link: /usr/local/bin/terraform
    priority: "10"
- name: terraform14 option with higher priority
  community.general.alternatives:
    name: terraform
    path: /usr/local/bin/terraform14
    link: /usr/local/bin/terraform
    priority: "20"

- name: set to auto so highest priority wins
  command:
    cmd: update-alternatives --auto terraform

REFERENCES

Ansible community.general.alternatives

Ansible galaxy, community.general collection

 

NOTES:

ansible-galaxy collection download path can be placed into shared group folder

ansible-galaxy collection install community.general -p my-galaxy-collections

Collections path can be set in ansible.cfg

collections_paths = my-galaxy-collections