Ansible 在一組伺服器上執行某些任務,在另一組伺服器上執行其他任務

Ansible 在一組伺服器上執行某些任務,在另一組伺服器上執行其他任務

我正在將一組網站從一個基礎設施遷移到另一個基礎設施,其中包括相當緩慢的 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 }}"

取消移轉onesite.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

我找不到一種方法來將某些任務定位在來源系統上,而將某些任務定位在目標系統上。我嘗試透過為每個任務新增主機:(所有任務都在一個角色中)來做到這一點

讀到這裡我的第一個想法是將你的替換hosts:when: "'old_server' in group_names"when: "'new_server' in group_names"

如果這不起作用,我會嘗試一下delegate_to,這裡是代表團文件

一次一個網站

如果你想非常慢,請考慮使用serial: 1.或甚至--step命令列介面

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

上面是包含兩個任務的一齣戲。以下是包含一項任務的一齣戲。

 - 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

相關內容