根据操作系统复制不同的configuration文件

我想根据使用Ansible的操作系统版本来分发不同的configuration文件。

我想区分操作系统版本与ansible_distribution ansible_distribution,这样我就不必手动分配操作系统版本。

我遇到的问题是我不知道在哪里指定哪个版本的configuration文件用于特定的操作系统版本。
我想在剧本中分配这个variables,而不是在一些额外的variables文件中,因为它绝对只用在这个剧本中。
我想我喜欢这样的东西:

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

但是我不确定Ansible是否可以像这样工作(或者如果你想这样使用的话)

我目前使用以下,由于多种原因显然可怕的:

 --- - 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 ... 

(我刚刚开始使用Ansible,我仍然试图找出它是否是我想要做的正确的工具,所以请原谅我对Ansible的可怕使用)

编辑 :我只是意识到这是一个可怕的例子,因为我可以使用

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

为文件来源。
如果文件DESTINATION在不同的操作系统上不同,我将如何分配正确的variables?

带有词典列表的示例剧本:

 --- - 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 

根据自己的喜好自定义。