Ansible run certain tasks one one group of servers, other on another
Solution 1:
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.
Solution 2:
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