AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 1000547
Accepted
Subramanian Lakshmanan
Subramanian Lakshmanan
Asked: 2020-01-28 04:36:36 +0800 CST2020-01-28 04:36:36 +0800 CST 2020-01-28 04:36:36 +0800 CST

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

  • 772

我有一台带有 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.yml和ubuntu.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_tasks为,include_tasks但我得到了同样的错误。

ansible ansible-playbook
  • 2 2 个回答
  • 7118 Views

2 个回答

  • Voted
  1. c4f4t0r
    2020-01-28T09:52:47+08:002020-01-28T09:52:47+08:00

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

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

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

    • 3
  2. Best Answer
    John Mahowald
    2020-01-28T06:15:58+08:002020-01-28T06:15:58+08:00

    我的解决方案是将它们合并到一个游戏中,因为它们做同样的事情。

    ---
    - 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 那样添加 repos 时不需要它。

    Vars 结构是 OS 系列特定值的字典。这使得包和服务名称能够被事实索引。操作系统系列,所以除了 CentOS 和 Ubuntu 之外,这也适用于 RHEL 和 Debian。

    缩进是错误的。模块参数需要缩进低于任务级别指令的级别,例如name:.

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


    还有更多的工作要使它有用。

    当你需要使用template安装配置时,EL 将 configs 放入/etc/httpd/,而 Debian 放入/etc/apache2/.

    大量的 Web 服务器角色是开源的,如果你想要想法,可以看看。检查银河。

    考虑将您的任务转移到角色,以实现重用。

    • 1

相关问题

  • 重复的 Ansible 任务

  • 无法形成站点中的文件的链接,该链接可用于使用 ansible 在远程服务器中启用的目录站点?

  • 如何执行 ansible 的特定角色?

  • Ansible 和 rbash

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve