Reputation: 29
Currently when we are creating an AMI in AWS using packer then we Read variables for detected OS from /vars/var_<linux-os>.yml
files
example
- name: read variables for ubuntu 20.04
include_vars: "vars_os_ubuntu-20.yml"
when: ansible_distribution == "Ubuntu" and ansible_distribution_version == "20.04"
As you can see in the above code, we have specifically mentioned the Ubuntu version, now, if the version changes to 22.04 or let say 24.04, how can we set this value dynamically?
Is there any way where we can implement the same in Ansible?
Upvotes: 1
Views: 300
Reputation: 39159
You can use variables in the include_vars
:
- name: >-
Include variables for {{ ansible_distribution }}
{{ ansible_distribution_version }}
include_vars: >-
vars_os_{{ ansible_distribution | lower }}-
{{- ansible_distribution_version.split('.')[0] }}.yml
This would, in my Alpine container, give me, when running the playbook in verbose mode — so, with a -v
flag:
TASK [Include variables for Alpine 3.15.4] ***********************************
ok: [localhost] => changed=false
ansible_facts: {}
ansible_included_var_files:
- /usr/local/ansible/vars_os_alpine-3.yml
And so, as stated by the verbose message, include the file vars_os_alpine-3.yml, which match the pattern in your requirement.
Upvotes: 1