ansible junos.device collection how to get all diff on a loop

73 views
Skip to first unread message

hrv231

unread,
Jun 1, 2021, 1:08:20 PM6/1/21
to Junos Python EZ

Hello,

I'm using the junos.device collection to run a playbook to create a .patch file for each device.
The problem that I have is that the 'dest_dir' setting only save the last item on the loop.

Below is my playbook:

    - name: TEST1
      juniper.device.config:
        # config_mode: private
        # retrieve: 'candidate'
        diff: true
        check: true
        commit: false
        # format: set
        load: set
        # dest_dir: /home/user1/ansible/ansible/files/
        lines:
          - set interfaces xe-0/2/3 vlan-tagging
          - set interfaces xe-1/2/3 vlan-tagging
          - set interfaces xe-0/2/3 unit {{item.value.number}} vlan-id {{item.value.number}}
          - set interfaces xe-0/2/3 unit {{item.value.number}} family inet address 100.100.{{number}}.{{item.value.address}}/30
          - set interfaces xe-1/2/3 unit {{item.value.number}} vlan-id {{item.value.number}}
          - set interfaces xe-1/2/3 unit {{item.value.number}} family inet address 100.101.{{number}}.{{item.value.address}}/30
          - set routing-instances {{item.key}} instance-type virtual-router
          - set routing-instances {{item.key}} interface xe-0/2/3.{{item.value.number}}
          - set routing-instances {{item.key}} interface xe-1/2/3.{{item.value.number}}
          - set routing-instances {{item.key}} protocols ospf area 0.0.0.0 interface xe-1/2/3.{{item.value.number}}
          - set routing-instances {{item.key}} protocols ospf area 0.0.0.0 interface xe-0/2/3.{{item.value.number}}
      loop: "{{vrf_data.name|dict2items}}"
      register: response
     
     
     
If I filter my response to only get the diff.prepared , then I get the result in a single line and with the "\n" on it.

Can someone please help me out with this?

This is the file output:

["\n[edit interfaces]\n+   xe-0/2/3 {\n+       vlan-tagging;\n+       unit 507 {\n+           vlan-id 507;\n+           family inet {\n+               address 100.100.222.26/30;\n+           }\n+       }\n+   }\n+   xe-1/2/3 {\n+       vlan-tagging;\n+       unit 507 {\n+           vlan-id 507;\n+           family inet {\n+               address 100.101.222.26/30;\n+           }\n+       }\n+   }\n[edit routing-instances]\n+   FACILITIES {\n+       instance-type virtual-router;\n+       interface xe-0/2/3.507;\n+       interface xe-1/2/3.507;\n+       protocols {\n+           ospf {\n+               area 0.0.0.0 {\n+                   interface xe-1/2/3.507;\n+                   interface xe-0/2/3.507;\n+               }\n+           }\n+       }\n+   }\n", "\n[edit interfaces]\n+   xe-0/2/3 {\n+       vlan-tagging;\n+       unit 506 {\n+           vlan-id 506;\n+           family inet {\n+               address 100.100.222.22/30;\n+           }\n+       }\n+   }\n+   xe-1/2/3 {\n+       vlan-tagging;\n+       unit 506 {\n+           vlan-id 506;\n+           family inet {\n+               address 100.101.222.22/30;\n+           }\n+       }\n+   }\n[edit routing-instances]\n+   VIDEO {\n+       instance-type virtual-router;\n+       interface xe-0/2/3.506;\n+       interface xe-1/2/3.506;\n+       protocols {\n+           ospf {\n+               area 0.0.0.0 {\n+                   interface xe-1/2/3.506;\n+                   interface xe-0/2/3.506;\n+               }\n+           }\n+       }\n+   }\n", "\n[edit interfaces]\n+   xe-0/2/3 {\n+       vlan-tagging;\n+       unit 505 {\n+           vlan-id 505;\n+           family inet {\n+               address 100.100.222.18/30;\n+           }\n+       }\n+   }\n+   xe-1/2/3 {\n+       vlan-tagging;\n+       unit 505 {\n+           vlan-id 505;\n+           family inet {\n+               address 100.101.222.18/30;\n+           }\n+       }\n+   }\n[edit routing-instances]\n+   VOICE {\n+       instance-type virtual-router;\n+       interface xe-0/2/3.505;\n+       interface xe-1/2/3.505;\n+       protocols {\n+           ospf {\n+               area 0.0.0.0 {\n+                   interface xe-1/2/3.505;\n+                   interface xe-0/2/3.505;\n+               }\n+           }\n+       }\n+   }\n", "\n[edit interfaces]\n+   xe-0/2/3 {\n+       vlan-tagging;\n+       unit 504 {\n+           vlan-id 504;\n+           family inet {\n+               address 100.100.222.14/30;\n+           }\n+       }\n+   }\n+   xe-1/2/3 {\n+       vlan-tagging;\n+       unit 504 {\n+           vlan-id 504;\n+           family inet {\n+               address 100.101.222.14/30;\n+           }\n+       }\n+   }\n[edit routing-instances]\n+   TEST {\n+       instance-type virtual-router;\n+       interface xe-0/2/3.504;\n+       interface xe-1/2/3.504;\n+       protocols {\n+           ospf {\n+               area 0.0.0.0 {\n+                   interface xe-1/2/3.504;\n+                   interface xe-0/2/3.504;\n+               }\n+           }\n+       }\n+   }\n"]



I would like something with the result I get when I use the dest_dir folder, but with all my changes on the same {{inventory_hostname}}.diff, example:


[edit interfaces xe-0/2/3]
+   vlan-tagging;
[edit interfaces xe-0/2/3]
+    unit 504 {
+        vlan-id 504;
+        family inet {
+            address 100.100.222.14/30;
+        }
+    }
[edit interfaces]
+   xe-1/2/3 {
+       vlan-tagging;
+       unit 504 {
+           vlan-id 504;
+           family inet {
+               address 100.101.222.14/30;
+           }
+       }
+   }
[edit routing-instances]
+   TEST {
+       instance-type virtual-router;
+       interface xe-0/2/3.504;
+       interface xe-1/2/3.504;
+       protocols {
+           ospf {
+               area 0.0.0.0 {
+                   interface xe-1/2/3.504;
+                   interface xe-0/2/3.504;
+               }
+           }
+       }
+   }


Thanks.

Rahul Kumar

unread,
Jun 10, 2021, 2:44:22 AM6/10/21
to Junos Python EZ, hrv231
Hi, 

As it is in loop, it creates a file in each loop and then overwrites it in next iteration. You can try multiple solutions but will have to put some extra effort. 

1) Create list of directories with as many items you have in the loop. Modify the dest_dir path to something like dest_dir: /home/user1/ansible/ansible/files/{{item.value.number}} . This will write the output in different folders for each loop iteration. You can then merge all of them. 

2) If you add another task below your current task "TEST1" 
    - name: "Print Response"
      debug:
#        var: response.results[0].diff_lines
        var: response
This variable response has the complete list of changes, but you may need to iterate it properly to get desired values. 

Regards
Rahul kumar


From: 'hrv231' via Junos Python EZ <junos-p...@googlegroups.com>
Sent: 01 June 2021 22:38
To: Junos Python EZ <junos-p...@googlegroups.com>
Subject: ansible junos.device collection how to get all diff on a loop
 

[External Email. Be cautious of content]

--
You received this message because you are subscribed to the Google Groups "Junos Python EZ" group.
To unsubscribe from this group and stop receiving emails from it, send an email to junos-python-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/junos-python-ez/d1a40fbd-62bf-41af-8c39-52eed678fb8an%40googlegroups.com.

Juniper Business Use Only

Reply all
Reply to author
Forward
0 new messages