Search code examples
dictionaryansiblejinja2prompt

Ansible loop on var prompt


I would like to create several node on my bigip. For that I want to do a loop on my var prompt and register each value in my variable {{node_list}}.

This is what I've tried


 - name: node creation
   hosts: F5
   gather_facts: no
   connection: local

   vars_prompt:

     ## ASK NUMBER OF NODES
   - name: node_nb
     prompt: "number of nodes"
     private: no


     ##  ASK THE NAME AND IP WITH FORMAT NAME;IP
   - name: node_list
     prompt: "name and Ip of the node like that toto;1.1.1.1"
     private: no
     with_sequence: count={{ node_nb | int }}


   - name: Create node
     bigip_node:
       user:  '{{ ansible_user }}'
       password: '{{ ansible_password }}'
       server: 'xxxxx'
       host: '{{ (item).split(";")[1] }}'
       name: '{{ (item).split(";")[0] }}'
       partition: 'Common'
       state: present
       validate_certs: false
     with_items: '{{ node_list }}'
  • First : My var prompt don't loop if for example I specify "4" in {{ node_nb }}. The question is prompt one time but I want 4 times.

  • Second: I would register all informations of the value in input each time in a list. If I want 4 nodes I need to have 4 items in my list


Solution

  • Just have them enter the list separated by spaces, since you are already using ; to separate node names from IPs, and it additionally saves you the trouble of having to prompt for the count because the count will be however many items there are in the list