这是多余的ansible标签吗?
虽然许多人可能喜欢ansible,但在我看来,它似乎与它有点混淆,有些冗余的概念,如这是多余的ansible标签吗?,ansible,ansible-role,Ansible,Ansible Role,虽然许多人可能喜欢ansible,但在我看来,它似乎与它有点混淆,有些冗余的概念,如标签,角色,目录/主机文件,主机组,剧本,任务“何时”条件句要确定最终将执行什么,有相当多的挑战。这个问题的核心问题是 我的理解是,如果我有一个剧本,其中有一个角色包含一个标签列表,那么这些标签将被所有包含的任务添加(继承) 例如: [----./playbook.yml----] --- - hosts: all roles: - {role: role01, tags: ['tag01', 'ta
标签
,角色
,目录/主机文件
,主机组
,剧本
,任务
“何时”条件句
要确定最终将执行什么,有相当多的挑战。这个问题的核心问题是
我的理解是,如果我有一个剧本,其中有一个角色包含一个标签列表,那么这些标签将被所有包含的任务添加(继承)
例如:
[----./playbook.yml----]
---
- hosts: all
roles:
- {role: role01, tags: ['tag01', 'tag02']}
- {role: role02, tags: ['tag01', 'tag03']}
这意味着在/roles/role01/tasks/main.yml
中找到的所有任务都将被隐式标记为tag01
和tag02
。确认这一点是回答这个问题的一部分
同样地,/roles/role02/tasks/main.yml
中的所有任务也因此被标记为tag01
和tag03
,对吗
现在这里的问题是,查看文件/roles/role01/tasks/main.yml
我看到了这个内容
[----./roles/role01/tasks/main.yml----]
---
- block:
- name: "upgrade all packages"
yum:
name: '*'
state: latest
tags:
- tag01
考虑到该角色仅从上面所示的/playbook.yml
行中引用过,我看不出使用tag01
有什么意义,因为该行应隐式标记该角色:
-{role:role01,标记:['tag01','tag02']}
另外,这里还必须使用另一个ansible概念块
,这让我完全不安
问题的各个方面:
main.yml
superflous中是否有标记TL;Dr:在问题中解释的情况下确实是这样(ansible角色级别的隐式标记使角色内部的单个任务/块标记变得不必要,因此是多余的) 详细信息: Ansible的语法和风格往往过于复杂和扭曲。在这种情况下,它只是一个不必要的重复标记,加上同样不必要的使用
块
(在ansible playbook中)的语法元素
ansible在标签方面的行为如何得到验证/测试?=>使用shell/bash sctipt:
我已经求助于这个shell脚本,它应该测试ansible标记
如何与角色的隐式继承相关
bash脚本将创建一个最小的ansible测试用例,它
包含两个角色role1
和role2
,均为逐字复制
除了名字(以便能够告诉他们)和
有两项任务:
未明确标记任务(A)
是用标签“tag01”明确标记的李>task(B)
playbook.yml
和主机/目录文件inventory.ini
在脚本中创建:
#!/bin/bash
#make a temporary directory for test
TEMPDIR="$(mktemp -d)"
#change to temporary directory
cd "$TEMPDIR"
# create ansible hosts file "./inventory.ini"
# only host we need is the the local box (to be simple disregard any ssh overhead)
cat > inventory.ini <<'EOF'
[hosts]
localhost ansible_connection=local
EOF
# create ansilble role "role1"
# (to keep it simple, we have minimal role with only a tasks entry)
mkdir -p roles/role1/tasks
# (the content for the tasks/main.yml file are two tasks, with one task
# namely task(A) not being explicitly and the task(B) being explicitly
# tagged)
cat > roles/role1/tasks/main.yml <<'EOF'
---
# tasks file for role1 and role2
- name: "task(A) tagged not explicitly 'tag01'"
debug:
msg: "debug-msg: this is \"task(A) tag explicitly 'tag01'\" {{ role_name }} ."
- name: "task(B) tagged explicitly 'tag01'"
debug:
msg: "debug-msg: this is \"task(B) tagged explicitly 'tag01'\" {{ role_name }} ."
tags:
- tag01
EOF
# create ansible role "role2" as a verbatim copy of "role1"
cp -r roles/role1 roles/role2
# now create an ansible playbook that uses these two ansible roles
# (note that for the role "role1" we used "implicit tagging"
cat > playbook.yml <<'EOF'
---
- hosts: all
roles:
- role: "role1"
tags:
- "tag01"
- role: "role2"
EOF
# show the test setup
find .
# TEST 1:
# run "playbook.yml" one time "as is" ( i.e. no using of tags -> "all tasks"
# should be run)
echo "TEST1" | tee TEST1
ansible-playbook -i inventory.ini playbook.yml | tee -a TEST1
# TEST 2:
# run "playbook.yml" with --tags command line options ( meaning this time
# we limit the task to be run to only those that have the specific tag)
echo "TEST2" | tee TEST2
ansible-playbook -i inventory.ini --tags 'tag01' playbook.yml | tee -a TEST2
# TEST 3:
# run "playbook.yml" with --skip-tags command line options ( meaning this time
# we limit the task to be run all, but those to tags to be "skipped")
echo "TEST3" | tee TEST3
ansible-playbook -i inventory.ini --skip-tags 'tag01' playbook.yml | tee -a TEST3
# show path of temporary directory
echo "TEMPDIR is $TEMPDIR"
tag01
设置为role1
的角色级别
包含的任务任务(A)和任务(B)
都运行。相反,对于role2
而言,如果没有隐式继承的标记,则只运行任务(B)
。结果表明:角色
已经(a)在playbook
中标记了自己,从而使其所有任务隐式继承任务,并且b)这是唯一使用的playbook
角色(就像这里的情况一样)
尽管可以通过执行此处提供的shell脚本生成输出,但出于礼貌和完整性考虑,输出是:
.
./playbook.yml
./roles
./roles/role2
./roles/role2/tasks
./roles/role2/tasks/main.yml
./roles/role1
./roles/role1/tasks
./roles/role1/tasks/main.yml
./inventory.ini
TEST1
PLAY [all] **************************************************************************
TASK [Gathering Facts] **************************************************************************
ok: [localhost]
TASK [role1 : task(A) tagged not explicitly 'tag01'] **************************************************************************
ok: [localhost] => {
"msg": "debug-msg: this is \"task(A) tag explicitly 'tag01'\" role1 ."
}
TASK [role1 : task(B) tagged explicitly 'tag01'] **************************************************************************
ok: [localhost] => {
"msg": "debug-msg: this is \"task(B) tagged explicitly 'tag01'\" role1 ."
}
TASK [role2 : task(A) tagged not explicitly 'tag01'] **************************************************************************
ok: [localhost] => {
"msg": "debug-msg: this is \"task(A) tag explicitly 'tag01'\" role2 ."
}
TASK [role2 : task(B) tagged explicitly 'tag01'] **************************************************************************
ok: [localhost] => {
"msg": "debug-msg: this is \"task(B) tagged explicitly 'tag01'\" role2 ."
}
PLAY RECAP **************************************************************************
localhost : ok=5 changed=0 unreachable=0 failed=0
TEST2
PLAY [all] **************************************************************************
TASK [Gathering Facts] **************************************************************************
ok: [localhost]
TASK [role1 : task(A) tagged not explicitly 'tag01'] **************************************************************************
ok: [localhost] => {
"msg": "debug-msg: this is \"task(A) tag explicitly 'tag01'\" role1 ."
}
TASK [role1 : task(B) tagged explicitly 'tag01'] **************************************************************************
ok: [localhost] => {
"msg": "debug-msg: this is \"task(B) tagged explicitly 'tag01'\" role1 ."
}
TASK [role2 : task(B) tagged explicitly 'tag01'] **************************************************************************
ok: [localhost] => {
"msg": "debug-msg: this is \"task(B) tagged explicitly 'tag01'\" role2 ."
}
PLAY RECAP **************************************************************************
localhost : ok=4 changed=0 unreachable=0 failed=0
TEST3
PLAY [all] **************************************************************************
TASK [Gathering Facts] **************************************************************************
ok: [localhost]
TASK [role2 : task(A) tagged not explicitly 'tag01'] **************************************************************************
ok: [localhost] => {
"msg": "debug-msg: this is \"task(A) tag explicitly 'tag01'\" role2 ."
}
PLAY RECAP **************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0
TEMPDIR is /tmp/tmp.cPE6a4gGiG
TEST2
PLAY [all] **************************************************************************
TASK [Gathering Facts] **************************************************************************
ok: [localhost]
TASK [role1 : task(A) tagged not explicitly 'tag01'] **************************************************************************
ok: [localhost] => {
"msg": "debug-msg: this is \"task(A) tag explicitly 'tag01'\" role1 ."
}
TASK [role1 : task(B) tagged explicitly 'tag01'] **************************************************************************
ok: [localhost] => {
"msg": "debug-msg: this is \"task(B) tagged explicitly 'tag01'\" role1 ."
}
TASK [role2 : task(B) tagged explicitly 'tag01'] **************************************************************************
ok: [localhost] => {
"msg": "debug-msg: this is \"task(B) tagged explicitly 'tag01'\" role2 ."
}
PLAY RECAP **************************************************************************
localhost : ok=4 changed=0 unreachable=0 failed=0
TEST3
PLAY [all] **************************************************************************
TASK [Gathering Facts] **************************************************************************
ok: [localhost]
TASK [role2 : task(A) tagged not explicitly 'tag01'] **************************************************************************
ok: [localhost] => {
"msg": "debug-msg: this is \"task(A) tag explicitly 'tag01'\" role2 ."
}
PLAY RECAP **************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0
.
./playbook.yml
./roles
./roles/role2
./roles/role2/tasks
./roles/role2/tasks/main.yml
./roles/role1
./roles/role1/tasks
./roles/role1/tasks/main.yml
./inventory.ini
TEST1
PLAY [all] **************************************************************************
TASK [Gathering Facts] **************************************************************************
ok: [localhost]
TASK [role1 : task(A) tagged not explicitly 'tag01'] **************************************************************************
ok: [localhost] => {
"msg": "debug-msg: this is \"task(A) tag explicitly 'tag01'\" role1 ."
}
TASK [role1 : task(B) tagged explicitly 'tag01'] **************************************************************************
ok: [localhost] => {
"msg": "debug-msg: this is \"task(B) tagged explicitly 'tag01'\" role1 ."
}
TASK [role2 : task(A) tagged not explicitly 'tag01'] **************************************************************************
ok: [localhost] => {
"msg": "debug-msg: this is \"task(A) tag explicitly 'tag01'\" role2 ."
}
TASK [role2 : task(B) tagged explicitly 'tag01'] **************************************************************************
ok: [localhost] => {
"msg": "debug-msg: this is \"task(B) tagged explicitly 'tag01'\" role2 ."
}
PLAY RECAP **************************************************************************
localhost : ok=5 changed=0 unreachable=0 failed=0
TEST2
PLAY [all] **************************************************************************
TASK [Gathering Facts] **************************************************************************
ok: [localhost]
TASK [role1 : task(A) tagged not explicitly 'tag01'] **************************************************************************
ok: [localhost] => {
"msg": "debug-msg: this is \"task(A) tag explicitly 'tag01'\" role1 ."
}
TASK [role1 : task(B) tagged explicitly 'tag01'] **************************************************************************
ok: [localhost] => {
"msg": "debug-msg: this is \"task(B) tagged explicitly 'tag01'\" role1 ."
}
TASK [role2 : task(B) tagged explicitly 'tag01'] **************************************************************************
ok: [localhost] => {
"msg": "debug-msg: this is \"task(B) tagged explicitly 'tag01'\" role2 ."
}
PLAY RECAP **************************************************************************
localhost : ok=4 changed=0 unreachable=0 failed=0
TEST3
PLAY [all] **************************************************************************
TASK [Gathering Facts] **************************************************************************
ok: [localhost]
TASK [role2 : task(A) tagged not explicitly 'tag01'] **************************************************************************
ok: [localhost] => {
"msg": "debug-msg: this is \"task(A) tag explicitly 'tag01'\" role2 ."
}
PLAY RECAP **************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0
TEMPDIR is /tmp/tmp.cPE6a4gGiG