Help with rolling deployments

53 views
Skip to first unread message

Aleks Przhelyaskovskiy

unread,
Oct 30, 2017, 4:12:51 PM10/30/17
to Ansible Project
Hello,

Trying to setup rolling deployments for WAS using ansible core.

My environment consists of two groups of hosts. Each group has one manager, and two nodes. The deployment process consists of running a task on manager host first, and then another tasks on every node in the group (including manager). I'd like to run the whole deployment process one group at-a-time.

I was thinking about creating a top-level playbook that will loop through groups, and pass group name to each play as a parameter. Is there an easy way to accomplish this, or is there a better way of doing this?

The example inventory would be:

----- snip -----
[groups]
group1
group2

[group1:children]
manager1
nodes1

[group2:children]
manager2
nodes2

[manager1]
host_m1

[manager2]
host_m2

[nodes1]
host_a
host_b

[nodes2]
host_c
host_d
----- snip -----



Thanks in advance.

Aleks Przhelyaskovskiy

unread,
Oct 31, 2017, 9:31:35 AM10/31/17
to Ansible Project
So far, I was able to accomplish only part of my original project. By passing a variable as an argument when invoking ansible-playbook. So my command line looks like this:

   >ansible-playbook deploy_group.yml -e group_num='1'

My top level playbook deploy_group.yml contains two plays: app_deploy.yml and post_deploy.yml. I use the group_num variable to limit list of hosts to members of a single group.

The deploy_group.yml looks like this:
----- ----- begin ----- -----

---
- name: top-level deployment playbook
  hosts: 'manager{{ group_num }}'
  gather_facts: True
  serial: 1

- include: app_deploy.yml 
- include: post_deploy.yml

----- -----  end  ----- -----

The app_deploy.yml looks like this:
----- ----- begin ----- -----

---
- hosts: 'manager{{ group_num }}'

  tasks:
  - name: deploy war file
    shell: '~/scripts/deploy_war.sh >> /tmp/deploy_war.out'

----- -----  end  ----- -----

The post_deploy.yml looks like this:
----- ----- begin ----- -----

---
- hosts: 'manager{{ group_num }}:&nodes{{ group_num }}'

  tasks:
  - name: restart all java processes
    shell: '~/scripts/restart_all.sh >> /tmp/restart_all.log'

----- -----  end  ----- -----

This works for individual groups, but requires keeping track of group numbers and status of playbook execution outside of ansible. Is there a way to loop through group names, and pass group name to a set of plays at each iteration?

Aleks Przhelyaskovskiy

unread,
Oct 31, 2017, 10:26:22 AM10/31/17
to Ansible Project
Is this the correct group to ask for help with playbooks, or is there a more-appropriate group. Thx.

Kai Stian Olstad

unread,
Oct 31, 2017, 11:25:37 AM10/31/17
to ansible...@googlegroups.com
On 31. okt. 2017 15:26, Aleks Przhelyaskovskiy wrote:
> Is this the correct group to ask for help with playbooks, or is there a
> more-appropriate group. Thx.

It's the correct group.


--
Kai Stian Olstad

Kai Stian Olstad

unread,
Oct 31, 2017, 11:52:50 AM10/31/17
to ansible...@googlegroups.com
On 30. okt. 2017 21:12, Aleks Przhelyaskovskiy wrote:
> Hello,
>
> Trying to setup rolling deployments for WAS using ansible core.
>
> My environment consists of two groups of hosts. Each group has one manager,
> and two nodes. The deployment process consists of running a task on manager
> host first, and then another tasks on every node in the group (including
> manager). I'd like to run the whole deployment process one group at-a-time.

You have a few options, one of them is something like this.


inventory
---------
[group1]
manager1
host1
host2

[group2]
manager2
host3
host4


playbook.yml
---
- hosts: group1
tasks:
- name: Only run on manager
debug: msg='I'm running on manager'
when: inventory_hostname == manager1

- name: Run on all host in group
debug: msg="I'm running on all hosts"

- hosts: group2
tasks:
- name: Only run on manager
debug: msg='I'm running on manager'
when: inventory_hostname == manager2

- name: Run on all host in group
debug: msg="I'm running on all hosts"



--
Kai Stian Olstad

Aleks Przhelyaskovskiy

unread,
Oct 31, 2017, 2:10:59 PM10/31/17
to Ansible Project
 
Kai,

Thank you for your reply. Your solution is indeed clean, and simple. It will work well for the exact problem I've described.
However, I would like to explore a more generic case where one may need to run the same group of tasks /roles on N groups, where N can be between 1 and 8. Just trying to minimize redundant code.


Thanks again.

Kai Stian Olstad

unread,
Oct 31, 2017, 2:54:00 PM10/31/17
to ansible...@googlegroups.com
Something like this


playbook.yml
---
- hosts: group{{ g_nr }}
tasks:
- name: Only run on manager
debug: msg='I'm running on manager'
when: inventory_hostname == "manager" ~ g_nr

- name: Run on all host in group
debug: msg="I'm running on all hosts"


ansible-playbook playbook.tml -e g_nr=1


--
Kai Stian Olstad

Aleks Przhelyaskovskiy

unread,
Oct 31, 2017, 2:59:59 PM10/31/17
to Ansible Project

Kai,

Thank you very much. 
Reply all
Reply to author
Forward
0 new messages