# Set variable based on group\_names

**URL:** <https://forum.ansible.com/t/set-variable-based-on-group-names/16264>\
**Category:** Ansible Project\
**Created:** [September 17, 2014, 11:42pm UTC](https://forum.ansible.com/t/set-variable-based-on-group-names/16264 "2014-09-17T23:42:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jacek](https://avatars.discourse-cdn.com/v4/letter/j/e19adc/32.png) [@Jacek](https://forum.ansible.com/u/Jacek)\
**Post date:** [September 17, 2014, 11:42pm UTC](https://forum.ansible.com/t/set-variable-based-on-group-names/16264/1 "2014-09-17T23:42:08Z")

</div>

Hi,

Perhaps it’s an easy question but I cannot find a way to answer it myself.

More in details – I have two groups in my inventory: clients (many hosts) and server (single host), and a template at the client which needs to use the server address. If it happens that a client host is also in the ‘server’ group, I wanted to set the address in the template to ‘localhost’. Otherwise, the address should point to the server address as specified in the inventory. I guess it’s quite easy to code it using jinja2 directly in the template but my template includes many places with such a reference, so I thought about first setting the server address variable, and then using this variable to evaluate the template. Is this correct way of thinking? How can I set a variable in a play before template evaluation? Otherwise, have you got any other ideas?

Cheers,  
Jacek

---

<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:** [September 18, 2014, 12:41am UTC](https://forum.ansible.com/t/set-variable-based-on-group-names/16264/2 "2014-09-18T00:41:50Z")

</div>

Hi Jacek, this should work for you:

- set\_fact: address\_variable=‘localhost’  
when: ‘server’ in group\_names
- set\_fact: address\_variable=‘server\_address\_here’  
when: ‘server’ not in group\_names
- template: …

The “group\_names” variable is a special variable that contains a list of the groups the current host is in.

---

<div class="post-metadata">

**Author:** ![Jacek](https://avatars.discourse-cdn.com/v4/letter/j/e19adc/32.png) [@Jacek](https://forum.ansible.com/u/Jacek)\
**Post date:** [September 19, 2014, 1:06pm UTC](https://forum.ansible.com/t/set-variable-based-on-group-names/16264/3 "2014-09-19T13:06:25Z")

</div>

Thanks James,

That’s nice. One thing I’m not sure about is the ‘server\_address\_here’ which you wrote. Ideally, what I need there is the name/address of the first (and the only) host in the inventory group ‘server’.  
Is there a way to do this?

Cheers,  
Jacek

W dniu czwartek, 18 września 2014 01:41:55 UTC+1 użytkownik James Cammarata napisał:

---

<div class="post-metadata">

**Author:** ![Jacek](https://avatars.discourse-cdn.com/v4/letter/j/e19adc/32.png) [@Jacek](https://forum.ansible.com/u/Jacek)\
**Post date:** [September 19, 2014, 2:59pm UTC](https://forum.ansible.com/t/set-variable-based-on-group-names/16264/4 "2014-09-19T14:59:46Z")

</div>

Ok, after a little search I’ve got it; for anyone interested:

- set\_fact: address\_variable={{ item }}  
with\_items: groups.server  
when: “‘server’ not in group\_names”

Cheers,  
Jacek

W dniu piątek, 19 września 2014 14:06:25 UTC+1 użytkownik Jacek napisał:

---

<div class="post-metadata">

**Author:** ![Tomasz\_Kontusz](https://avatars.discourse-cdn.com/v4/letter/t/bc79bd/32.png) [@Tomasz\_Kontusz](https://forum.ansible.com/u/Tomasz_Kontusz)\
**Post date:** [September 22, 2014, 7:50am UTC](https://forum.ansible.com/t/set-variable-based-on-group-names/16264/5 "2014-09-22T07:50:07Z")

</div>

I think this would be even better:

- set\_fact: address\_variable={{ groups.server[0] }}  
when: “‘server’ not in group\_names”

Looks nicer both in code and in output 😉

Jacek [jacek.cala@gmail.com](mailto:jacek.cala@gmail.com) napisał:
