I would like to install Apache on several linux server. Apache package has not the same name on RedHat or Debian operating system (apache2 vs httpd): Is it a way to use an ansible fact variable ("ansible_os_family") as a key of a dictionary variable ?
Something like that (but this doesn’t work) :
---
- name: playbook1
hosts: all
become: yes
vars:
apache_packages: {
"RedHat": "httpd",
"Debian": "apache2"
}
tasks:
- name: Install Apache server
package:
name: "{{ apache_packages['{{ ansible_os_family }}'] }}"
state: present
...
3
Answers
Nesting Jinja delimiters inside another Jinja delimiter is never a good idea.
Source: https://docs.ansible.com/ansible/latest/reference_appendices/faq.html#when-should-i-use-also-how-to-interpolate-variables-or-dynamic-variable-names
If you don’t surround something with quotes, it will be assumed as being a variable, so in this case, this is as simple as:
try this: you define packages as a dict of list (based on os family)
I would do some thing like below to reduce the lines