gpt4 book ai didi

ansible:变量未使用角色重新初始化

转载 作者:行者123 更新时间:2023-12-05 06:10:04 25 4
gpt4 key购买 nike

我有一个我不明白的奇怪行为

剧本“vars_roles.yml”:

---
- hosts: localhost
gather_facts: no
vars:
msg_role3: "first_run"

roles:
- role: role3

- hosts: localhost
gather_facts: no
vars:
msg_role3: "second_run"

roles:
- role: role3

角色 3 的内容:

cat roles/role3/defaults/main.yml

---
# defaults file for role3
add_value: false
msg_role3: "default"

tab_msg:
- "Init value: {{ msg_role3 }}"

cat roles/role3/tasks/main.yml

---
# tasks file for role3

- name: Value
debug:
msg: "Variable is {{ msg_role3 }}"
- name: Content of Array
debug:
var: tab_msg

- name: Add "value" into Array
set_fact:
tab_msg: "{{ (tab_msg) + [item] }}"
loop:
- "value: {{ msg_role3 }}"
when: add_value | bool

- name: Content of Array after add value
debug:
var: tab_msg

所以,我的问题来自带有列表 'tab_msg: "{{ (tab_msg) + [item] }}"' 的 set_fact如果执行此任务,列表“tab_msg”将保留角色“role3”第一次运行时的值

没有 set_fact 的例子:

ansible-playbook vars_roles.yml -e add_value=false
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

PLAY [localhost] ******************************************************************************************************************************************************

TASK [role3 : Value] **************************************************************************************************************************************************
Tuesday 27 October 2020 16:28:59 +0100 (0:00:00.055) 0:00:00.055 *******
ok: [localhost] => {
"msg": "Variable is first_run"
}

TASK [role3 : Content of Array] ***************************************************************************************************************************************
Tuesday 27 October 2020 16:28:59 +0100 (0:00:00.053) 0:00:00.108 *******
ok: [localhost] => {
"tab_msg": [
"Init value: first_run"
]
}

TASK [role3 : Add "value" into Array] *********************************************************************************************************************************
Tuesday 27 October 2020 16:28:59 +0100 (0:00:00.052) 0:00:00.161 *******
skipping: [localhost] => (item=value: first_run)

TASK [role3 : Content of Array after add value] ***********************************************************************************************************************
Tuesday 27 October 2020 16:28:59 +0100 (0:00:00.052) 0:00:00.214 *******
ok: [localhost] => {
"tab_msg": [
"Init value: first_run"
]
}

PLAY [localhost] ******************************************************************************************************************************************************

TASK [role3 : Value] **************************************************************************************************************************************************
Tuesday 27 October 2020 16:28:59 +0100 (0:00:00.058) 0:00:00.272 *******
ok: [localhost] => {
"msg": "Variable is second_run"
}

TASK [role3 : Content of Array] ***************************************************************************************************************************************
Tuesday 27 October 2020 16:28:59 +0100 (0:00:00.047) 0:00:00.319 *******
ok: [localhost] => {
"tab_msg": [
"Init value: second_run"
]
}

TASK [role3 : Add "value" into Array] *********************************************************************************************************************************
Tuesday 27 October 2020 16:28:59 +0100 (0:00:00.048) 0:00:00.368 *******
skipping: [localhost] => (item=value: second_run)

TASK [role3 : Content of Array after add value] ***********************************************************************************************************************
Tuesday 27 October 2020 16:28:59 +0100 (0:00:00.052) 0:00:00.420 *******
ok: [localhost] => {
"tab_msg": [
"Init value: second_run"
]
}

PLAY RECAP ************************************************************************************************************************************************************
localhost : ok=6 changed=0 unreachable=0 failed=0 skipped=2 rescued=0 ignored=0

播放 set_fact 的示例:

ansible-playbook vars_roles.yml -e add_value=true
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

PLAY [localhost] ******************************************************************************************************************************************************

TASK [role3 : Value] **************************************************************************************************************************************************
Tuesday 27 October 2020 16:31:44 +0100 (0:00:00.052) 0:00:00.052 *******
ok: [localhost] => {
"msg": "Variable is first_run"
}

TASK [role3 : Content of Array] ***************************************************************************************************************************************
Tuesday 27 October 2020 16:31:44 +0100 (0:00:00.052) 0:00:00.104 *******
ok: [localhost] => {
"tab_msg": [
"Init value: first_run"
]
}

TASK [role3 : Add "value" into Array] *********************************************************************************************************************************
Tuesday 27 October 2020 16:31:44 +0100 (0:00:00.049) 0:00:00.154 *******
ok: [localhost] => (item=value: first_run)

TASK [role3 : Content of Array after add value] ***********************************************************************************************************************
Tuesday 27 October 2020 16:31:44 +0100 (0:00:00.054) 0:00:00.208 *******
ok: [localhost] => {
"tab_msg": [
"Init value: first_run",
"value: first_run"
]
}

PLAY [localhost] ******************************************************************************************************************************************************

TASK [role3 : Value] **************************************************************************************************************************************************
Tuesday 27 October 2020 16:31:44 +0100 (0:00:00.053) 0:00:00.262 *******
ok: [localhost] => {
"msg": "Variable is second_run"
}

TASK [role3 : Content of Array] ***************************************************************************************************************************************
Tuesday 27 October 2020 16:31:44 +0100 (0:00:00.047) 0:00:00.309 *******
ok: [localhost] => {
"tab_msg": [
"Init value: first_run",
"value: first_run"
]
}

TASK [role3 : Add "value" into Array] *********************************************************************************************************************************
Tuesday 27 October 2020 16:31:44 +0100 (0:00:00.047) 0:00:00.357 *******
ok: [localhost] => (item=value: second_run)

TASK [role3 : Content of Array after add value] ***********************************************************************************************************************
Tuesday 27 October 2020 16:31:44 +0100 (0:00:00.055) 0:00:00.412 *******
ok: [localhost] => {
"tab_msg": [
"Init value: first_run",
"value: first_run",
"value: second_run"
]
}

PLAY RECAP ************************************************************************************************************************************************************
localhost : ok=8 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

有什么想法吗?

谢谢

最佳答案

结果,你看,是正确的。参见 Understanding variable precedence .在 set_fact 中赋值比角色的默认值 (2.) 具有更高的优先级 (19.)。除此之外,set_fact 中分配的值 will be available to subsequent plays during an ansible-playbook run .

在下面的简化示例中,set_fact (19.) 的优先级高于 play vars (12.)

shell> cat pb.yml
- hosts: localhost
vars:
msg_role3: "first_run"
tasks:
- debug:
var: msg_role3
- set_fact:
msg_role3: "Added value"
when: add_value|default(false)|bool

- hosts: localhost
vars:
msg_role3: "second_run"
tasks:
- debug:
var: msg_role3

给出(删节)

shell> ansible-playbook pb.yml

PLAY [localhost] ****

msg_role3: first_run

PLAY [localhost] ****

msg_role3: second_run
shell> ansible-playbook pb.yml -e "add_value=true"

PLAY [localhost] ****

msg_role3: first_run


PLAY [localhost] ****

msg_role3: Added value

您的问题的解决方案是从 defaults/main.yml 中删除 tab_msg 并将其放入 tasks/main.yml。例如

shell> cat roles/role3/defaults/main.yml
add_value: false
msg_role3: "default"
shell> cat roles/role3/tasks/main.yml
- name: Init tab_msg
set_fact:
tab_msg:
- "Init value: {{ msg_role3 }}"
- name: Value
...

关于ansible:变量未使用角色重新初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64557791/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com