使用 Ansible 在多个操作系统上安装多个软件包

使用 Ansible 在多个操作系统上安装多个软件包

我有一台带有 2 台服务器的主机:一台安装了 CentOS,另一台安装了 Ubuntu。

我决定在两台服务器上安装 apache、nginx 和 php-fpm,并编写了 3 个剧本。

  1. ubuntu.yml (/home/ansible/playbook):
---
- name: Ubuntu Playbook
  hosts: ubun
  become: true
  vars:
   - packages:
     - nginx
     - apache2
     - php-fpm 
  tasks:
   - name: Update apt package
   apt:
   name: "*"
   state: latest
   update_cache: yes
   - name: Install Packages
     apt:
       pkg: "{{ packages }}"
       state: latest
       update_cache: yes
   - name: Apache Service Start
     service:
       name: nginx
       state: restarted
       enabled: yes
  1. centos.yml (/home/ansible/playbook):
---
- name: CentOS Playbook
  hosts: cent
  become: true
  vars:
   packages:
   - epel-release
   - httpd
   - nginx
   - php-fpm
  tasks:
   - name: Update yum package
     yum:
       name: "*"
       state: latest
       update_cache: yes
   - name: Install Packages
     yum:
       name: "{{ packages }}"
       state: latest
       update_cache: yes
   - name: Apache Service Start
     service:
       name: nginx
       state: restarted
       enabled: yes
  1. base.yml(/home/ansible/playbook):
---
- name: Base Playbook
  hosts: aws
  become: true
  tasks:
    - name: Performing Tasks for CentOS
      when: ansible_facts['distribution'] == 'CentOS'
      include_tasks: centos.yml
    - name: Performing Tasks for Ubuntu
      when: ansible_facts['distribution'] == 'Ubuntu'
      include_tasks: ubuntu.yml

我的 3 个 Ansible 组是:

  • [aws] 包含服务器

  • [cent] 包含 CentOS 服务器

  • [ubun] 包含 Ubuntu 服务器

我尝试了干运行centos.ymlubuntu.yml单独运行并且成功了,但是当我尝试干运行时base.yml出现以下错误:

    FAILED! => {"reason": "unexpected parameter type in action: <class 'ansible.parsing.yaml.objects.AnsibleSequence'>\n\nThe error appears to be in '/home/ansible/playbook/centos.yml': line 2, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n---\n- name: CentOS Playbook\n  ^ here\n"}

    FAILED! => {"reason": "unexpected parameter type in action: <class 'ansible.parsing.yaml.objects.AnsibleSequence'>\n\nThe error appears to be in '/home/ansible/playbook/ubuntu.yml': line 2, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n---\n- name: Ubuntu Playbook\n  ^ here\n"}

我已经尝试替换import_tasksinclude_tasks出现同样的错误。

答案1

我的解决方案是将它们合并为一个剧本,因为它们的作用是相同的。

---
- name: php-fpm play
  hosts: aws
  become: true
  vars:
  - repo:
      Debian:
      - apt # already installed, but need something here
      RedHat:
      - epel-release
  - packages:
      Debian:
      - apache2
      #- nginx # cannot have 2 listening on port 80
      - php-fpm
      RedHat:
      - httpd
      #- nginx
      - php-fpm
  - services:
      Debian: apache2
      RedHat: httpd

  tasks:
     # TODO move update * task to different play

   - name: Install repo
     # Seperate package transaction for EPEL
     # so it is available in the next task
     package:
       name: "{{ repo[ansible_os_family] }}"

   - name: Install Web Server Packages
     # Keyed by OS family fact to also support RHEL and Debian
     package:
       name: "{{ packages[ansible_os_family] }}"
       state: latest

   - name: Web Service Start
     service:
       name: "{{ services[ansible_os_family] }}"
       state: restarted
       enabled: yes

不能有多个服务器监听端口 80 和 443。我注释掉了 nginx,因为任务被错误地标记为“Apache 服务启动”。如果您希望其中一个服务器代理另一个服务器或类似的东西,则需要部署配置文件来更改端口。

使用package:委托给实际包管理器的操作。使包安装任务能够在不同的操作系统上运行。无法通过update_cache这种方式实现,但 yum 在添加存储库时不需要它,而 apt 则需要它。

Vars 结构是操作系统系列特定值的字典。这使得程序包和服务名称能够通过事实进行索引。操作系统系列,因此除了 CentOS 和 Ubuntu 之外,它还适用于 RHEL 和 Debian。

缩进错误。模块参数需要比任务级别指令低一级缩进,例如name:

不能include_tasks只使用import_playbook.来完成整个剧本,include_tasks当你有角色时会更容易,角色有此类文件的任务目录。(剧本级别任务是一回事,但我赞成角色做所有事情。)


要使它变得有用,还有更多的工作要做。

当需要使用template安装配置时,EL 将配置放在 中/etc/httpd/,而 Debian 则放在 中/etc/apache2/

很多 Web 服务器角色都是开源的,如果你想要一些想法,可以看看。检查星系

考虑将您的任务转移到角色,以便重复使用。

答案2

如果你以角色为例会更好,这里有固定的代码:

---

- hosts: aws
  remote_user: myuser
  become: true
  tasks:
  - name: Performing Tasks for CentOS
    include_tasks: centos.yml
    when: ansible_facts['distribution'] == 'CentOS'
  - name: Performing Tasks for Ubuntu
    include_tasks: ubuntu.yml
    when: ansible_facts['distribution'] == 'Ubuntu'

centos.yml 任务:

---

- name: installing httpd
  yum: pkg=httpd state=present

ubuntu.yml 任务:


- name: installing apache2
  apt: pkg=apache2 state=present

在任务文件中,您只需要任务,而不需要主机、任务等。

相关内容