我想在Linux mint和Ubuntu上运行类似的命令,但它们有细微的区别。 我找到了一个解决方案,但它让我重写每个任务两次。有没有更好的方法可以做到这一点?
- name: install the latest version of Ansible
block:
- name: Add Ansible PPA (Ubuntu)
ansible.builtin.apt_repository:
repo: ppa:ansible/ansible
become: true
when: ansible_facts['distribution'] == 'Ubuntu'
- name: Add Ansible PPA (Linux Mint)
ansible.builtin.apt_repository:
repo: ppa:ansible/ansible
codename: focal
become: true
when: ansible_facts['distribution'] == 'Linux Mint' and ansible_distribution_release == 'una'
- name: install Ansible and dependency
apt:
name:
- software-properties-common
- ansible
state: latest
update_cache: yes
become: true
一种方法是使用omit
过滤器。请参阅文档。这可用于在运行apt_repository
模块时使codename
参数可选。
例:
- name: set distro codename when OS is Linux Mint
ansible.builtin.set_fact:
distro_codename: focal
when: ansible_facts['distribution'] == 'Linux Mint' and ansible_distribution_release == 'una'
- name: Add Ansible PPA
ansible.builtin.apt_repository:
repo: "ppa:ansible/ansible"
codename: "{{ distro_codename | default(omit) }}"
这将确保在创建 APT 存储库时使用codename
(如果已设置)(在本例中为 Linux Mint)。否则,将省略。
你可以做这样的事情:
- name: install the latest version of Ansible
block:
- name: Add Ansible PPA (Linux Mint or Ubuntu)
ansible.builtin.apt_repository:
repo: ppa:ansible/ansible
codename: "{{ '' if ansible_facts['distribution'] == 'Ubuntu' else 'focal' }}"
become: true
- name: install Ansible and dependency
apt:
name:
- software-properties-common
- ansible
state: latest
update_cache: yes
become: true
当代号为空时,它会自动采用分发名称