# Can I slice an existing group within a play (without a unique attribute to group\_by)?

**URL:** <https://forum.ansible.com/t/can-i-slice-an-existing-group-within-a-play-without-a-unique-attribute-to-group-by/17030>\
**Category:** Ansible Project\
**Created:** [December 11, 2014, 6:47pm UTC](https://forum.ansible.com/t/can-i-slice-an-existing-group-within-a-play-without-a-unique-attribute-to-group-by/17030 "2014-12-11T18:47:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_Casey](https://avatars.discourse-cdn.com/v4/letter/m/b4bc9f/32.png) [@Mark\_Casey](https://forum.ansible.com/u/Mark_Casey)\
**Post date:** [December 11, 2014, 6:47pm UTC](https://forum.ansible.com/t/can-i-slice-an-existing-group-within-a-play-without-a-unique-attribute-to-group-by/17030/1 "2014-12-11T18:47:32Z")

</div>

Hello,

I have a group of servers in EC2 that will be a cluster of Consul server nodes, but on some of them I also want to run LDAP.

I believe in a static inventory file this would be:

[consul]  
server1  
server2  
server3

[ldap]  
server1  
server 2

I’m using the pattern from the EC2 guide of calling out to the ec2 module to create or persist/no-op the instances and then using add\_host to group them (so it does ec2/add\_host during every call to ansible-playbook). I’m passing ‘type: consul’ in to the ec2 role which ends up in some of the naming tags and being used for idempotence/exact\_count.

My question is: Where do I create the second group (‘ldap’)?

I’m kind of hoping I could slice the existing ‘consul’ group into an ldap group or start a play (within the same run of ansible-playbook) on only some of the hosts in the consul group:

- name: Create instances  
hosts: localhost  
connection: local  
roles:

- role: ec2  
type: consul  
count: 3

- name: Install Consul  
hosts: consul  
roles:

- role: consul

- name: Install LDAP  
hosts: **“A subset of ‘consul’, or a new ‘ldap’ subgroup created from a subset of ‘consul’”**  
roles:

- role: ldap

And finally… the catch.

I’m using just enough tags to name the instances and get ‘count\_tags’+‘exact\_count’ to work with the EC2 module. I’d rather not add more tags to get an ‘ldap’ group out of ec2.py because I want to be able to use this with other cloud providers (existing playbooks already work with EC2 and Rackspace). In the past I’ve found that some providers may only support one tag/group, may not allow changing groups via API, or don’t do tags/groups at all and impotence is based on the instance name alone.

I’m hoping I’m overlooking an obvious fix but right now I’m planning to do:

- name: Create instances  
hosts: localhost  
connection: local  
roles:

- role: ec2  
type: consul\_ldap  
count: 2

- role: ec2  
type: consul  
count: 1

- name: Install Consul  
hosts: **consul\_ldap:consul**  
roles:

- role: consul

- name: Install LDAP  
hosts: consul\_ldap  
roles:

- role: ldap

Thanks,  
Mark

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [December 11, 2014, 8:04pm UTC](https://forum.ansible.com/t/can-i-slice-an-existing-group-within-a-play-without-a-unique-attribute-to-group-by/17030/2 "2014-12-11T20:04:03Z")

</div>

Hi Mark, the way you’re planning is the correct way to do this, however you could use a conditional on the ldap role such that it would only run if the consul\_ldap group was in the current host’s groups:

- { role: consul\_ldap, when: “‘consul\_ldap’ is in group\_names” }

I don’t really recommend doing that, but it’s there should you decide to use it.

---

<div class="post-metadata">

**Author:** ![Mark\_Casey](https://avatars.discourse-cdn.com/v4/letter/m/b4bc9f/32.png) [@Mark\_Casey](https://forum.ansible.com/u/Mark_Casey)\
**Post date:** [May 26, 2015, 5:02pm UTC](https://forum.ansible.com/t/can-i-slice-an-existing-group-within-a-play-without-a-unique-attribute-to-group-by/17030/3 "2015-05-26T17:02:44Z")

</div>

James (and list),

Thanks for the feedback on this back when… I just got a semi-permanent solution added that I wanted to share/see if I could get any thoughts on.

I added a couple tasks in my VM provisioning Ansible roles that process a new fact named “additional\_roles” (with ‘roles’ in this context meaning “things this host does”. I need to do some term-fixing… this is using ‘role’, ‘service’, and ‘type’ fairly interchangably… but anyway)

This is the call which would set the new fact, and ‘provider’ may be ec2, rax, vagrant, or etc.:

> roles:
> 
> - role: ‘{{ provider }}/{{ provider }}\_add’  
> type: dbnode  
> additional\_roles: webnode  
> count: 1

and then the tasks within the ec2\_add role, for example ( **new tasks in bold** ):

> * * *
> 
> - name: Set up a new host instance on EC2  
> local\_action:  
> module: ec2  
> region: “{{ region }}”
> 
> … …
> 
> register: ec2
> 
> - name: Add instance(s) to group representing type of host such as webnode or dbnode  
> local\_action: add\_host hostname={{ item.private\_ip }} ansible\_ssh\_host={{ item.private\_ip }} groupname={{ type }}  
> with\_items: ec2.tagged\_instances  
> when: wait == “yes”
> 
> - name: Add instance(s) to combo service\_type group, such as prod\_dbnode, or staging\_webnode  
> local\_action: add\_host hostname={{ item.private\_ip }} ansible\_ssh\_host={{ item.private\_ip }} groupname=“{{ service }}\_{{ type }}”  
> with\_items: ec2.tagged\_instances  
> when: wait == “yes”
> 
> - name: Add instance(s) to group representing type of host such as webnode or dbnode  
> local\_action: add\_host hostname={{ item[0].private\_ip }} ansible\_ssh\_host={{ item[0].private\_ip }} groupname={{ item[1] }}  
> with\_nested:
> 
> - ec2.tagged\_instances
> 
> - additional\_roles  
> when: wait == “yes”
> 
> - name: Add instance(s) to combo service\_type group, such as prod\_dbnode, or staging\_webnode  
> local\_action: add\_host hostname={{ item[0].private\_ip }} ansible\_ssh\_host={{ item[0].private\_ip }} groupname=“{{ service }}\_{{ item[1] }}”  
> with\_nested:
> 
> - ec2.tagged\_instances
> 
> - additional\_roles  
> when: wait == “yes”

So, the method for my original goal of 3 consul nodes and 2 ldap nodes would be:

> roles:
> 
> - role: ‘{{ provider }}/{{ provider }}\_add’  
> type: consul  
> additional\_roles: ldap  
> count: 2
> 
> roles:
> 
> - role: ‘{{ provider }}/{{ provider }}\_add’  
> type: consul  
> count: 1

Thanks,  
Mark

---

<div class="post-metadata">

**Author:** ![Mark\_Casey](https://avatars.discourse-cdn.com/v4/letter/m/b4bc9f/32.png) [@Mark\_Casey](https://forum.ansible.com/u/Mark_Casey)\
**Post date:** [May 26, 2015, 5:04pm UTC](https://forum.ansible.com/t/can-i-slice-an-existing-group-within-a-play-without-a-unique-attribute-to-group-by/17030/4 "2015-05-26T17:04:06Z")

</div>

I forgot to bold the new tasks after I said they’d be bold… In case it isn’t very obvious they are the only two shown that use ‘with\_nested:’
