Ansible:将数字附加到名称标签的任务

问题描述

我正在尝试在实例名称标签的末尾附加一个数字,我具有以下代码,但第二个任务有一个我找不到的问题,并且我无法找到一个示例任何其他试图解决此问题的人。

我对Ansible还是比较陌生,无法找到相关文档来做某些事情,例如如何在列表中查找值,例如我对直到:这可能是无效的语法

Ansible为2.9,可在实例本身上运行

我具有的任务被设置为执行以下操作

  1. 获取属于同一启动模板的正在运行的EC2实例的列表
  2. 与实例列表循环相同的次数,直到在ec2列表的名称标签中找不到基于项目索引的所需名称
  3. 设置更新的标签名称

当前代码:

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

根据要求,这是我得到的错误:

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

错误名称不正确:当任务正文中出现其他错误时,我会不断收到该错误

解决方法

第二个任务中似乎没有列出模块。您也许可以将debug用作模块,或者使用set_fact并跳过寄存器行。

我认为也可以将最后两个任务与一些更高级的循环结合起来,但是我不得不尝试一下,以提供一个可行的示例。

,

感谢bnabbs的回答,我意识到该版本的问题是缺少模块,在修复该问题之后,我完成了创建和测试该模块的工作,现在拥有一套可以正常工作的任务,从而产生了以下代码。

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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...