我正在将一组网站从一个基础设施迁移到另一个基础设施,其中包括一个相当慢的 rsync 操作。迁移过程包括对源系统和目标系统的更改。
由于该过程的一部分 rsync 操作相当慢,我循环遍历包含每个网站详细信息的 var 文件,然后执行一次移动一个网站所需的任务。
我找不到一种方法来将一些任务定位到源系统,将一些任务定位到目标系统。我尝试通过向每个任务添加主机来实现这一点:(所有任务都属于一个角色)
我的剧本:
---
- hosts:
- tag_aws_autoscaling_groupName_thename
- tag_Name_server_name
vars_files:
- group_vars/all
roles:
- unmigratesite
unmigratesite 角色:
---
- include_tasks: "unmigrateonesite.yml"
loop: "{{ wordpress_websites }}"
取消迁移一个站点.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 次——与主持人的数量一样多。
我如何针对一组服务器运行一些任务并针对另一组服务器运行一些任务?
答案1
答案2
通过将任务分组到不同的主机上,您可以在不同的主机上运行任务戏剧,每个任务都可以定义其任务在其上运行的主机集。为单个任务设置主机实际上无效,这是导致错误消息的直接原因。
以下是一个包含两个 play 的示例,每个 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
上面是一个包含两个任务的 play。下面是一个包含一个任务的 play。
- 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