2

I want to distribute different config files depending on the OS version using Ansible.

I'd like to distinguish the OS version with the ansible fact ansible_distribution so that I dont have to manually assign an OS version.

The problem I have is that I don't know where to specify which version of the configuration file to use for a specific OS version.
I'd like to assign this variable inside the playbook and not in some additional variable file, since it's absolutely only used inside this playbook.
I think I'd like something like:

- ansible_distribution == "Debian"
  - vars:
    vimrc_file = "vimrc.DEBIAN"
- ansible_distribution == "Ubuntu"
  - vars:
    vimrc_file = "vimrc.UBUNTU"

but I'm not sure if Ansible works like that (or if it does if you're supposed to use it like that)

I currently use the following, which obviously is terrible for multiple reasons:

---
- hosts: servers,workstations

  tasks:
  - name: Ensure vim is installed
    apt: name=vim state=latest

  - shell: echo "vimrc.DEBIAN"
    changed_when: false
    register: vimrc
  - name: "Copy Debian vimrc file"
    copy: src=/ansible/files/{{ vimrc.stdout }} dest=/etc/vim/vimrc
    when: ansible_distribution == "Debian"
    with_items: vimrc.stdout

  - shell: echo "vimrc.UBUNTU"
    changed_when: false
    register: vimrc
  - name: "Copy Ubuntu vimrc file"
    copy: src=/ansible/files/{{ vimrc.stdout }} dest=/etc/vim/vimrc
    when: ansible_distribution == "Ubuntu"
    with_items: vimrc.stdout
...

(I'm just starting to use Ansible and I'm still trying to figure out if it's even the right tool for what I want to do, so please excuse my horrible use of Ansible)

EDIT: I just realized this is a terrible example, since I could just use

/ansible/files/vimrc.{{ ansible_distribution }}

for the file source.
How would I assign the correct variables if the file DESTINATION is different on different OS?

Ansgar
  • 123
  • 4
  • 1
    http://serverfault.com/a/697508/126632 – Michael Hampton Dec 07 '16 at 21:53
  • Like I said I don't see the point in declaring a variable in an additional file, if I only need it in this one specific playbook. I'd like to declare it inside the playbook. – Ansgar Dec 07 '16 at 22:30
  • You aren't going to have just one variable. You'll eventually have lots of them. – Michael Hampton Dec 07 '16 at 22:32
  • Of course I will and I don't want to deny the necessity of including variables. But I don't think I will ever need the path of the vimrc file in any playbook other than the one which sets up vim. Declaring that variable in an additional file would unnecessarily split information between files. – Ansgar Dec 07 '16 at 22:44
  • Why do you think it's unnecessary? – Michael Hampton Dec 07 '16 at 22:55
  • I will eventually have a lot of variables that are OS specific like the location of a lot of configuration files. If I store them all in OS specific files I will never know if I still need them and if so in which playbooks they are used. In a lot of cases one variable will only be needed in one specific playbook. So in my opinion it's unneccesary to split information about where a config file is stored from the playbook which installs the corresponding package and copies the config file. (Unless the location of the config file is needed in another playbook, in which case it would make sense) – Ansgar Dec 12 '16 at 22:23

1 Answers1

0

An example playbook with a list of dictionaries:

---                                           
- hosts: localhost
  connection: local

  vars:
    distribution_settings:                                 
      - distribution: "MacOSX"
        vimrc_file: "vimrc.MACOSX"
        vimrc_location: "/destination/path/on/mac"
      - distribution: "Debian"
        vimrc_file: "vimrc.DEBIAN"
        vimrc_location: "/destination/path/on/debian"     

  tasks:                                         
    - set_fact:                                      
        current_distribution_settings: "{{ distribution_settings | selectattr('distribution', 'equalto', ansible_distribution) | list }}"                                 
    - debug:
        var: current_distribution_settings[0].vimrc_file
    - debug:
        var: current_distribution_settings[0].vimrc_location

Customise to your liking.

techraf
  • 4,163
  • 8
  • 27
  • 44