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"?
On Friday, August 19, 2022 at 3:55:36 PM UTC-4 Carmie 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].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/cd5de1a8-ca93-4abf-8328-a8d2f5916eb3n%40googlegroups.com.