# what s the use of % set with ansible

**URL:** <https://forum.ansible.com/t/what-s-the-use-of-set-with-ansible/36809>\
**Category:** Ansible Project\
**Created:** [July 6, 2022, 9:09am UTC](https://forum.ansible.com/t/what-s-the-use-of-set-with-ansible/36809 "2022-07-06T09:09:16Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![gomz\_arvind](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@gomz\_arvind](https://forum.ansible.com/u/gomz_arvind)\
**Post date:** [July 6, 2022, 9:09am UTC](https://forum.ansible.com/t/what-s-the-use-of-set-with-ansible/36809/1 "2022-07-06T09:09:16Z")

</div>

Hi team,

can i know the actual usage of % set with ansible

---

<div class="post-metadata">

**Author:** ![Akasurde](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/akasurde/32/230_2.png) [@Akasurde](https://forum.ansible.com/u/Akasurde)\
**Post date:** [July 7, 2022, 4:46am UTC](https://forum.ansible.com/t/what-s-the-use-of-set-with-ansible/36809/2 "2022-07-07T04:46:09Z")

</div>

Could you please provide more context, examples, error logs, or use case? The amount of information you provided is too little to answer anything.

---

<div class="post-metadata">

**Author:** ![gomz\_arvind](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@gomz\_arvind](https://forum.ansible.com/u/gomz_arvind)\
**Post date:** [July 7, 2022, 8:24am UTC](https://forum.ansible.com/t/what-s-the-use-of-set-with-ansible/36809/3 "2022-07-07T08:24:55Z")

</div>



---

<div class="post-metadata">

**Author:** ![Akasurde](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/akasurde/32/230_2.png) [@Akasurde](https://forum.ansible.com/u/Akasurde)\
**Post date:** [July 7, 2022, 8:34am UTC](https://forum.ansible.com/t/what-s-the-use-of-set-with-ansible/36809/4 "2022-07-07T08:34:03Z")

</div>

I am assuming you want to split a string and set the value of that to a variable, then no need to use jinja templates

```auto
---
- hosts: localhost
gather_facts: no
vars:
name: "ab:hi:je:ee"
tasks:
- name: Print variable
set_fact:
my_var: "{{ (name.split(':') | default(''))[2] }}"
- debug:
msg: "{{ my_var }}"

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [July 7, 2022, 2:32pm UTC](https://forum.ansible.com/t/what-s-the-use-of-set-with-ansible/36809/5 "2022-07-07T14:32:39Z")

</div>

To further explain, {% set var =value %} will only have an effect  
within that template, that does not create/update ansible variables.  
So while it does 'work' , you should really only use it inside a full  
jinja2 template.

---

<div class="post-metadata">

**Author:** ![gomz\_arvind](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@gomz\_arvind](https://forum.ansible.com/u/gomz_arvind)\
**Post date:** [July 7, 2022, 6:07pm UTC](https://forum.ansible.com/t/what-s-the-use-of-set-with-ansible/36809/6 "2022-07-07T18:07:16Z")

</div>

@abhijeet and @brian …thanks for ur responses

@abhijeet- I was trying to remove a nth element from the array…so I have tried with popup

@brian : this is my understanding from ur tips…correct me if I am wrong  
{% set var =value %}  
should we consider var is used only once if we use %.?

if we use {{ set var = value }} jinja syntax, we can use it through the playbook.

can u plz give me examples for both ?

---

<div class="post-metadata">

**Author:** ![Akasurde](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/akasurde/32/230_2.png) [@Akasurde](https://forum.ansible.com/u/Akasurde)\
**Post date:** [July 8, 2022, 7:18am UTC](https://forum.ansible.com/t/what-s-the-use-of-set-with-ansible/36809/7 "2022-07-08T07:18:14Z")

</div>

You can remove nth element like - like in following case I am removing 2nd element

- name: Print variable  
set\_fact:  
my\_new\_list: |  
{% set result = name.split(‘:’) %}  
{% set \_ = result.pop(2) %}  
{{ result }}

---

<div class="post-metadata">

**Author:** ![Akasurde](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/akasurde/32/230_2.png) [@Akasurde](https://forum.ansible.com/u/Akasurde)\
**Post date:** [July 8, 2022, 8:36am UTC](https://forum.ansible.com/t/what-s-the-use-of-set-with-ansible/36809/8 "2022-07-08T08:36:15Z")

</div>

Another easy solution - but you need to know value to drop before hand

- name: Print variable  
set\_fact:  
new\_name: “{{ name.split(‘:’) | reject(‘search’, ‘hi’) }}”  
vars:  
name: “ab:hi:je:et”
