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

un migrationonesite.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심지어CLI.

답변2

작업을 그룹화하여 다양한 호스트에서 작업을 실행합니다.연극, 각각은 해당 작업이 실행되는 호스트 집합을 정의할 수 있습니다. 오류 메시지의 직접적인 원인인 개별 작업에 대해 호스트를 설정하는 것은 실제로 유효하지 않습니다.

다음은 각각 서로 다른 호스트 집합을 정의하는 두 개의 연극이 포함된 예입니다. 각 플레이는 해당 작업이 실행될 호스트를 정의합니다.

 - 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

관련 정보