amazon ec2 - Ansible conditional with pattern match using variables -
i using ansible roles provision aws ec2 instances, have created series of task in ec2 roles gets execute each server.
- name: provisioning ec2 instance ec2: region: "{{ region }}" key_name: "{{ key }}" instance_type: "{{ instance_type }}" image: "{{ ami }}" wait: yes assign_public_ip: yes group_id: "{{ sgs }}" vpc_subnet_id: "{{ publicsubnet }}" source_dest_check: false instance_tags: '{ "name": "{{ server_name }}", "environment": "{{ tag_env }}" }' register: instance_info - name: storing instance information in {{ server_name }}_info file shell: echo "{{ host_name }}:" " {{ item.public_ip }}"> group_vars/"{{ server_name }}"_info with_items: instance_info.instances when: 'nat in {{ server_name }}' <<=== here - name: add server inventory add_host: hostname={{ item.public_ip }} groupname={{ host_name }} with_items: instance_info.instances when: "'nat' not in {{ server_name }}" - name: waiting server come wait_for: host: "{{ item.public_ip }}" port: 22 delay: 5 timeout: 300 with_items: instance_info.instances
so want check if server has prefix nat last 3 task should skipped doesn't make sense execute them. cannot direct comparison other postfix data added {{ server_name }} depending upon environment, time , other details. can provide me information of how can achieve this. thank you
this should work. basically, set flag, no need use jinja in when
. i'm using set_fact
twice in reality you'd create is_nat=false var elsewhere.
- set_fact: is_nat=false - set_fact: is_nat=true when: "'nat' in server_name" - name: add server inventory add_host: hostname={{ item.public_ip }} groupname={{ host_name }} with_items: instance_info.instances when: not is_nat
here's example of in playbook, , execution runs various server_names.
$ cat compare.yml --- - hosts: localhost connection: local vars: is_nat: false tasks: - set_fact: is_nat=true when: "'nat' in server_name" - debug: msg="run when not nat" when: not is_nat
.
$ ansible-playbook -i "localhost," compare.yml -e "server_name=gnat" play [localhost] ************************************************************** gathering facts *************************************************************** ok: [localhost] task: [set_fact is_nat=true] ************************************************** ok: [localhost] task: [debug msg="run when not nat"] ************************************* skipping: [localhost] play recap ******************************************************************** localhost : ok=2 changed=0 unreachable=0 failed=0
.
$ ansible-playbook -i "localhost," compare.yml -e "server_name=mosquito" play [localhost] ************************************************************** gathering facts *************************************************************** ok: [localhost] task: [set_fact is_nat=true] ************************************************** skipping: [localhost] task: [debug msg="run when not nat"] ************************************* ok: [localhost] => { "msg": "run when not nat" } play recap ******************************************************************** localhost : ok=2 changed=0 unreachable=0 failed=0
Comments
Post a Comment