使用ansible顺序赋值
Assign value sequentially using ansible
我想按顺序为变量赋值。第一次 运行 我想将 103 分配给变量的剧本,下次应该是 104。我还需要确保分配的值不在列表中。
变量:
list1:
- 100
- 101
- 102
val: 100
剧本:
- name: Set fact
set_fact:
val: "{{ val | int + 1 }}"
until: val not in list1
它尝试了 3 次但失败并出现以下错误。
FAILED - RETRYING: Set xxx (1 retries left).
fatal: [xxx.com]: FAILED! => {"ansible_facts": {"val": "102"}, "attempts": 3, "changed": false}
我也试过with_sequence,但是它分配了100个值,最后的值是200,它只分配了第一个值就没有退出。有办法吗?
with_sequence: start=100 end=200
until: val not in list1
请指教
我可能会为此创建一个自定义过滤器,但这里有一种直接在 ansible 中执行此操作的方法。我将现有值作为额外的逗号分隔值传递给命令行,以便于测试。
想法:
- 创建一个序列,从占用的插槽开始,到最后一个数字之后的 1 个数字结束。
- 从该序列中减去现有值。
- 取结果列表中较低的值。
这是示例剧本:
---
- name: Find next available slot in a range
hosts: localhost
gather_facts: false
vars:
occupied_slots: "{{ existing.split(',') | map('int') }}"
range_start: "{{ occupied_slots | min }}"
range_end: "{{ occupied_slots | max + 2 }}"
next_available: "{{ (range(range_start | int, range_end | int)) | difference(occupied_slots) | min }}"
tasks:
- name: Show next available slot
debug:
msg: "{{ next_available }}"
给出:
# Pass a contiguous list
$ ansible-playbook playbook.yml -e existing=100,101,102
PLAY [Find next available slot in a range] *********************************************************************************************************************************************************************************************
TASK [Show next available slot] ********************************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "103"
}
PLAY RECAP *****************************************************************************************************************************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
# Pass a list containing holes
$ ansible-playbook playbook.yml -e existing=100,101,102,103,105
PLAY [Find next available slot in a range] *********************************************************************************************************************************************************************************************
TASK [Show next available slot] ********************************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "104"
}
PLAY RECAP *****************************************************************************************************************************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
我想按顺序为变量赋值。第一次 运行 我想将 103 分配给变量的剧本,下次应该是 104。我还需要确保分配的值不在列表中。
变量:
list1:
- 100
- 101
- 102
val: 100
剧本:
- name: Set fact
set_fact:
val: "{{ val | int + 1 }}"
until: val not in list1
它尝试了 3 次但失败并出现以下错误。
FAILED - RETRYING: Set xxx (1 retries left). fatal: [xxx.com]: FAILED! => {"ansible_facts": {"val": "102"}, "attempts": 3, "changed": false}
我也试过with_sequence,但是它分配了100个值,最后的值是200,它只分配了第一个值就没有退出。有办法吗?
with_sequence: start=100 end=200
until: val not in list1
请指教
我可能会为此创建一个自定义过滤器,但这里有一种直接在 ansible 中执行此操作的方法。我将现有值作为额外的逗号分隔值传递给命令行,以便于测试。
想法:
- 创建一个序列,从占用的插槽开始,到最后一个数字之后的 1 个数字结束。
- 从该序列中减去现有值。
- 取结果列表中较低的值。
这是示例剧本:
---
- name: Find next available slot in a range
hosts: localhost
gather_facts: false
vars:
occupied_slots: "{{ existing.split(',') | map('int') }}"
range_start: "{{ occupied_slots | min }}"
range_end: "{{ occupied_slots | max + 2 }}"
next_available: "{{ (range(range_start | int, range_end | int)) | difference(occupied_slots) | min }}"
tasks:
- name: Show next available slot
debug:
msg: "{{ next_available }}"
给出:
# Pass a contiguous list
$ ansible-playbook playbook.yml -e existing=100,101,102
PLAY [Find next available slot in a range] *********************************************************************************************************************************************************************************************
TASK [Show next available slot] ********************************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "103"
}
PLAY RECAP *****************************************************************************************************************************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
# Pass a list containing holes
$ ansible-playbook playbook.yml -e existing=100,101,102,103,105
PLAY [Find next available slot in a range] *********************************************************************************************************************************************************************************************
TASK [Show next available slot] ********************************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "104"
}
PLAY RECAP *****************************************************************************************************************************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0