The registered new_ec2.instances has a tags dictionary key so item.tags.Name 
should reference it in a loop on new_ec2.instances. I assume domain[fqdn] is 
some other dictionary set somewhere else.

"tags": {"Application Group": "CORE", "Application System": "Development Team", 
"Backupset": "true", "CST Start/Stop Schedule": "0700;1900;Weekdays", 
"CostCenter": "Dev", "Description": "Evaluation Server", "Environment": "DEV", 
"Name": "test8", "OS": "RHEL", "OS Version": "8", "Owner": "Development 
Center", "Patch Group": "Normal", "Sub Environment": "Development", "Type": 
"Eval"}

     - name: 'Update system name'
       shell: >
         hostnamectl set-hostname
         {{ item.tags.Name }}.{{ domain['fqdn'] }} --static --transient &&
         hostnamectl set-hostname {{ tags.Name }}.{{ domain['fqdn'] }} --pretty
        loop: "{{ new_ec2.instances }}"


Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM

On Aug 22, 2022, at 1:42 PM, Carmen 
<[email protected]<mailto:[email protected]>> wrote:

Hi Walter,

Thanks for the reply! Yes, that's correct. How can I reference the tags inside 
the new_ec2_instances?
Specifically, I want to pull the tag called Name to use in this next set of 
play once the new instance is launched and up and running. Below is where i 
expect to use the name tag to set the hostname on the server:

  - name: 'Run post-launch baseline configuration on instance'
    hosts: 'launched'
    gather_facts: no
    become: yes

    tasks:

     - name: 'Update system name'
       shell: >
         hostnamectl set-hostname
         {{ tags.Name }}.{{ domain['fqdn'] }} --static --transient &&
         hostnamectl set-hostname {{ tags.Name }}.{{ domain['fqdn'] }} --pretty




Here's more of the output results, if needed:

TASK [Launch instance] 
******************************************************************************************************************************************************************
task path: /etc/ansible/playbooks/rhel-8-build.yml:96
redirecting (type: modules) ansible.builtin.ec2_instance to 
community.aws.ec2_instance
redirecting (type: modules) community.aws.ec2_instance to 
amazon.aws.ec2_instance
[DEPRECATION WARNING]: The purge_tags parameter currently defaults to False. 
For consistency across the collection, this default value will change to True 
in release 5.0.0. This
feature will be removed from amazon.aws in version 5.0.0. Deprecation warnings 
can be disabled by setting deprecation_warnings=False in ansible.cfg.
changed: [127.0.0.1] => {"changed": true, "instance_ids": ["i-005f7ce"], 
"instances": [{"ami_launch_index": 0, "architecture": "x86_64", 
"block_device_mappings": [{"device_name": "/dev/sda1", "ebs": {"attach_time": 
"2022-08-22T16:44:38+00:00", "delete_on_termination": false, "status": 
"attaching", "volume_id": "vol-0488a0"}}], 
"capacity_reservation_specification": {"capacity_reservation_preference": 
"open"}, "client_token": "64c1d4b064f", "cpu_options": {"core_count": 1, 
"threads_per_core": 2}, "ebs_optimized": true, "ena_support": true, 
"enclave_options": {"enabled": false}, "hibernation_options": {"configured": 
false}, "hypervisor": "xen", "iam_instance_profile": {"arn": 
"arn:aws-us:iam::23489:instance-profile/Ansible", "id": "AIPAXKK"}, "image_id": 
"ami-0beac6", "instance_id": "i-005f7ce", "instance_type": "t3a.medium", 
"key_name": "test", "launch_time": "2022-08-22T16:44:37+00:00", 
"maintenance_options": {"auto_recovery": "default"}, "metadata_options": 
{"http_endpoint": "enabled", "http_protocol_ipv6": "disabled", 
"http_put_response_hop_limit": 1, "http_tokens": "optional", 
"instance_metadata_tags": "disabled", "state": "pending"}, "monitoring": 
{"state": "disabled"}, "network_interfaces": [{"attachment": {"attach_time": 
"2022-08-22T16:44:37+00:00", "attachment_id": "eni-attach-03c0fe9", 
"delete_on_termination": true, "device_index": 0, "network_card_index": 0, 
"status": "attaching"}, "description": "", "groups": [{"group_id": "sg-34", 
"group_name": "INT User Data"}], "interface_type": "interface", 
"ipv6_addresses": [], "mac_address": "06", "network_interface_id": 
"eni-0cac24c", "owner_id": "23489", "private_dns_name": 
"ip-10-1-4-3.us<http://ip-10-1-4-3.us>-west-1.compute.internal", 
"private_ip_address": "10.1.4.3", "private_ip_addresses": [{"primary": true, 
"private_dns_name": 
"ip-10-1-4-3.us<http://ip-10-1-4-3.us>-west-1.compute.internal", 
"private_ip_address": "10.1.4.3"}], "source_dest_check": true, "status": 
"in-use", "subnet_id": "subnet-f5b", "vpc_id": "vpc-dbc"}], "placement": 
{"availability_zone": "us-west-1a", "group_name": "", "tenancy": "default"}, 
"platform_details": "Red Hat Enterprise Linux", "private_dns_name": 
"ip-10-1-4-3.us<http://ip-10-1-4-3.us>-west-1.compute.internal", 
"private_dns_name_options": {"enable_resource_name_dns_a_record": false, 
"enable_resource_name_dns_aaaa_record": false, "hostname_type": "ip-name"}, 
"private_ip_address": "10.1.4.3", "product_codes": [], "public_dns_name": "", 
"root_device_name": "/dev/sda1", "root_device_type": "ebs", "security_groups": 
[{"group_id": "sg-34", "group_name": "INT User Data"}], "source_dest_check": 
true, "state": {"code": 0, "name": "pending"}, "state_transition_reason": "", 
"subnet_id": "subnet-f5b", "tags": {"Application Group": "CORE", "Application 
System": "Development Team", "Backupset": "true", "CST Start/Stop Schedule": 
"0700;1900;Weekdays", "CostCenter": "Dev", "Description": "Evaluation Server", 
"Environment": "DEV", "Name": "test8", "OS": "RHEL", "OS Version": "8", 
"Owner": "Development Center", "Patch Group": "Normal", "Sub Environment": 
"Development": "hvm", "vpc_id": "vpc-dbc"}], "spec": {"BlockDeviceMappings": 
[], "ClientToken": "64c1d4b", "DisableApiTermination": true, "EbsOptimized": 
true, "IamInstanceProfile": {"Arn": 
"arn:aws-us:iam::23489:instance-profile/Ansible"}, "ImageId": "ami-0beac", 
"InstanceType": "t3a.medium", "KeyName": "test", "MaxCount": 1, "MinCount": 1, 
"NetworkInterfaces": [{"AssociatePublicIpAddress": false, "DeviceIndex": 0, 
"Groups": ["sg-34"], "SubnetId": "subnet-f5b"}], "TagSpecifications": 
[{"ResourceType": "volume", "Tags": [{"Key": "Application Group", "Value": 
"CORE"}, {"Key": "Application System", "Value": "Development Team"}, {"Key": 
"Backupset", "Value": "true"}, {"Key": "CST Start/Stop Schedule", "Value": 
"0700;1900;Weekdays"}, {"Key": "Description", "Value": "Evaluation Server"}, 
{"Key": "CostCenter", "Value": "Dev"}, {"Key": "Environment", "Value": "DEV"}, 
{"Key": "Name", "Value": "test8"}, {"Key": "OS", "Value": "RHEL"}, {"Key": "OS 
Version", "Value": "8"}, {"Key": "Owner", "Value": "Develoment Center"}, 
{"Key": "Patch Group", "Value": "Normal"}, {"Key": "Sub Environment", "Value": 
"Development"}, {"Key": "Type", "Value": "Eval"}]}, {"ResourceType": 
"instance", "Tags": [{"Key": "Application Group", "Value": "CORE"}, {"Key": 
"Application System", "Value": "Development Team"}, {"Key": "Backupset", 
"Value": "true"}, {"Key": "CST Start/Stop Schedule", "Value": 
"0700;1900;Weekdays"}, {"Key": "Description", "Value": "A1 Operations 
Evaluation Server"}, {"Key": "CostCenter", "Value": "Dev"}, {"Key": 
"Environment", "Value": "DEV"}, {"Key": "Name", "Value": "test8"}, {"Key": 
"OS", "Value": "RHEL"}, {"Key": "OS Version", "Value": "8"}, {"Key": "Owner", 
"Value": "Development Center"}, {"Key": "Patch Group", "Value": "Normal"}, 
{"Key": "Sub Environment", "Value": "Development"}, {"Key": "Type", "Value": 
"Eval"}]}]}}



 TASK [Add new instance to host group] 
***************************************************************************************************************************************************
task path: /etc/ansible/playbooks/rhel-8-build.yml:209
creating host via 'add_host': hostname=test8
ok: [127.0.0.1] => (item={'ami_launch_index': 0, 'image_id': 'ami-0beac6b6', 
'instance_id': '06-005sd3sfs', 'instance_type': 't3a.medium', 'key_name': 
'test', 'launch_time': '2022-08-22T16:44:37+00:00', 'monitoring': {'state': 
'disabled'}, 'placement': {'availability_zone': 'us-west-1a', 'group_name': '', 
'tenancy': 'default'}, 'private_dns_name': 
'ip-10-1-4-1x.us<http://ip-10-1-4-1x.us>-west-1.compute.internal', 
'private_ip_address': '10.1.4.1x', 'product_codes': [], 'public_dns_name': '', 
'state': {'code': 0, 'name': 'pending'}, 'state_transition_reason': '', 
'subnet_id': 'subnet-f5b', 'vpc_id': 'vpc-dbc5', 'architecture': 'x86_64', 
'block_device_mappings': [{'device_name': '/dev/sda1', 'ebs': {'attach_time': 
'2022-08-22T16:44:38+00:00', 'delete_on_termination': False, 'status': 
'attaching', 'volume_id': 'vol-0488sdfec'}}], 'client_token': '64c1d49a1d', 
'ebs_optimized': True, 'ena_support': True, 'hypervisor': 'xen', 
'iam_instance_profile': {'arn': 
'arn:aws-usv:iam::23489:instance-profile/Ansible', 'id': 'AI'}, 
'network_interfaces': [{'attachment': {'attach_time': 
'2022-08-22T16:44:37+00:00', 'attachment_id': 'eni-attach-03c0fe', 
'delete_on_termination': True, 'device_index': 0, 'status': 'attaching', 
'network_card_index': 0}, 'description': '', 'groups': [{'group_name': 'User 
group', 'group_id': 'sg-34'}], 'ipv6_addresses': [], 'mac_address': '06', 
'network_interface_id': 'eni-0cac24cf', 'owner_id': '23489', 
'private_dns_name': 
'ip-10-1-4-3.us<http://ip-10-1-4-3.us>-west-1.compute.internal', 
'private_ip_address': '10.1.4.3', 'private_ip_addresses': [{'primary': True, 
'private_dns_name': 
'ip-10-1-4-3.us<http://ip-10-1-4-3.us>-west-1.compute.internal', 
'private_ip_address': '10.1.4.3'}], 'source_dest_check': True, 'status': 
'in-use', 'subnet_id': 'subnet-f5bsdfs', 'vpc_id': 'vpc-dbc5', 
'interface_type': 'interface'}], 'root_device_name': '/dev/sda1', 
'root_device_type': 'ebs', 'security_groups': [{'group_name': 'User group', 
'group_id': 'sg-34'}], 'source_dest_check': True, 'tags': {'OS': 'RHEL', 
'Owner': 'Development Center', 'Environment': 'DEV', 'Sub Environment': 
'Development', 'CostCenter': 'Dev', 'Backupset': 'true', 'Application System': 
'Development Team', 'OS Version': '8', 'Application Group': 'CORE', 'Patch 
Group': 'Normal', 'Description': 'Evaluation Server', 'Name': 'test8', 'CST 
Start/Stop Schedule': '0700;1900;Weekdays', 'Type': 'Eval'}, 
'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': 'pending', 'http_tokens': 
'optional', 'http_put_response_hop_limit': 1, 'http_endpoint': 'enabled', 
'http_protocol_ipv6': 'disabled', 'instance_metadata_tags': 'disabled'}, 
'enclave_options': {'enabled': False}, 'platform_details': 'Red Hat Enterprise 
Linux', 'usage_operation': 'RunInstances:0010', 'usage_operation_update_time': 
'2022-08-22T16:44:37+00:00', 'private_dns_name_options': {'hostname_type': 
'ip-name', 'enable_resource_name_dns_a_record': False, 
'enable_resource_name_dns_aaaa_record': False}, 'maintenance_options': 
{'auto_recovery': 'default'}}) => {"add_host": {"groups": ["launched"], 
"host_name": "test8", "host_vars": {}}, "ansible_loop_var": "item", "changed": 
false, "item": {"ami_launch_index": 0, "architecture": "x86_64", 
"block_device_mappings": [{"device_name": "/dev/sda1", "ebs": {"attach_time": 
"2022-08-22T16:44:38+00:00", "delete_on_termination": false, "status": 
"attaching", "volume_id": "vol-0488sdfec2f4e9e6"}}], 
"capacity_reservation_specification": {"capacity_reservation_preference": 
"open"}, "client_token": "64c1d4b064fa418d99eeca4aa44c7a1d", "cpu_options": 
{"core_count": 1, "threads_per_core": 2}, "ebs_optimized": true, "ena_support": 
true, "enclave_options": {"enabled": false}, "hibernation_options": 
{"configured": false}, "hypervisor": "xen", "iam_instance_profile": {"arn": 
"arn:aws-us-:iam::23489:instance-profile/Ansible", "id": 
"AIPAXKKA2BWLPEG6LE23X"}, "image_id": "ami-0beac6b6ed4da44ce", "instance_id": 
"06-005sd3sfs0a0d9", "instance_type": "t3a.medium", "key_name": "test", 
"launch_time": "2022-08-22T16:44:37+00:00", "maintenance_options": 
{"auto_recovery": "default"}, "metadata_options": {"http_endpoint": "enabled", 
"http_protocol_ipv6": "disabled", "http_put_response_hop_limit": 1, 
"http_tokens": "optional", "instance_metadata_tags": "disabled", "state": 
"pending"}, "monitoring": {"state": "disabled"}, "network_interfaces": 
[{"attachment": {"attach_time": "2022-08-22T16:44:37+00:00", "attachment_id": 
"eni-attach-03c0fe95d6d45e0a2", "delete_on_termination": true, "device_index": 
0, "network_card_index": 0, "status": "attaching"}, "description": "", 
"groups": [{"group_id": "sg-347", "group_name": "User Data"}], 
"interface_type": "interface", "ipv6_addresses": [], "mac_address": "06", 
"network_interface_id": "eni-0cac24c770", "owner_id": "23489", 
"private_dns_name": 
"ip-10-1-4-3.us<http://ip-10-1-4-3.us>-west-1.compute.internal", 
"private_ip_address": "10.1.4.3", "private_ip_addresses": [{"primary": true, 
"private_dns_name": 
"ip-10-1-4-3.us<http://ip-10-1-4-3.us>-west-1.compute.internal", 
"private_ip_address": "10.1.4.3"}], "source_dest_check": true, "status": 
"in-use", "subnet_id": "subnet-f5sdfws", "vpc_id": "vpc-dbc5"}], "placement": 
{"availability_zone": "us-west-1a", "group_name": "", "tenancy": "default"}, 
"platform_details": "Red Hat Enterprise Linux", "private_dns_name": 
"ip-10-1-4-3.us<http://ip-10-1-4-3.us>-west-1.compute.internal", 
"private_dns_name_options": {"enable_resource_name_dns_a_record": false, 
"enable_resource_name_dns_aaaa_record": false, "hostname_type": "ip-name"}, 
"private_ip_address": "10.1.4.3", "product_codes": [], "public_dns_name": "", 
"root_device_name": "/dev/sda1", "root_device_type": "ebs", "security_groups": 
[{"group_id": "sg-34", "group_name": "User group"}], "source_dest_check": true, 
"state": {"code": 0, "name": "pending"}, "state_transition_reason": "", 
"subnet_id": "subnet-f5sdfws", "tags": {"Application Group": "CORE", 
"Application System": "Development Team", "Backupset": "true", "CST Start/Stop 
Schedule": "0700;1900;Weekdays", "CostCenter": "Dev", "Description": 
"Evaluation Server", "Environment": "DEV", "Name": "test8", "OS": "RHEL", "OS 
Version": "8", "Owner": "Development Center", "Patch Group": "Normal", "Sub 
Environment": "Development", "Type": "Eval"}, "usage_operation": 
"RunInstances:0010", "usage_operation_update_time": 
"2022-08-22T16:44:37+00:00", "virtualization_type": "hvm", "vpc_id": "vpc-db"}}

TASK [Wait for SSH to come up] 
**********************************************************************************************************************************************************
task path: /etc/ansible/playbooks/rhel-8-build.yml:231
[WARNING]: Reset is not implemented for this connection
ok: [127.0.0.1 -> 10.1.4.3] => (item={'ami_launch_index': 0, 'image_id': 
'ami-0beac6b6ed4da44ce', 'instance_id': '06-005sd3sfs0a0d9', 'instance_type': 
't3a.medium', 'key_name': 'test', 'launch_time': '2022-08-22T16:44:37+00:00', 
'monitoring': {'state': 'disabled'}, 'placement': {'availability_zone': 
'us-west-1a', 'group_name': '', 'tenancy': 'default'}, 'private_dns_name': 
'ip-10-1-4-3.us<http://ip-10-1-4-3.us>-west-1.compute.internal', 
'private_ip_address': '10.1.4.3', 'product_codes': [], 'public_dns_name': '', 
'state': {'code': 0, 'name': 'pending'}, 'state_transition_reason': '', 
'subnet_id': 'subnet-f5bsdfs', 'vpc_id': 'vpc-dbc5', 'architecture': 'x86_64', 
'block_device_mappings': [{'device_name': '/dev/sda1', 'ebs': {'attach_time': 
'2022-08-22T16:44:38+00:00', 'delete_on_termination': False, 'status': 
'attaching', 'volume_id': 'vol-0488sdfec2f4e9e6'}}], 'client_token': 
'64c1d4b064fa418d99eeca4aa44c7a1d', 'ebs_optimized': True, 'ena_support': True, 
'hypervisor': 'xen', 'iam_instance_profile': {'arn': 
'arn:aws-us-:iam::23489:instance-profile/Ansible', 'id': 'AIPAXKK'}, 
'network_interfaces': [{'attachment': {'attach_time': 
'2022-08-22T16:44:37+00:00', 'attachment_id': 'eni-attach-03c0fe', 
'delete_on_termination': True, 'device_index': 0, 'status': 'attaching', 
'network_card_index': 0}, 'description': '', 'groups': [{'group_name': 'User 
group', 'group_id': 'sg-34'}], 'ipv6_addresses': [], 'mac_address': '06', 
'network_interface_id': 'eni-0cac24cf', 'owner_id': '23489', 
'private_dns_name': 
'ip-10-1-4-3.us<http://ip-10-1-4-3.us>-west-1.compute.internal', 
'private_ip_address': '10.1.4.3', 'private_ip_addresses': [{'primary': True, 
'private_dns_name': 
'ip-10-1-4-3.us<http://ip-10-1-4-3.us>-west-1.compute.internal', 
'private_ip_address': '10.1.4.3'}], 'source_dest_check': True, 'status': 
'in-use', 'subnet_id': 'subnet-f5bsdfs', 'vpc_id': 'vpc-dbc5', 
'interface_type': 'interface'}], 'root_device_name': '/dev/sda1', 
'root_device_type': 'ebs', 'security_groups': [{'group_name': 'User group', 
'group_id': 'sg-34'}], 'source_dest_check': True, 'tags': {'OS': 'RHEL', 
'Owner': 'Development Center', 'Environment': DEV', 'Sub Environment': 
'Evaluation'', 'CostCenter': 'Eval2', 'Backupset': 'true', 'Application 
System': 'Development Team', 'OS Version': '8', 'Application Group': 'CORE', 
'Patch Group': 'Normal', 'Description': 'Evaluation Server', 'Name': 'test8', 
'CST Start/Stop Schedule': '0700;1900;Weekdays', 'Type': 'Eval'}, 
'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': 'pending', 'http_tokens': 
'optional', 'http_put_response_hop_limit': 1, 'http_endpoint': 'enabled', 
'http_protocol_ipv6': 'disabled', 'instance_metadata_tags': 'disabled'}, 
'enclave_options': {'enabled': False}, 'platform_details': 'Red Hat Enterprise 
Linux', 'usage_operation': 'RunInstances:0010', 'usage_operation_update_time': 
'2022-08-22T16:44:37+00:00', 'private_dns_name_options': {'hostname_type': 
'ip-name', 'enable_resource_name_dns_a_record': False, 
'enable_resource_name_dns_aaaa_record': False}, 'maintenance_options': 
{'auto_recovery': 'default'}}) => {"ansible_loop_var": "item", "changed": 
false, "elapsed": 120, "item": {"ami_launch_index": 0, "architecture": 
"x86_64", "block_device_mappings": [{"device_name": "/dev/sda1", "ebs": 
{"attach_time": "2022-08-22T16:44:38+00:00", "delete_on_termination": false, 
"status": "attaching", "volume_id": "vol-0488sdfe"}}], 
"capacity_reservation_specification": {"capacity_reservation_preference": 
"open"}, "client_token": "64c1d", "cpu_options": {"core_count": 1, 
"threads_per_core": 2}, "ebs_optimized": true, "ena_support": true, 
"enclave_options": {"enabled": false}, "hibernation_options": {"configured": 
false}, "hypervisor": "xen", "iam_instance_profile": {"arn": 
"arn:aws-us-:iam::23489:instance-profile/Ansible", "id": "AIPAX"}, "image_id": 
"ami-0bea", "instance_id": "i-005", "instance_type": "t3a.medium", "key_name": 
"test", "launch_time": "2022-08-22T16:44:37+00:00", "maintenance_options": 
{"auto_recovery": "default"}, "metadata_options": {"http_endpoint": "enabled", 
"http_protocol_ipv6": "disabled", "http_put_response_hop_limit": 1, 
"http_tokens": "optional", "instance_metadata_tags": "disabled", "state": 
"pending"}, "monitoring": {"state": "disabled"}, "network_interfaces": 
[{"attachment": {"attach_time": "2022-08-22T16:44:37+00:00", "attachment_id": 
"eni-attach-03c0fe95d6d45e0a2", "delete_on_termination": true, "device_index": 
0, "network_card_index": 0, "status": "attaching"}, "description": "", 
"groups": [{"group_id": "sg-34", "group_name": "User group"}], 
"interface_type": "interface", "ipv6_addresses": [], "mac_address": "06", 
"network_interface_id": "eni-0cac", "owner_id": "23489", "private_dns_name": 
"ip-10-1-4-1x-1.us<http://ip-10-1-4-1x-1.us>-west-1.compute.internal", 
"private_ip_address": "10.1.4.1x.1", "private_ip_addresses": [{"primary": true, 
"private_dns_name": 
"ip-10-1-4-3.us<http://ip-10-1-4-3.us>-west-1.compute.internal", 
"private_ip_address": "10.1.4.3"}], "source_dest_check": true, "status": 
"in-use", "subnet_id": "subnet-f5sd", "vpc_id": "vpc-dbc5"}], "placement": 
{"availability_zone": "us-west-1a", "group_name": "", "tenancy": "default"}, 
"platform_details": "Red Hat Enterprise Linux", "private_dns_name": 
"ip-10-1-4-3.us<http://ip-10-1-4-3.us>-west-1.compute.internal", 
"private_dns_name_options": {"enable_resource_name_dns_a_record": false, 
"enable_resource_name_dns_aaaa_record": false, "hostname_type": "ip-name"}, 
"private_ip_address": "10.1.4.3", "product_codes": [], "public_dns_name": "", 
"root_device_name": "/dev/sda1", "root_device_type": "ebs", "security_groups": 
[{"group_id": "sg-34", "group_name": "User group"}], "source_dest_check": true, 
"state": {"code": 0, "name": "pending"}, "state_transition_reason": "", 
"subnet_id": "subnet-f5sdfws", "tags": {"Application Group": "CORE", 
"Application System": "Development Team", "Backupset": "true", "CST Start/Stop 
Schedule": "0700;1900;Weekdays", "CostCenter": Dev", "Description": "Evaluation 
Server", "Environment": "DEV", "Name": "test8", "OS": "RHEL", "OS Version": 
"8", "Owner": "Development Center", "Patch Group": "Normal", "Sub Environment": 
"Evaluation", "Type": "Eval"}, "usage_operation": "RunInstances:0010", 
"usage_operation_update_time": "2022-08-22T16:44:37+00:00", 
"virtualization_type": "hvm", "vpc_id": "vpc-dbc"}}
META: ran handlers
META: ran handlers

PLAY [Run post-launch baseline configuration on instance] 
*******************************************************************************************************************************
META: ran handlers

TASK [Update system name] 
***************************************************************************************************************************************************************
task path: /etc/ansible/playbooks/rhel-8-build.yml:265
fatal: [test8]: FAILED! => {"msg": "The task includes an option with an 
undefined variable. The error was: 'tags' is undefined\n\nThe error appears to 
be in '/etc/ansible/playbooks/rhel-8-build.yml': line 265, column 8, but 
may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe 
offending line appears to be:\n\n\n     - name: 'Update system name'\n       ^ 
here\n"}

PLAY RECAP 
******************************************************************************************************************************************************************************
127.0.0.1                  : ok=14   changed=2    unreachable=0    failed=0    
skipped=0    rescued=0    ignored=0
test8                      : ok=0    changed=0    unreachable=0    failed=1    
skipped=0    rescued=0    ignored=0




#######################################################################################################





On Mon, Aug 22, 2022 at 6:21 AM 'Rowe, Walter P. (Fed)' via Ansible Project 
<[email protected]<mailto:[email protected]>> 
wrote:
Do you need to reference the tags inside new_ec2.instances ?

Walter
--
Walter Rowe, Division Chief
Infrastructure Services, OISM

On Aug 19, 2022, at 3:55 PM, Carmie 
<[email protected]<mailto:[email protected]>> wrote:

Can anyone shed some light into this?

1. I'm using the aws_ec2 plugin
2. I can launch an ec2 instance successfully and run commands post launch

My issue comes in when I reference the {{ tags['Name'] }} to pull from AWS tag 
Name from the newly created instance. I can see all the  tag key/value info 
from the  when I add the new instance to the host group; however, for some 
reason it fails as soon as it hits the new play/task to configure the hostname 
based on the aws ec2 tag Name. I'm guessing Ansible is not carrying over the 
variables in the add_host to host group.

 fatal: FAILED! => {"msg": "The task includes an option with an undefined 
variable. The error was: 'tags' is undefined\n\nThe error appears to be in 
'/etc/ansible/roles/component_baselines/hostname/tasks/main.yml': line 12, 
column 5, but may\nbe elsewhere in the file depending on the exact syntax 
problem.\n\nThe offending line appears to be:\n\n\n  - name: 'Update system 
name'\n    ^ here\n"}

Here's a snippet of the tail end of the ec2 create playbook:

       register: new_ec2


      - name: 'Add new instance to host group'
        add_host:
          hostname: "{{ item.private_ip_address }}"
          groupname: 'launched'
        loop: "{{ new_ec2.instances }}"

      - name: 'Wait for SSH to come up'
        delegate_to: "{{ item.private_ip_address }}"
        wait_for_connection:
          delay: 120
          timeout: 480
        loop: "{{ new_ec2.instances }}"


  - name: 'Run post-launch baseline configuration on instance'
    hosts: 'launched'
    gather_facts: no
    become: yes

    roles:
     - 'component_baselines/hostname'


Details of the hostname role being called:

  - name: 'Gather current system name'
    ansible.builtin.setup:

  - name: 'Update system name'
    shell: >
      hostnamectl set-hostname
      {{ tags['Name'] }}.{{ domain['fqdn'] }} --static --transient &&
      hostnamectl set-hostname {{ tags['Name'] }}.{{ domain['fqdn'] }} --pretty
     when: "tags['Name'] != ansible_facts['hostname']"

  - name: 'Update sysconfig File'
    template:
      src: 'network.j2'
      dest: '/etc/sysconfig/network'
      owner: 'root'
      group: 'root'
      mode: '0644'
    register: taskresult
    notify: 'Restart systemd-hostnamed'
    when: "tags['Name'] != ansible_facts['hostname']"


--
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to 
[email protected]<mailto:[email protected]>.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/f76dcef9-921e-47e7-aea3-8b588613192cn%40googlegroups.com<https://gcc02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgroups.google.com%2Fd%2Fmsgid%2Fansible-project%2Ff76dcef9-921e-47e7-aea3-8b588613192cn%2540googlegroups.com%3Futm_medium%3Demail%26utm_source%3Dfooter&data=05%7C01%7Cwalter.rowe%40nist.gov%7C92888ccea9804189735808da8465c466%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C0%7C637967869882272440%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=9Tu4b%2FJU8kWNwPoHlefvLm6VimTiYC8UXcEPCqN2zhM%3D&reserved=0>.


--
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to 
[email protected]<mailto:[email protected]>.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/AA182698-D710-47BF-A68C-47F98192403E%40nist.gov<https://gcc02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgroups.google.com%2Fd%2Fmsgid%2Fansible-project%2FAA182698-D710-47BF-A68C-47F98192403E%2540nist.gov%3Futm_medium%3Demail%26utm_source%3Dfooter&data=05%7C01%7Cwalter.rowe%40nist.gov%7C92888ccea9804189735808da8465c466%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C0%7C637967869882272440%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=UB6kUcbeOmfOQeE9oX0oVktwPzQXMkSkKeWpD2Xb5Lk%3D&reserved=0>.

--
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to 
[email protected]<mailto:[email protected]>.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAPsYAg%2BuB6Z5%2BOZeCGUyQVmaZPS%3DbO%3DJq8Cjz_tn9xH7jKP%3DWQ%40mail.gmail.com<https://gcc02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgroups.google.com%2Fd%2Fmsgid%2Fansible-project%2FCAPsYAg%252BuB6Z5%252BOZeCGUyQVmaZPS%253DbO%253DJq8Cjz_tn9xH7jKP%253DWQ%2540mail.gmail.com%3Futm_medium%3Demail%26utm_source%3Dfooter&data=05%7C01%7Cwalter.rowe%40nist.gov%7C92888ccea9804189735808da8465c466%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C0%7C637967869882272440%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=N2TArAsJj0UxDhuRZ%2BrugBsNrXSaVtnTwsyH1GQ3My8%3D&reserved=0>.

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/589501B1-F229-42E6-92F1-D54E3443BB00%40nist.gov.

Reply via email to