Syntax to access item in with_sequence in a complex variable

266 views
Skip to first unread message

bonheu...@gmail.com

unread,
Nov 27, 2016, 12:54:54 AM11/27/16
to Ansible Project
Hi everyone

I'm trying to use the item value in 'with_sequence' to access an element in my array variable.

I understand that to access variables using the bracket syntax [...] in
 - vars => use vars[...]
 - hostvars => use hostvars[...]

But how to access an item in with_sequence using the bracket syntax [...]?

---
- name: Stage devices
  hosts
: localhost
  gather_facts
: false
  connection
: local
  vars
:
   
##Path to scripts
    script_path
: ~/ansible/playbooks
   
##Path to generated config files
    config_path
: ~/ansible/playbooks/configs  
   
##Prefix for the Management
    mgmt_prefix_ip
: 172.30.100
   
##First IP address
    mgmt_start_ip
: 20

    username
: admin

    password
: password
    device
:
     
- { hostname: host1 }
     
- { hostname: host2 }


  tasks
:
   
- name: Paste configuration template
      vars
:
      command
: "{{ script_path }}/send-commands.exp {{ mgmt_prefix_ip }}.{{ item | int + vars['mgmt_start_ip'] }} {{ username }} {{ password }} {{ config_path }}/{{ vars['device'][item???]['hostname'] }}.txt"



      with_sequence
: start=0 end=1 format=%02x

     
register: command_result
     
   
- debug: var=command_result


Expected behaviour is 
  "item": "00" => {{ vars['device'][00]['hostname'] }} => host1 =>  command: ~/ansible/playbooks/send-commands.exp 172.30.100.20 admin password ~/ansible/playbooks/configs}/host1.txt"
  "item": "01" => {{ vars['device'][01]['hostname'] }} => host2 =>  command: ~/ansible/playbooks/send-commands.exp 172.30.100.21 admin password ~/ansible/playbooks/configs}/host2.txt"


I've set the index value manually (e.g. vars['device'][00]['hostname']) and it works but I want the index to be dynamic.


I've looked at the documentation but couldn't find the magic variable name.


Can you please help? I'm still a newbie.


Thanks.





bonheu...@gmail.com

unread,
Nov 27, 2016, 1:06:46 AM11/27/16
to Ansible Project
Ok I just found the right syntax... [item | int]

      command: "{{ script_path }}/send-commands.exp {{ mgmt_prefix_ip }}.{{ item | int + vars['mgmt_start_ip'] }} {{ username }} {{ password }} {{ config_path }}/{{ vars['device'][item | int]['hostname'] }}.txt"
Reply all
Reply to author
Forward
0 new messages