i use 3 separate values of user input combine variable defined, , value of defined variable.
current playbook:
- hosts: remote_user: root gather_facts: true vars: # lab lab_bau_la1: "10.1.2.1" lab_dfw_aws: "10.1.3.1" lab_rt_la1: "10.1.4.1" # production prod_bau_lap: "10.12.2.1" prod_dfw_la2: "10.12.3.1" prod_rt_aws: "10.12.4.1" vars_prompt: - name: env prompt: "production or lab? input options: prod/lab" private: no - name: system prompt: "bau or realtime or dfw system? input options: bau/rt/dfw" private: no - name: location prompt: "location of system? input options: aws/lap/la1/la2" private: no tasks: - name: test connectivity wait_for: host={{ vars[{{ vars.env }}_{{ vars.system }}_{{ vars.location }}] }} port=80 delay=0 timeout=10 state=started
the above not work. here output of playbook:
[root@ansmgt]# ansible-playbook /etc/ansible/playbooks/test.yaml -i /tmp/test -vvv using /etc/ansible/ansible.cfg config file ssh password: 1 plays in /etc/ansible/playbooks/test.yaml new host production or lab? input options: prod/lab: prod new host bau or realtime or dfw system? input options: bau/rt/dfw: bau location of server? input options: crbc/labc/aws/crp/lap/njp/vap/mssn/ffld/la1: lap play *************************************************************************** task [setup] ******************************************************************* ok: [kicktest01.me.net] task [test connectivity] ***************************** fatal: [kicktest01.me.net]: failed! => {"failed": true, "msg": "error! template error while templating string: expected token ':', got '}'"} play recap ********************************************************************* kicktest01.me.net : ok=2 changed=1 unreachable=0 failed=1
i know how can combine user input, , specific value defined variables.
in specific case, user input is: prod
, bau
, lap
. combining 3 prod_bau_lap
, looking actual value of 10.12.2.1
.
i suggest defining dynamic variable make code cleaner.
add vars
:
destination_host: "{{ env }}_{{ system }}_{{ location }}"
use in task (i'm not fan of ansible notation, changed syntax yaml):
- name: test connectivity wait_for: host: "{{ vars[destination_host] }}" port: 80 delay: 0 timeout: 10 state: started
Comments
Post a Comment