【问题标题】:Executing specific tasks against specific hosts in an Ansible playbook在 Ansible playbook 中针对特定主机执行特定任务
【发布时间】:2020-08-22 00:31:14
【问题描述】:

我需要使用 Ansible playbook 将我的应用部署到两个 VM。每个虚拟机都有不同的用途,我的应用由几个不同的组件组成,因此每个组件在 playbook 中都有自己的一组任务。

我的库存文件如下所示:

[vig]
192.168.10.11

[websvr]
192.168.10.22

Ansible playbook 只有一个用于声明主机,它位于顶部,所有任务都针对指定的主机执行。但我希望达到的是:

  1. 任务 1 到 10 针对 vig 组执行
  2. 任务 11 到 20 针对 websvr 组执行

所有内容都在同一个剧本中,例如:ansible-playbook -i <inventory file> deploy.yml

这可能吗?我必须使用 Ansible 角色来实现这一点吗?

【问题讨论】:

    标签: ansible


    【解决方案1】:

    剧本可以有多个剧本(见https://docs.ansible.com/ansible/latest/user_guide/playbooks_intro.html)。

    剧本可以包含多个剧本。你可能有一本剧本 首先针对 Web 服务器,然后针对数据库服务器。为了 示例:

    ---
    - hosts: webservers
      remote_user: root
    
      tasks:
      - name: ensure apache is at the latest version
        yum:
          name: httpd
          state: latest
      - name: write the apache config file
        template:
          src: /srv/httpd.j2
          dest: /etc/httpd.conf
    
    - hosts: databases
      remote_user: root
    
      tasks:
      - name: ensure postgresql is at the latest version
        yum:
          name: postgresql
          state: latest
      - name: ensure that postgresql is started
        service:
          name: postgresql
          state: started
    

    【讨论】:

    • 它有效。非常感谢您的启发。 :)
    猜你喜欢
    • 2018-05-19
    • 1970-01-01
    • 2017-09-08
    • 2020-10-05
    • 2018-10-06
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    相关资源
    最近更新 更多