# Concatenate strings using with\_items and assign to a variable in Ansible

**URL:** <https://forum.ansible.com/t/concatenate-strings-using-with-items-and-assign-to-a-variable-in-ansible/24015>\
**Category:** Ansible Project\
**Created:** [March 20, 2017, 1:37pm UTC](https://forum.ansible.com/t/concatenate-strings-using-with-items-and-assign-to-a-variable-in-ansible/24015 "2017-03-20T13:37:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![charan.sevakula](https://avatars.discourse-cdn.com/v4/letter/c/858c86/32.png) [@charan.sevakula](https://forum.ansible.com/u/charan.sevakula)\
**Post date:** [March 20, 2017, 1:37pm UTC](https://forum.ansible.com/t/concatenate-strings-using-with-items-and-assign-to-a-variable-in-ansible/24015/1 "2017-03-20T13:37:01Z")

</div>

I need to save two 2 IPs to a variable in a vars\_file when launching ec2\_instances, which are used later during deployment.

This is how I am saving a single server ip:

```auto
- name: Save server public IP to vars file
  lineinfile: line="server_public_ip{{':'}} {{ item.public_ip }}"
              dest="{{ansible_env.HOME}}/dynamic_ips_{{ec2_environment}}"
  with_items: server.instances #server is registered in previous task

```

The output I have in dynamic\_ips file is **`server_public_ip: xxx.xxx.xx.x`**

Now I have 2 servers launched and registered as servers.

I need to save this as **`server_public_ips: xxx.xx.x.xx , xxx.x.xx.x`**

I tried to declare an empty string and append ips to it, something like this, but I am getting errors.

```auto
set_fact:
   ips: ""
set_fact:
   ips: " {{ ips }} + {{ item.public_ip}} "
with_items: servers.instances #servers is registered in previous task
lineinfile: line="server_public_ips{{':'}} {{ ips }}"
            dest="{{ansible_env.HOME}}/dynamic_ips_{{ec2_environment}}"

```

I think it can be done using lineinfile insertafter and regex.

Finally, I need this to do this in a different server,

```auto
- name: Restrict access to outside world
  command: iptables INPUT {{ item }} ACCEPT
  with_items: {{ server_public_ips }}.split(,) #grant access for each ip 
  command: iptables INPUT DROP

```

---

<div class="post-metadata">

**Author:** ![Harsh\_Agarwal](https://avatars.discourse-cdn.com/v4/letter/h/a5b964/32.png) [@Harsh\_Agarwal](https://forum.ansible.com/u/Harsh_Agarwal)\
**Post date:** [April 21, 2020, 12:12am UTC](https://forum.ansible.com/t/concatenate-strings-using-with-items-and-assign-to-a-variable-in-ansible/24015/2 "2020-04-21T00:12:49Z")

</div>

why dont you…concatenate strings to a list and then join the list instead to make the string?

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [April 21, 2020, 4:39am UTC](https://forum.ansible.com/t/concatenate-strings-using-with-items-and-assign-to-a-variable-in-ansible/24015/3 "2020-04-21T04:39:37Z")

</div>

Few things come to mind when looking at your code

1. Why are you using iptables? Security groups would take away a lot of problems especially the dynamics that you seem to working around with iptables.

2. If you need to restrict access between ec2 instances, why do you use public interfaces for communication?

If you let us know what high level goal you are trying to achieve with your playbook? There might be better/easier ways to achieve it.

Dick
