# register a variable created from other variables?

**URL:** <https://forum.ansible.com/t/register-a-variable-created-from-other-variables/27298>\
**Category:** Ansible Project\
**Created:** [April 17, 2018, 6:45am UTC](https://forum.ansible.com/t/register-a-variable-created-from-other-variables/27298 "2018-04-17T06:45:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Karl\_Auer](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karl\_Auer](https://forum.ansible.com/u/Karl_Auer)\
**Post date:** [April 17, 2018, 6:45am UTC](https://forum.ansible.com/t/register-a-variable-created-from-other-variables/27298/1 "2018-04-17T06:45:52Z")

</div>

I have several roles that create various numbers of security groups.

I have another role that creates an instance.

I want the second role to be re-usable, and to be usable with the roles that create security groups.

So I want the second role to attach a list of security groups to the instance it creates.

The role that creates security groups thus has to be able to somehow return a list of security groups.

My role creates security groups and registers eg sg\_1, sg\_2, sg\_3 and so on.

How can I create a list variable? Something like sgs: [sg\_1.group\_id, sg\_2.group\_id, sg\_3.group\_id] ?

The variable cannot be created inside the role that creates the instance, as that would bind it too closely to the role that creates the security groups. It has to be created inside the role that creates the security groups.

Or is there some other way to achieve this?

Regards, K.

---

<div class="post-metadata">

**Author:** ![Karl\_Auer](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karl\_Auer](https://forum.ansible.com/u/Karl_Auer)\
**Post date:** [April 17, 2018, 7:25am UTC](https://forum.ansible.com/t/register-a-variable-created-from-other-variables/27298/2 "2018-04-17T07:25:12Z")

</div>

Further to this: I have no problem collecting the various groups into a list using ec2\_group\_facts.

What I can’t figure out how to do is get the group IDs from that list of group facts into a single variable that can be used in the ec2 module, which takes a list of group IDs in its “group\_id” attribute.

Regards, K.

---

<div class="post-metadata">

**Author:** ![Karl\_Auer](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karl\_Auer](https://forum.ansible.com/u/Karl_Auer)\
**Post date:** [April 17, 2018, 9:18am UTC](https://forum.ansible.com/t/register-a-variable-created-from-other-variables/27298/3 "2018-04-17T09:18:13Z")

</div>

Sorry - I’ve done it again; figured out a solution to my own problem AFTER posting a question.

Anyway, the answer is the set\_facts: task. This lets you build a variable from other variables.

In the role that creates security groups, the last task is a set\_fact: task that builds a list of the security group IDs. That fact (variable) is then available to the role that creates the instance, which can use it in the instance’s group\_id attribute.

For an instance that I had already created, I found a workaround by googling. The workaround was to use set\_fact: after creating the security groups to create a space-delimited string of group IDs. The playbook itself (not the server role) then has an extra task, after the instance has already been built, of running an AWS CLI command to modify the instance (“aws ec2 modify-instance-attributes …”)

Regards, K.
