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 / 问题 / 1049299
Accepted
jdog
jdog
Asked: 2021-01-11 13:53:10 +0800 CST2021-01-11 13:53:10 +0800 CST 2021-01-11 13:53:10 +0800 CST

Ansible 在一组服务器上运行某些任务,在另一组服务器上运行其他任务

  • 772

我正在将一组网站从一个基础架构迁移到另一个基础架构,包括相当慢的 rsync 操作。迁移过程包括对源系统和目标系统的更改。

由于该过程的一部分 rsync 操作相当慢,我正在循环一个包含每个网站详细信息的 var 文件,然后运行一次移动一个网站所需的任务。

我无法找到一种方法来针对源系统中的一些任务和目标系统中的一些任务。我已经尝试通过添加主机来做到这一点:每个任务:(所有任务都在一个角色中)

我的剧本:

---
- hosts:
    - tag_aws_autoscaling_groupName_thename
    - tag_Name_server_name
  vars_files:
    - group_vars/all

  roles:
   - unmigratesite

取消迁移站点角色:

---

- include_tasks: "unmigrateonesite.yml"
  loop: "{{ wordpress_websites }}"

unmigrateonesite.yml:

- name: rsync site
  hosts: tag_aws_autoscaling_groupName_thename
  shell: rsync -avz /efs/www/{{new_folder}}/htdocs/ 172.31.18.217:/www/{{item.folder}}
  run_once: true
  register: rsync_out

- name: setup proxy host file
  template: src=proxy-host.j2 dest=/efs/nginx/sites-available/{{item.name}} owner=root group=root mode=0644
  hosts: tag_aws_autoscaling_groupName_thename
  notify:
    - restart nginx
  tags:
    - site
    - nginx-host-conf
    - nginx-temp-proxy

- name: setup wp-config.php WP_CONTENT_DIR on old server
  lineinfile: name=/www/{{ folder }}/{{ configuration_file }} regexp=WP_CONTENT_DIR line="define('WP_CONTENT_DIR', '/www/{{folder}}/app');"
  hosts: tag_Name_server_name
  ignore_errors: yes
  tags:
    - wp-config.php
    - wordpress
    - site
    - WP_CONTENT_DIR

但是我收到一个错误:

    fatal: [54.219.216.237]: FAILED! => {"reason": "conflicting action statements: shell, hosts\n\nThe error appears to be in '/Users/jd/projects/bizinconline/ansible/roles/unmigratesite/tasks/unmigrateonesite.yml': line 22, 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: rsync site\n  ^ here\n"}
    fatal: [54.193.100.223]: FAILED! => {"reason": "conflicting action statements: shell, hosts\n\nThe error appears to be in '/Users/jd/projects/bizinconline/ansible/roles/unmigratesite/tasks/unmigrateonesite.yml': line 22, 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: rsync site\n  ^ here\n"}
    fatal: [13.57.52.221]: FAILED! => {"reason": "conflicting action statements: shell, hosts\n\nThe error appears to be in '/Users/jd/projects/bizinconline/ansible/roles/unmigratesite/tasks/unmigrateonesite.yml': line 22, 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: rsync site\n  ^ here\n"}

正好 3 次 - 与主机数量一样多。

如何对一组服务器运行一些任务,而对另一组服务器运行一些任务?

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

2 个回答

  • Voted
  1. Best Answer
    Rémy
    2021-01-18T23:15:42+08:002021-01-18T23:15:42+08:00

    我无法找到一种方法来针对源系统中的一些任务和目标系统中的一些任务。我已经尝试通过添加主机来做到这一点:每个任务:(所有任务都在一个角色中)

    我读到这篇文章的第一个想法是hosts:用when: "'old_server' in group_names"或替换你的when: "'new_server' in group_names"

    如果这不起作用,我会试一试delegate_to,这是代表团文档

    一次一个网站

    如果你想真的很慢,可以考虑使用serial: 1. 甚至--step在cli中。

    • 3
  2. Michael Hampton
    2021-01-11T15:04:51+08:002021-01-11T15:04:51+08:00

    您可以通过将任务分组到 play中来在不同的主机上运行任务,每个 play 都可以定义其任务运行的主机集。为单个任务设置主机实际上是无效的,这是错误消息的直接原因。

    这是一个例子,有两场戏,每场都定义了一组不同的主持人。每个播放定义其任务将在其上运行的主机。

     - name: configure new hosts
       hosts: tag_aws_autoscaling_groupName_thename
       strategy: free
       tasks:
        - name: rsync site
          shell: rsync -avz /efs/www/{{new_folder}}/htdocs/ 172.31.18.217:/www/{{item.folder}}
          run_once: true
          register: rsync_out
        
        - name: setup proxy host file
          template: src=proxy-host.j2 dest=/efs/nginx/sites-available/{{item.name}} owner=root group=root mode=0644
          notify:
            - restart nginx
          tags:
            - site
            - nginx-host-conf
            - nginx-temp-proxy
    

    上面是一个包含两个任务的游戏。下面是一个包含一项任务的游戏。

     - name: configure old hosts
       hosts: tag_Name_server_name
       strategy: free
       tasks:
        - name: setup wp-config.php WP_CONTENT_DIR on old server
          lineinfile: name=/www/{{ folder }}/{{ configuration_file }} regexp=WP_CONTENT_DIR line="define('WP_CONTENT_DIR', '/www/{{folder}}/app');"
          ignore_errors: yes
          tags:
            - wp-config.php
            - wordpress
            - site
            - WP_CONTENT_DIR
    
    • 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