Added some examples of accessing nested variables

This commit is contained in:
Vlad R 2023-08-14 07:51:55 +00:00
parent 128fb50e36
commit 5d8d76fa5c
3 changed files with 97 additions and 5 deletions

View File

@ -9,11 +9,11 @@
ansible.builtin.shell: "cd /var/log; find . -maxdepth 1 -type f | cut -d'/' -f2"
register: files_to_copy
- name: Copy the log files
ansible.builtin.fetch:
src: /var/log/{{ item }}
dest: /home/vlad/Desktop/ansible/dnaclab_linux/logs/
with_items: "{{ files_to_copy.stdout_lines }}"
# - name: Copy the log files
# ansible.builtin.fetch:
# src: /var/log/{{ item }}
# dest: /home/vlad/Desktop/ansible/dnaclab_linux/logs/
# with_items: "{{ files_to_copy.stdout_lines }}"

7
testing/inner.yml Normal file
View File

@ -0,0 +1,7 @@
- name: Print outer and inner items
ansible.builtin.debug:
msg: "outer item={{ outer_item }} inner item={{ item }}"
loop:
- a
- b
- c

85
testing/variable-test.yml Normal file
View File

@ -0,0 +1,85 @@
- name: Testing how to access variables
hosts: localhost
gather_facts: false
vars:
test_1:
- name: vlan 20
id: 20
svi_ip_peer_1: 192.168.77.1/24
svi_ip_peer_2: 192.168.77.2/24
portchannel_id: 20
portchannel_interfaces_peer_1:
name: port-channel20
members:
- Eth1/5
- Eth1/6
mode: on
switchport_mode: access
portchannel_interfaces_peer_2:
name: port-channel20
members:
- Eth1/5
- Eth1/6
mode: on
switchport_mode: access
hsrp_group: 20
hsrp_vip: 192.168.77.254
hsrp_preempt: enabled
hsrp_priority_peer_1: 105
hsrp_priority_peer_2: 100
- name: vlan 30
id: 20
svi_ip_peer_1: 192.168.77.1/24
svi_ip_peer_2: 192.168.77.2/24
portchannel_id: 20
portchannel_interfaces_peer_1:
name: port-channel20
members:
- Eth1/7
- Eth1/8
mode: on
switchport_mode: access
portchannel_interfaces_peer_2:
name: port-channel20
members:
- Eth1/7
- Eth1/8
mode: on
switchport_mode: access
hsrp_group: 20
hsrp_vip: 192.168.77.254
hsrp_preempt: enabled
hsrp_priority_peer_1: 105
hsrp_priority_peer_2: 100
test_2: "{{ test_1 | map(attribute='portchannel_interfaces_peer_1') | map(attribute='members') }}"
tasks:
- name: Print var
ansible.builtin.debug:
var: test_2
- name: Print "portchannel_interfaces_peer_1"
ansible.builtin.debug:
msg: "{{ item }}"
loop:
"{{ test_1 | map(attribute='portchannel_interfaces_peer_1')}}"
- name: Print "members"
ansible.builtin.debug:
msg: "{{ item }}"
loop:
"{{ test_1 | map(attribute='portchannel_interfaces_peer_1') | map(attribute='members')}}"
# - name: Set members peer 1
# ansible.builtin.set_fact:
# peer_1_vpc_interfaces: "{{ item }}"
# cacheable: yes
# loop:
# "{{ test_1 | map(attribute='portchannel_interfaces_peer_1') | map(attribute='members') }}"
- name: Print member interfaces
ansible.builtin.debug:
msg: "{{ item[1] }}"
loop:
"{{ test_1 | map(attribute='portchannel_interfaces_peer_1') | subelements('members') }}"