# Undefined variable

**URL:** <https://forum.ansible.com/t/undefined-variable/37014>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [August 19, 2022, 7:55pm UTC](https://forum.ansible.com/t/undefined-variable/37014 "2022-08-19T19:55:36Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Carm](https://avatars.discourse-cdn.com/v4/letter/c/c2a13f/32.png) [@Carm](https://forum.ansible.com/u/Carm)\
**Post date:** [August 19, 2022, 7:55pm UTC](https://forum.ansible.com/t/undefined-variable/37014/1 "2022-08-19T19:55:36Z")

</div>

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’]”

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [August 20, 2022, 5:51pm UTC](https://forum.ansible.com/t/undefined-variable/37014/2 "2022-08-20T17:51:45Z")

</div>

You say, “I can see all the tag key/value info from the when I add the new instance to the host group;” Okay, but we can’t. Can you show us?

Or perhaps more briefly, at what line in your code do you expect Ansible to become aware of a variable called “tags”?

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [August 22, 2022, 11:21am UTC](https://forum.ansible.com/t/undefined-variable/37014/3 "2022-08-22T11:21:28Z")

</div>

Do you need to reference the tags inside new\_ec2.instances ?

Walter

---

<div class="post-metadata">

**Author:** ![Carm](https://avatars.discourse-cdn.com/v4/letter/c/c2a13f/32.png) [@Carm](https://forum.ansible.com/u/Carm)\
**Post date:** [August 22, 2022, 5:42pm UTC](https://forum.ansible.com/t/undefined-variable/37014/4 "2022-08-22T17:42:43Z")

</div>

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-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-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-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-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-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-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-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-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-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-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-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-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-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-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-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

---

<div class="post-metadata">

**Author:** ![Carm](https://avatars.discourse-cdn.com/v4/letter/c/c2a13f/32.png) [@Carm](https://forum.ansible.com/u/Carm)\
**Post date:** [August 22, 2022, 6:24pm UTC](https://forum.ansible.com/t/undefined-variable/37014/5 "2022-08-22T18:24:59Z")

</div>

Going from localhost to ‘launched’ host, it seems to drop the variables from the new\_ec2\_instances. I’m not sure how to pull those tags going from localhost to executing tasks on the newly launched host.

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [August 22, 2022, 7:01pm UTC](https://forum.ansible.com/t/undefined-variable/37014/6 "2022-08-22T19:01:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Carm](https://avatars.discourse-cdn.com/v4/letter/c/c2a13f/32.png) [@Carm](https://forum.ansible.com/u/Carm)\
**Post date:** [August 22, 2022, 9:23pm UTC](https://forum.ansible.com/t/undefined-variable/37014/7 "2022-08-22T21:23:16Z")

</div>

Correct, I’m getting the domain[‘fqdn’] in my global environment variables yml file.

I updated the hostname task, but it’s still failing on an undefined variable 'new\_ec2" when it reaches that point

- name: ‘Update system name’  
shell: \>  
hostnamectl set-hostname  
{{ [item.tags.Name](http://item.tags.Name) }}.{{ domain[‘fqdn’] }} --static --transient &&  
hostnamectl set-hostname {{ [item.tags.Name](http://item.tags.Name) }}.{{ domain[‘fqdn’] }} --pretty  
loop: “{{ new\_ec2.instances }}”

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”: “‘new\_ec2’ is undefined”}

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

So there are 2 different hosts with specific tasks ran from each. The start of this playbook has localhost set to create a new RHEL ec2 instance from an ami. I also prompt for specific vars at the beginning prior to creating a new ec2 instance. See below…I didn’t include all the prompts to keep this ansible post so it’s not so long but let me know if you need it. Does Ansible clear its memory of variables/facts from tasks from localhost to the new hosts? I want to be able to launch a new ec2 instance, connect to it, and then run post configuration tasks (without having to run 2 different ansible-playbook commands). The only issue has been trying to grab the tag Name from a newly launched ec2 instance. Maybe there’s a better way to do this than the approach I’m taking.

- name: ‘Build a RHEL 8 server from AMI’  
hosts: ‘127.0.0.1’  
connection: ‘local’  
gather\_facts: no

pre\_tasks:

- pause:  
prompt: \>-  
What is the name of this system? ( ex: DB-09 )  
register: prompt\_name\_tag

- pause:  
prompt: \>-  
Instance Type? ( press Enter for default: t3a.medium )  
register: prompt\_ec2\_type

- pause:  
prompt: \>-  
Provide an AMI ID:  
( press Enter for default: Vanilla RHEL 8 Base AMI )  
register: prompt\_ami\_id

tasks:

- name: ‘Launch instance’  
ec2\_instance:  
image\_id: \>-  
{{  
prompt\_ami\_id.user\_input if prompt\_ami\_id.user\_input  
else build[‘aws’][‘defaults’][‘ami’]  
}}  
instance\_type: \>-  
{{  
prompt\_ec2\_type.user\_input if prompt\_ec2\_type.user\_input  
else ‘t3a.medium’  
}}

register: new\_ec2

- 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: ‘Update system name’  
shell: \>  
hostnamectl set-hostname  
{{ [item.tags.Name](http://item.tags.name/) }}.{{ domain[‘fqdn’] }} --static --transient &&  
hostnamectl set-hostname {{ [item.tags.Name](http://item.tags.name/) }}.{{ domain[‘fqdn’] }} --pretty  
loop: “{{ new\_ec2.instances }}”

---

<div class="post-metadata">

**Author:** ![Carm](https://avatars.discourse-cdn.com/v4/letter/c/c2a13f/32.png) [@Carm](https://forum.ansible.com/u/Carm)\
**Post date:** [August 23, 2022, 2:51pm UTC](https://forum.ansible.com/t/undefined-variable/37014/8 "2022-08-23T14:51:23Z")

</div>

I was able to reference new\_ec2 using the hostvars magic variable as follows  
:

- name: ‘Run post-launch baseline configuration on instance’  
hosts: ‘launched’  
gather\_facts: no  
become: yes

tasks:

- debug: var=hostvars[‘127.0.0.1’][‘new\_ec2’][‘instances’]  
when: hostvars[‘127.0.0.1’][‘new\_ec2’][‘instances’] is defined

The output shows the following:

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /etc/ansible/playbooks/rhel-8-build.yml:255  
ok: [test8] =\> {  
“hostvars[‘127.0.0.1’][‘new\_ec2’][‘instances’]”: [  
{  
“ami\_launch\_index”: 0,  
“architecture”: “x86\_64”,  
“block\_device\_mappings”: [  
{  
“device\_name”: “/dev/sda1”,  
“ebs”: {  
“attach\_time”: “2022-08-23T14:32:05+00:00”,  
“delete\_on\_termination”: false,  
“status”: “attaching”,  
“volume\_id”: “vol-0”  
}  
}  
],  
“capacity\_reservation\_specification”: {  
“capacity\_reservation\_preference”: “open”  
},  
“client\_token”: “”,  
“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::xxxxxxxxxxxxxxxx:instance-profile/xxxxxxxxxxx”,  
“id”: “xxxxxxxxxxxx”  
},  
“image\_id”: “ami-0xxxxxxxxxxxxxx”,  
“instance\_id”: “i-0xxxxxxxxxxxx”,  
“instance\_type”: “t3a.medium”,  
“key\_name”: “xxxx”,  
“launch\_time”: “2022-08-23T14:32:05+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-23T14:32:05+00:00”,  
“attachment\_id”: “eni-attach-06xxxxx7”,  
“delete\_on\_termination”: true,  
“device\_index”: 0,  
“network\_card\_index”: 0,  
“status”: “attaching”  
},  
“description”: “”,  
“groups”: [  
{  
“group\_id”: “sg-34758e50”,  
“group\_name”: “xxxxxx”  
}  
],  
“interface\_type”: “interface”,  
“ipv6\_addresses”: ,  
“mac\_address”: “0xxxxxxx”,  
“network\_interface\_id”: “eni-xxxxxxxx”,  
“owner\_id”: “xxxxxxxxxxxx”,  
“private\_dns\_name”: “ip-xxx-xx-xx-xx.us-west-1.compute.internal”,  
“private\_ip\_address”: “xxx.xx.xx.24”,  
“private\_ip\_addresses”: [  
{  
“primary”: true,  
“private\_dns\_name”: “ip-xxx-xx-xx-24.us-west-1.compute.internal”,  
“private\_ip\_address”: “xxx.xx.xx.24”  
}  
],  
“source\_dest\_check”: true,  
“status”: “in-use”,  
“subnet\_id”: “subnet-xxx”,  
“vpc\_id”: “vpc-xxx”  
}  
],  
“placement”: {  
“availability\_zone”: “us-west-1a”,  
“group\_name”: “”,  
“tenancy”: “default”  
},  
“platform\_details”: “Red Hat Enterprise Linux”,  
“private\_dns\_name”: “ip-xxx-xx-xx-24.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”: “xxx.xx.xx.24”,  
“product\_codes”: ,  
“public\_dns\_name”: “”,  
“root\_device\_name”: “/dev/sda1”,  
“root\_device\_type”: “ebs”,  
“security\_groups”: [  
{  
“group\_id”: “sg-xxxxx”,  
“group\_name”: “xxxxx”  
}  
],  
“source\_dest\_check”: true,  
“state”: {  
“code”: 0,  
“name”: “pending”  
},  
“state\_transition\_reason”: “”,  
“subnet\_id”: “subnet-xxxxx”,  
“tags”: {  
“Application Group”: “CO”,  
“Application System”: “xxx”,  
“Backupset”: “true”,  
“CST Start/Stop Schedule”: “0700;1900;Weekdays”,  
“CostCenter”: “xxx”,  
“Description”: “Evaluation Server”,  
“Environment”: “xxxxx”,  
“Name”: “test8”,  
“OS”: “RHEL”,  
“OS Version”: “8”,  
“Owner”: “CO”,  
“Patch Group”: “Normal”,  
“Sub Environment”: “xxxxxxxx”,  
“Type”: “Eval”  
},  
“usage\_operation”: “RunInstances:0010”,  
“usage\_operation\_update\_time”: “2022-08-23T14:32:05+00:00”,  
“virtualization\_type”: “hvm”,  
“vpc\_id”: “vpc-dxxxxx”  
}  
]  
}

My next question is, how can I grab the Name tags and use it in my update system name command below? I tried this but it errors out:

- name: ‘Update system name’  
shell: \>

hostnamectl set-hostname  
{{ hostvars[‘127.0.0.1’][‘new\_ec2’][‘instances’][‘tags’][‘Name’] }}.{{ domain[‘fqdn’] }} --static --transient &&  
hostnamectl set-hostname {{ hostvars[‘127.0.0.1’][‘new\_ec2’][‘instances’] ‘tags]’['‘Name’] }}.{{ domain[‘fqdn’] }} --pretty

TASK [Update system name] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /etc/ansible/playbooks/rhel-8-build.yml:278  
fatal: [test8]: FAILED! =\> {“msg”: “The task includes an option with an undefined variable. The error was: ‘list object’ has no attribute ‘tags’\n\nThe error appears to be in ‘/etc/ansible/playbooks/rhel-8-build.yml’: line 278, 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”}

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [August 23, 2022, 5:32pm UTC](https://forum.ansible.com/t/undefined-variable/37014/9 "2022-08-23T17:32:34Z")

</div>

hostnamectl set-hostname {{ hostvars[‘127.0.0.1’][‘new\_ec2’][‘instances’] ‘tags]’['‘Name’] }}.{{ domain[‘fqdn’] }} --pretty

this line is missing a “[” before ‘tags’ and has a “]” before the ’ after tags.

hostnamectl set-hostname {{ hostvars[‘127.0.0.1’][‘new\_ec2’][‘instances’] [‘tags]’ ['‘Name’] }}.{{ domain[‘fqdn’] }} --pretty

---

<div class="post-metadata">

**Author:** ![Carm](https://avatars.discourse-cdn.com/v4/letter/c/c2a13f/32.png) [@Carm](https://forum.ansible.com/u/Carm)\
**Post date:** [August 23, 2022, 7:57pm UTC](https://forum.ansible.com/t/undefined-variable/37014/10 "2022-08-23T19:57:37Z")

</div>

Thanks Walter! I better grab my reading glasses. I made the changes:

![image.png](https://us1.discourse-cdn.com/ansible/original/2X/c/ce63c95ca3aafc6a3eea8f49ce303eb4b4eaaa64.png)

That should be correct, right?

It still fails even after making that correction

 ![image.png](https://us1.discourse-cdn.com/ansible/original/2X/c/c2411583fbaa9e591c58a78956e6854e34db3c6f.png)

---

<div class="post-metadata">

**Author:** ![Carm](https://avatars.discourse-cdn.com/v4/letter/c/c2a13f/32.png) [@Carm](https://forum.ansible.com/u/Carm)\
**Post date:** [August 23, 2022, 8:24pm UTC](https://forum.ansible.com/t/undefined-variable/37014/11 "2022-08-23T20:24:27Z")

</div>

Ahh, made a slight change to finally get it working! Thanks for all your replies, Walter.

![Screenshot 2022-08-23 152347.png](https://us1.discourse-cdn.com/ansible/original/2X/6/6e46a8168df014808da9ac09f71f6ba2a1678e7f.png)

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [August 23, 2022, 8:25pm UTC](https://forum.ansible.com/t/undefined-variable/37014/12 "2022-08-23T20:25:32Z")

</div>

That would do it. Congrats and good luck.

Walter
