Ansible:with_sequence变量的开始和结束

时间:2019-05-13 06:15:34

标签: ansible

在我的剧本中,我提供了Jenkins构建ID,如下所示: -e frontend_build_id=43

此ID用于标记在构建过程中创建的Docker映像。稍后,我要删除旧图像,如下所示:

- name: remove old frontend images
  docker_image:
    state: absent
    name: "{{ docker_registry }}/{{ frontend_image_name }}"
    tag: "{{ item }}"
    force: yes
    with_sequence:
      start="{{ frontend_build_id }} - 10" end="{{ frontend_build_id }} - 1" stride=1

但是我遇到以下错误:

TASK [FrontEnd : remove old frontend images] ***********************************
fatal: [10.98.68.116]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'item' is undefined\n\nThe error appears to have been in '/home/jenkins/workspace/myproject_development/roles/FrontEnd/tasks/main.yml': line 31, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: remove old frontend images\n  ^ here\n"}
  1. 我该怎么办?
  2. 也许以某种方式用loop来实现这一目标更好?

1 个答案:

答案 0 :(得分:1)

这是您要寻找的顺序吗?

with_sequence:
  start="{{ frontend_build_id - 10 }}" end="{{ frontend_build_id - 1 }}" stride=1

步幅”的默认值为1。为简单起见,可以将其删除。

with_sequence:
  start="{{ frontend_build_id - 10 }}" end="{{ frontend_build_id - 1 }}"

Loop' range '将执行相同的操作。结束元素不包括在内,应递增。

loop: "{{ range(frontend_build_id - 10, frontend_build_id)|list }}"

将变量“ frontend_build_id ”显式转换为整数将使其更可靠。将每个对“ frontend_build_id ”的引用更改为“ frontend_build_id | int ”。例如:

with_sequence:
  start="{{ frontend_build_id|int - 10 }}" end="{{ frontend_build_id|int - 1 }}"

这应该可以解决问题

  

--'unicode'和'int'的不受支持的操作数类型