ansible – Run Certain Tasks on Specific Groups of Servers

ansibleansible-playbook

I'm migrating a group of websites from one infrastructure to another, including a fairly slow rsync operation. The migration process includes changes to both the source and target systems.

Due to a fairly slow rsync operation that is part of the process, I am looping through a var file which contains the details for each website and then run through the tasks required to move one website at a time.

I cannot find a way to target some tasks at the source system and some at the target system. I have tried doing this by adding hosts: to each task: (all tasks are in a single role)

My playbook:

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

  roles:
   - unmigratesite

unmigratesite role:

---

- 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

However I'm getting an error:

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

Exactly 3 times – as many times as there are hosts.

How can I run some tasks against one group and some against another group of servers?

Best Answer

I cannot find a way to target some tasks at the source system and some at the target system. I have tried doing this by adding hosts: to each task: (all tasks are in a single role)

My first idea reading this is to replace your hosts: by when: "'old_server' in group_names" or when: "'new_server' in group_names"

If that doesn't work, I'ld give a shot at delegate_to, here's the delegation documentation

one website at a time

If you want to be really slow, consider using serial: 1. Or even --step in the cli.