有没有更优雅的方法来使用 ansible 管理 linux mint 和 ubuntu 服务器的混合?
Is there a more elegant way to manage a mix of linux mint and ubuntu servers using ansible?
我想 运行 在 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
你可以这样做:
- 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
codename为空时,自动取发行版名称
一种方法是使用 omit
过滤器。参见documentation。当 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)。否则将被省略。
我想 运行 在 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
你可以这样做:
- 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
codename为空时,自动取发行版名称
一种方法是使用 omit
过滤器。参见documentation。当 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)。否则将被省略。