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

ansible:未使用角色重新初始化变量,ansible,ansible-role,Ansible,Ansible Role,我有一种奇怪的行为,我不明白 剧本“vars_roles.yml”: 角色3的内容: cat角色/role3/defaults/main.yml cat角色/role3/tasks/main.yml 因此,我的问题来自于列表“tab_msg:{{(tab_msg)+[item]}”的set_事实 如果执行此任务,列表“tab_msg”将保留角色“role3”第一次运行时的值 不带set_事实的示例: ansible-playbook vars_roles.yml -e add_value=fal

我有一种奇怪的行为,我不明白

剧本“vars_roles.yml”:

角色3的内容:

cat角色/role3/defaults/main.yml

cat角色/role3/tasks/main.yml

因此,我的问题来自于列表“tab_msg:{{(tab_msg)+[item]}”的set_事实 如果执行此任务,列表“tab_msg”将保留角色“role3”第一次运行时的值

不带set_事实的示例:

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_事实的示例:

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
有什么想法吗


谢谢,你看,结果是正确的。看见中指定的值的优先级(19)高于角色的默认值(2)。除此之外,set_fact中指定的值

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

shell>cat pb.yml
-主机:本地主机
变量:
msg_role3:“第一次运行”
任务:
-调试:
变量:msg_role3
-设定事实:
信息角色3:“附加值”
时间:添加|值|默认值(false)|布尔
-主机:本地主机
变量:
msg_role3:“第二次运行”
任务:
-调试:
变量:msg_role3
给出(节略)

shell>ansible剧本pb.yml
播放[本地主机]****
msg_role3:第一次运行
播放[本地主机]****
msg_role3:第二次运行
shell>ansible playbook pb.yml-e“add_value=true”
播放[本地主机]****
msg_role3:第一次运行
播放[本地主机]****
味精角色3:附加值

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

shell>cat角色/role3/defaults/main.yml
添加值:false
msg_role3:“默认值”
shell>cat角色/role3/tasks/main.yml
-名称:Init tab_msg
设定事实:
tab_msg:
-“初始值:{{msg_role3}}”
-名称:Value
...

好的,我明白了。我的问题是变量“tab_msg”在主剧本中是已知的,我需要在角色中加入类似私有变量的内容。或者,在任务中初始化变量。我在答案中加了一个例子。
---
# 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
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
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