Hello,
I had an idea with regards to a common syntax (or a filter, if you will) that would have ansible auto-select items in playbooks based on existing vars like ansible_os_family. I was wondering if (as I’m still pretty new here) this is something already in the works, is already done better in other ways, or whether it would be worth discussion. Essentially, as long as ansible is keeping track of all of these parameters why can’t a playbook use them to intelligently select which files or parts of a playbook to pay attention to.
Perhaps something like “ansible_os_family:ansible_distribution:ansible_distribution_release:ansible_distribution_version:FOO”? and simply leaving out the irrelevant bits?
Example 1 - “autosource” option to copy module, instead of source
Files directory:
luser@new12:~$ ls -l foo/roles/files/
total 8
-rw-rw-r-- 1 luser luser 1015 Oct 1 15:44 Debian::::sshd_config
-rw-rw-r-- 1 luser luser 985 Oct 1 15:45 Debian:Ubuntu:precise:12.04:sshd_config
Task snippet:
- name: Pick a file based on ansible_* vars
copy: autosource=sshd_config dest=/etc/ssh/sshd_config
Result:
Running through with any OS in the Debian family besides Ubuntu precise 12.04 would get you the first, more generic config file, while running that particular version of Ubuntu (which we’ll pretend has some sort of bug that requires an option be removed from the config) would get the second config file.
Example 2 - handler intelligence
Playbook snippet:
tasks:
- name: Change a config file
lineinfile: dest=/etc/some_service/config regexp=“^anoption” line=“anoption no” state=present
notify: Restart some_service
handlers
- name: Restart some_service Debian::::
service: name=sservice state=restarted - name: Restart some_service Debian:Debian:lenny:: # sserviced in older
service: name=sserviced state=restarted - name: Restart some_service Debian:Ubuntu:hardy:: # sserviced in older
service: name=sserviced state=restarted
Result:
Hardy and Lenny are handled differently than all the others.