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%7C18ab310178444afc2e5d08da821ccd62%7C2ab5d82fd8fa4797a93e054655c61dec%7C1%7C0%7C637965357461102455%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=I%2FjuP5CGjRDDp3dYyjTOyz%2BLofuQdGrgwbtzwQeiJsA%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/AA182698-D710-47BF-A68C-47F98192403E%40nist.gov.

Reply via email to