Cyber Axe
Cyber Axe

Reputation: 196

Ansible: tasks to append number to name tag

I'm trying to append a number to the end of the instance name tag, i have the following code but there's a problem with the second task which i cannot find, and i've not been able to find an example of anyone else trying to solve this problem.

I'm also relatively new to Ansible and cannot find the relevant documentation to do certain things like how to lookup a value in a list like how i'm doing with my until: which is probably invalid syntax

Ansible is 2.9 and runs on the instance itself

The Tasks I have are setup to do the following

  1. Get a list of running EC2 instances that belong to the same launch template
  2. Loop the same amount of times as the instance list until the desired name based on item index is not found in the name tags of the ec2 list
  3. Set the updated tag name

Current Code:

---
- name:                            "{{ role_name }} | Name Instance: Gather facts about current LT instances"
  ec2_instance_info:
    tags:
      "aws:autoscaling:groupName": "{{ tag_asg }}"
      "aws:ec2launchtemplate:id":  "{{ tag_lt }}"
      Application:                 "{{ tag_application }}"
    filters:
      instance-state-name:         [ "running" ]
  no_log:                          false
  failed_when:                     false
  register:                        ec2_list

- name:                            "{{ role_name }} | Name Instance: Generate Name"
  with_sequence:                   count="{{ ec2_list.instances|length }}"
  until:                           not "{{ tag_default_name }} {{ '%02d' % (item + 1) }}" in ec2_list.instances[*].tags['Name']
  when:                            tag_name == tag_default_name
  no_log:                          false
  failed_when:                     false
  register:                        item

- name:                            "{{ role_name }} | Name Instance: Append Name Tag"
  ec2_tag:
    region:                        eu-west-1
    resource:                      "{{ instance_id }}"
    state:                         present
    tags:
      Name:                        "{{ tag_default_name }} {{ '%02d' % (item + 1) }}"
  when:                            tag_name == tag_default_name
  no_log:                          false
  failed_when:                     false

As requested here's the error I am getting:

ERROR! no module/action detected in task.

The error appears to be in '/app/deploy/Ansible/roles/Boilerplate/tasks/name_instance.yml': line 14, column 3, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:


- name:                            "{{ role_name }} | Name Instance: Generate Name"
  ^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes. Always quote template expression brackets when they
start a value. For instance:

    with_items:
      - {{ foo }}

Should be written as:

    with_items:
      - "{{ foo }}"

The error is not with the name: as i constantly get that error when there's some other error within the task body

Upvotes: 0

Views: 275

Answers (2)

Cyber Axe
Cyber Axe

Reputation: 196

Thanks to bnabbs answer i realised the problem with that version was the lack of module, after fixing that i then finished creating and testing it and now have a fully working set of tasks which has resulted in the following code.

---
- name:                                 "{{ role_name }} | Name Instance: Gather facts about current LT instances"
  ec2_instance_info:
    filters:
      "tag:aws:autoscaling:groupName": "{{ tag_asg }}"
      "tag:aws:ec2launchtemplate:id":  "{{ tag_lt }}"
      "tag:Application":               "{{ tag_application }}"
      instance-state-name:             [ "pending", "running" ]
  register:                            ec2_list


- name:                                "{{ role_name }} | Name Instance: Set Needed Naming Facts"
  set_fact:
    tag_name_seperator:                " "
    start_index:                       "{{ (ec2_list.instances | sort(attribute='instance_id') | sort(attribute='launch_time') | map(attribute='instance_id') | list).index(instance_id) }}"
    name_tag_list:                     "{{ (ec2_list.instances | map(attribute='tags') | list) | map(attribute='Name') | list }}"

# Generate Name from starting index of array and mod to the length of the amount of instances to help prevent conflicts when multiple instances are launched at the same time
- name:                                "{{ role_name }} | Name Instance: Generate Name"
  set_fact:
    desired_tag_name:                  "{{ tag_default_name }}{{ tag_name_seperator }}{{ '%02d' % (((item|int) + (start_index|int) % (name_tag_list|length)) + 1) }}"
  loop:                                "{{ name_tag_list }}"
  until:                               not "{{ tag_default_name }}{{ tag_name_seperator }}{{ '%02d' % (((item|int) + (start_index|int) % (name_tag_list|length)) + 1) }}" in name_tag_list

- name:                                "{{ role_name }} | Name Instance: Append Name Tag"
  ec2_tag:
    region:                            eu-west-1
    resource:                          "{{ instance_id }}"
    state:                             present
    tags:
      Name:                            "{{ desired_tag_name }}"

Upvotes: 0

bndabbs
bndabbs

Reputation: 56

You don't appear to have a module listed in the second task. You might be able to use debug as the module, or use set_fact and skip the register line.

I think it might also be possible to combine the last two tasks with some more advanced looping, but I'd have to play around with it to give you a working example.

Upvotes: 2

Related Questions