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"

On Sunday, November 27, 2016 at 4:54:54 PM UTC+11, [email protected] 
wrote:
>
> 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.
>
> https://docs.ansible.com/ansible/faq.html#how-do-i-access-a-variable-name-programmatically
>  
> http://docs.ansible.com/ansible/playbooks_variables.html
>
>
> Can you please help? I'm still a newbie.
>
>
> Thanks.
>
>
>
>
>
>

-- 
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/bac9dcc1-6def-4331-805a-ca05ac19b546%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to