Reputation: 71
I am executing below ansible task based on facts value gathered via ansible.
- hosts : mirth1
vars :
env : dev
gather_facts : true
tasks:
#- name: Build corresponding JSON files
- name: Build a Stream
shell: uptime
when: tags['ha'] == 'active'
Facts value gathered via ansible:
"hostvars[inventory_hostname]": "{'httpd_port': 80, 'ntpserver': '192.168.1.2', 'ansible_user': 'ec2-user', 'inventory_file': '/Users/bada/Documents/we-ansible/inventory_awsplugin/test', 'inventory_dir': '/Users/bada/Documents/we-ansible/inventory_awsplugin/test', 'ami_launch_index': 0, 'image_id': 'ami-0915bcb5fa654992', 'instance_id': 'i-06bd5115d656789a9', 'instance_type': 't3.small', 'key_name': 'mykey', 'launch_time': datetime.datetime(2021, 3, 5, 5, 44, 35, tzinfo=tzutc()), 'monitoring': {'state': 'disabled'}, 'placement': {'availability_zone': 'us-east-1a', 'group_name': '', 'region': 'us-east-1'}, 'private_dns_name': 'ip-172-12-16-224.ec2.internal', 'private_ip_address': '172.12.16.224', 'state': {'code': 16, 'name': 'running'}, 'subnet_id': 'subnet-04fcfc6', 'vpc_id': 'vpc-0cf0a45', 'architecture': 'x86_64', 'block_device_mappings': [{'device_name': '/dev/xvda', 'ebs': {'attach_time': datetime.datetime(2021, 3, 5, 5, 44, 36, tzinfo=tzutc()), 'delete_on_termination': True, 'status': 'attached', 'volume_id': 'vol-057912c770df38754'}}], 'client_token': 'a0ce63e5', 'ebs_optimized': False, 'ena_support': True, 'hypervisor': 'xen', 'network_interfaces': [{'attachment': {'attach_time': datetime.datetime(2021, 3, 5, 5, 44, 35, tzinfo=tzutc()), 'attachment_id': 'eni-attach-03fc486b4c06970ce', 'delete_on_termination': True, 'device_index': 0, 'status': 'attached', 'network_card_index': 0}, 'description': '', 'groups': [{'group_name': 'sg_priv_vce_test', 'group_id': 'sg-0b89c5'}], 'ipv6_addresses': [], 'mac_address': '12:0d:44:15:55:a9', 'network_interface_id': 'eni-0772a53', 'owner_id': '58435', 'private_dns_name': 'ip-172-16-12-224.ec2.internal', 'private_ip_address': '172.16.12.224', 'private_ip_addresses': [{'primary': True, 'private_dns_name': 'ip-172-16-12-224.ec2.internal', 'private_ip_address': '172.16.12.224'}], 'source_dest_check': True, 'status': 'in-use', 'subnet_id': 'subnet-04fcfc42cda7cdaa6', 'vpc_id': 'vpc-0cf0a4dded14c2f05', 'interface_type': 'interface'}], 'root_device_name': '/dev/xvda', 'root_device_type': 'ebs', 'security_groups': [{'group_name': 'sg_priv_test', 'group_id': 'sg-0b8b36c5'}], 'source_dest_check': True, 'tags': {'ha': 'active', 'Platform': 'linux', 'Role': 'Mirth', 'Environment': 'test', 'Name': 'we-test-mirth1', 'PrincipalId': 'AIDAIXY2CRBZU5K', 'Owner': 'we-ansible', 'Product': 'we'}, 'virtualization_type': 'hvm', 'cpu_options': {'core_count': 1, 'threads_per_core': 2}, 'capacity_reservation_specification': {'capacity_reservation_preference': 'open'}, 'hibernation_options': {'configured': False}, 'metadata_options': {'state': 'applied', 'http_tokens': 'optional', 'http_put_response_hop_limit': 1, }, 'enclave_options': {'enabled': False}, 'inventory_hostname': '172.16.12.224', 'group_names': ['aws_ec2', 'linux', 'mirthlin_servers'], 'ansible_facts': {}, 'playbook_dir': '/Users/bada/Docs/we-ansible', 'ansible_playbook_python': '/usr/local/opt/[email protected]/bin/python3.9', 'ansible_config_file': '/Users/bada/Documents/vce-ansible/ansible.cfg', 'groups': {'all': ['172.16.13.21','mirth_servers': ['172.16.12.224']}, 'omit': '__omit_place_holder__6af09f538bad60133ef3eb0949ec09272254aad1', 'ansible_version': {'string': '2.10.3', 'full': '2.10.3', 'major': 2, 'minor': 10, 'revision': 3}, 'ansible_check_mode': False, 'ansible_diff_mode': False, 'ansible_forks': 5, 'ansible_inventory_sources': ['/Users/bada/Documents/vce-ansible/inventory_awsplugin/test'], 'ansible_verbosity': 0}"
If you see above facts grabbed by ansible its storing instance tag fact; I am trying to execute a task when a tag ha has value as active but its been failing. Could you let me know how can I execute task based on fact value stored.
Upvotes: 2
Views: 460
Reputation: 39099
As pointed in the documentation:
Ansible facts are data related to your remote systems, including operating systems, IP addresses, attached filesystems, and more. You can access this data in the
ansible_facts
variable.
Source: https://docs.ansible.com/ansible/latest/user_guide/playbooks_vars_facts.html#ansible-facts
So your taks should be:
- shell: uptime
when: ansible_facts.tags['ha'] == 'active'
## or ansible_facts.tags.ha
## or ansible_facts['tags']['ha']
## if you want to keep them coherent
And since your debug
was done this way you could also have used:
when: hostvars[inventory_hostname].tags.ha == 'active'
Upvotes: 1