Search code examples
ansibleyamljinja2

Ansible get index list


I m using ansible to create a playbook to install my project in terms of prompt parameters.

I can install 2 applications with this script, I prompt which one at the beginning and then I need to get good ssh port because applications have different port.

ssh_smx_ports:
  - pds: 8103
  - activemq: 8102

when script is started:

vars_prompt:
- name: "type_smx"
  prompt: "cible? (pds,activemq)"
  private: no

after I need to create pass this port to another command:

 name: install bundle on {{ type_smx }}
 tags: ssh
 command: sudo /servicemix/servicemix/bin/client -h {{ inventory_hostname }} -a {{ ssh_smx_ports[type_smx]  }} -u smx -p {{ ssh_smx_pass }} 'install mybundle'

but with this I got this error:

fatal: []: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'list object' has no attribute u'pds'\n\nThe error appears to have been in '/home/ansible/env/install_version.yml': line 60, column 6, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n - "{{ type_smx }}"\n - name: Add url on servicemix {{ type_smx }}\n ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n with_items:\n - {{ foo }}\n\nShould be written as:\n\n with_items:\n - "{{ foo }}"\n"}

how can I access to the correct port element?


Solution

  • You defined a list with two elements:

    ssh_smx_ports:
      - pds: 8103
      - activemq: 8102
    

    You refer to it with ssh_smx_ports[type_smx] like it was a dictionary:

    ssh_smx_ports:
      pds: 8103
      activemq: 8102