首页 > 解决方案 > 在 ansible 中执行任务时使用 ansible 事实

问题描述

我正在根据通过 ansible 收集的事实值执行下面的 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'

通过 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/python@3.9/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}"

如果您看到 ansible 其存储实例标签事实抓取的上述事实;当标签ha具有活动值但失败时,我正在尝试执行任务 。您能否让我知道如何根据存储的事实值执行任务。

标签: ansible

解决方案


正如文档中所指出的:

Ansible 事实是与您的远程系统相关的数据,包括操作系统、IP 地址、附加文件系统等。ansible_facts您可以在变量中访问此数据。

资料来源:https ://docs.ansible.com/ansible/latest/user_guide/playbooks_vars_facts.html#ansible-facts

所以你的任务应该是:

- 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 

既然你debug是这样完成的,你也可以使用:

when: hostvars[inventory_hostname].tags.ha == 'active'

推荐阅读