A rough idea on specifying resources based on ansible_* vars

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.

Actually this is something you can do already with the “group_by” module, but I think we could provide better ways to surface it.

See “group_by” in the module docs for how this works.

This is something I was already planning for as second thing to do after we start recording facts to a pluggable backend.

I’d probably guess the syntax should be

  • hosts: whatever_groups
    when:
  • ansible_os_family == “Debian”
  • foons > sporks
  • not emergency_mode

Etc

And if the host didn’t meet the criteria, it would get pulled out of the group.

There’s nothing to say this has to wait for fact caching though.

Very cool. Thanks for your response.

Mark