Ansible run certain tasks one one group of servers, other on another

jdog asked:

For a migration, I need to run some tasks in a playbook on one group of hosts, some on another.

I have tried doing this by adding hosts: to each task: (all tasks are in a single role)

- 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?

My answer:


You run tasks on different hosts by grouping the tasks into plays, each of which can define the set of hosts its tasks runs on. It’s not really valid to set hosts for an individual task, which is the immediate cause of the error message.

Here is an example, with two plays, each defining a different set of hosts. Each play defines the hosts on which its tasks will run.

 - 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

Above is one play containing two tasks. Below is one play containing one task.

 - 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

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.