Ansible:如何安装多个 rpm 但每个 os 一个?
Ansible: how to install multiple rpm but one per os?
我想安装多个rpm,一个用于Fedora 服务器,另一个用于Centos 服务器。
我做了这个剧本文件,但是错了
- name: Copy rpm file to server
hosts: fedora
copy:
src: /tmp/pam_krb5-2.4.8-6.fc31.x86_64.rpm
dest: /tmp/pam_krb5-2.4.8-6.fc31.x86_64.rpm
- name: Install package.
hosts: fedora
yum:
name: /tmp/pam_krb5-2.4.8-6.fc31.x86_64.rpm
state: present
- name: Copy another rpm file to server
hosts: centos
copy:
src: /tmp/pam_krb5-2.4.8-6.el8.x86_64.rpm
dest: /tmp/pam_krb5-2.4.8-6.el8.x86_64.rpm
- name: Install another package.
hosts: centos
yum:
name: /tmp/pam_krb5-2.4.8-6.el8.x86_64.rpm
state: present
您的剧本应该有效,但您可以通过添加 when 语句来加倍保护自己:
- name: Copy another rpm file to server
hosts: centos
copy:
src: /tmp/pam_krb5-2.4.8-6.el8.x86_64.rpm
dest: /tmp/pam_krb5-2.4.8-6.el8.x86_64.rpm
when:
- ansible_facts['distribution'] == "CentOS"
- name: Install another package.
hosts: centos
yum:
name: /tmp/pam_krb5-2.4.8-6.el8.x86_64.rpm
state: present
when:
- ansible_facts['distribution'] == "CentOS"
我用一些不同的语法解决了问题
- name: Transfer and install a rpm for Centos server
hosts: centos
become_user: root
tasks:
- name: Copy another rpm file to server
copy: src=/tmp/pam_krb5-2.4.8-6.el8.x86_64.rpm dest=/tmp/pam_krb5-2.4.8-6.el8.x86_64.rpm
- name: Install the rpm
command: dnf -y localinstall /tmp/pam_krb5-2.4.8-6.el8.x86_64.rpm
- name: Transfer and install a rpm for Fedora server
hosts: fedora
become_user: root
tasks:
- name: Copy another rpm file to server
copy: src=/tmp/pam_krb5-2.4.8-6.fc31.x86_64.rpm dest=/tmp/pam_krb5-2.4.8-6.fc31.x86_64.rpm
- name: Install the rpm
command: dnf -y localinstall /tmp/pam_krb5-2.4.8-6.fc31.x86_64.rpm
您可以使用 2 个 vars 文件(每个发行版一个):
vars/centos_8.yml
packages:
- pam_krb5-2.4.8-6.el8.x86_64.rpm
- ...
vars/fedora_31.yml
packages:
- pam_krb5-2.4.8-6.fc31.x86_64.rpm
- ...
然后在你的任务中,你可以做这样的事情:
- name: Include vars for host distribution
include_vars: "{{ ansible_distribution|lower }}_{{ ansible_distribution_major_version }}.yml"
- name: Copy RPM files to server
copy:
src: /tmp/{{ item }}
dest: /tmp/{{ item }}
with_items:
- "{{ packages }}"
- name: Install RPM packages
yum:
name: /tmp/{{ item }}
state: present
with_items:
- "{{ packages }}"
我想安装多个rpm,一个用于Fedora 服务器,另一个用于Centos 服务器。 我做了这个剧本文件,但是错了
- name: Copy rpm file to server
hosts: fedora
copy:
src: /tmp/pam_krb5-2.4.8-6.fc31.x86_64.rpm
dest: /tmp/pam_krb5-2.4.8-6.fc31.x86_64.rpm
- name: Install package.
hosts: fedora
yum:
name: /tmp/pam_krb5-2.4.8-6.fc31.x86_64.rpm
state: present
- name: Copy another rpm file to server
hosts: centos
copy:
src: /tmp/pam_krb5-2.4.8-6.el8.x86_64.rpm
dest: /tmp/pam_krb5-2.4.8-6.el8.x86_64.rpm
- name: Install another package.
hosts: centos
yum:
name: /tmp/pam_krb5-2.4.8-6.el8.x86_64.rpm
state: present
您的剧本应该有效,但您可以通过添加 when 语句来加倍保护自己:
- name: Copy another rpm file to server
hosts: centos
copy:
src: /tmp/pam_krb5-2.4.8-6.el8.x86_64.rpm
dest: /tmp/pam_krb5-2.4.8-6.el8.x86_64.rpm
when:
- ansible_facts['distribution'] == "CentOS"
- name: Install another package.
hosts: centos
yum:
name: /tmp/pam_krb5-2.4.8-6.el8.x86_64.rpm
state: present
when:
- ansible_facts['distribution'] == "CentOS"
我用一些不同的语法解决了问题
- name: Transfer and install a rpm for Centos server
hosts: centos
become_user: root
tasks:
- name: Copy another rpm file to server
copy: src=/tmp/pam_krb5-2.4.8-6.el8.x86_64.rpm dest=/tmp/pam_krb5-2.4.8-6.el8.x86_64.rpm
- name: Install the rpm
command: dnf -y localinstall /tmp/pam_krb5-2.4.8-6.el8.x86_64.rpm
- name: Transfer and install a rpm for Fedora server
hosts: fedora
become_user: root
tasks:
- name: Copy another rpm file to server
copy: src=/tmp/pam_krb5-2.4.8-6.fc31.x86_64.rpm dest=/tmp/pam_krb5-2.4.8-6.fc31.x86_64.rpm
- name: Install the rpm
command: dnf -y localinstall /tmp/pam_krb5-2.4.8-6.fc31.x86_64.rpm
您可以使用 2 个 vars 文件(每个发行版一个):
vars/centos_8.yml
packages:
- pam_krb5-2.4.8-6.el8.x86_64.rpm
- ...
vars/fedora_31.yml
packages:
- pam_krb5-2.4.8-6.fc31.x86_64.rpm
- ...
然后在你的任务中,你可以做这样的事情:
- name: Include vars for host distribution
include_vars: "{{ ansible_distribution|lower }}_{{ ansible_distribution_major_version }}.yml"
- name: Copy RPM files to server
copy:
src: /tmp/{{ item }}
dest: /tmp/{{ item }}
with_items:
- "{{ packages }}"
- name: Install RPM packages
yum:
name: /tmp/{{ item }}
state: present
with_items:
- "{{ packages }}"