what s the use of % set with ansible

52 views
Skip to first unread message

gomz arvind

unread,
Jul 6, 2022, 5:09:16 AM7/6/22
to Ansible Project
Hi team,

can i know the actual usage of % set with ansible

Abhijeet Kasurde

unread,
Jul 7, 2022, 12:46:42 AM7/7/22
to ansible...@googlegroups.com
Could you please provide more context, examples, error logs, or use case? The amount of information you provided is too little to answer anything.

On Wed, Jul 6, 2022 at 2:39 PM gomz arvind <gomska...@gmail.com> wrote:
Hi team,

can i know the actual usage of % set with ansible

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/ab46e6f4-f7bc-4ea9-a2b6-44acd3a1a41dn%40googlegroups.com.


--
Thanks,
Abhijeet Kasurde

gomz arvind

unread,
Jul 7, 2022, 4:24:56 AM7/7/22
to Ansible Project
---
 - name: Cisco NXOS
   hosts: all
   connection: network_cli
   gather_facts: false

   vars:

     - cmdlist: sh clock
     - ansible_python_interpreter: /usr/bin/python3
     - ansible_network_os: nxos
     - project:

   tasks:

     - name: Execute command
       nxos_command:
        commands: "{{ cmdlist }}"
       register: output

     - set_fact:
         arr: "{{ output.stdout_lines[0][1].split() }}"

     - debug:
          msg: "{{ arr[0] }}"

     - name: print variable
       set_fact:
         my_string_var: "{{ arr[0].split(':') | default('') }}"

     - debug:
       #  msg: "{{my_string_var.pop(2)}}"
         msg: |
              {% set project = my_string_var.pop(2) %}
plz find the above example

Abhijeet Kasurde

unread,
Jul 7, 2022, 4:34:39 AM7/7/22
to ansible...@googlegroups.com
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

```
---
- 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 }}"
```



--
Thanks,
Abhijeet Kasurde

Brian Coca

unread,
Jul 7, 2022, 10:34:00 AM7/7/22
to Ansible Project
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.


--
----------
Brian Coca

gomz arvind

unread,
Jul 7, 2022, 2:07:17 PM7/7/22
to Ansible Project
@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 ?

Abhijeet Kasurde

unread,
Jul 8, 2022, 3:18:41 AM7/8/22
to ansible...@googlegroups.com
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 }}



--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.


--
Thanks,
Abhijeet Kasurde

Abhijeet Kasurde

unread,
Jul 8, 2022, 4:36:41 AM7/8/22
to ansible...@googlegroups.com
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"
--
Thanks,
Abhijeet Kasurde
Reply all
Reply to author
Forward
0 new messages