Ansible – playbook calls another playbook with variables, tags, and limits

ansibleansible-2.xansible-playbook

I have a blue green deploy playbook. It relies on certain variables to determine which hosts to apply the underlying roles. Here is one of the roles for an example:

- name: Remove current server from load balancer
  hosts: 'tag_Name_{{server_name}}_production'
  remote_user: ec2-user
  sudo: true
  roles:
    - remove-load-balancer

I can call this playbook with specified limits and tags and it works wonderfully – but only for one type of server. For example, this command will blue green deploy our services servers:

ansible-playbook blue.green.yml -i ec2.py -l tag_Name_services_production,tag_Name_services_production_old --skip-tags=restart,stop -e server_name=services -e core_repo=~/core

I would like to write a master blue green playbook which essentially runs several playbooks – first for the api servers and then for the services servers. I have tried using includes but cannot seem to get the syntax right – ansible either complains that my task doesn't do anything or complains that the syntax is incorrect:


- name: Blue green deploy to all production boxes.
  hosts: localhost
  tasks:
  - include: blue.green.single.yml
    hosts:
    - tag_Name_api_production
    - tag_Name_api_production_old
    vars:
    - server_name: api
    skip-tags:
    - restart
    - stop

  - include: blue.green.single.yml
    hosts:
    - tag_Name_services_production
    - tag_Name_services_production_old
    vars:
    - server_name: services
    skip-tags:
    - restart
    - stop

Ideally I'd be able to call this like so:

ansible-playbook blue.green.yml -i ec2.py -e core_repo=~/core

Has anyone done this successfully? If so – how can I accomplish this?

Best Answer

Would this work for your case?

- name: Blue green deploy to all production boxes.
  hosts: [tag_Name_api_production, tag_Name_api_production_old]
  tasks:
    - include: blue.green.single.yml
      vars:
        - server_name: api
      skip-tags:
        - restart
        - stop

- name: Blue green deploy to all production boxes.
  hosts: [tag_Name_services_production, tag_Name_services_production_old]
  tasks:
    - include: blue.green.single.yml
      vars:
        - server_name: services
      skip-tags:
        - restart
        - stop
Related Topic