Ansible-未指定的标记仍在执行
对我的剧本deploy.yml运行-list标记,我得到以下结果:Ansible-未指定的标记仍在执行,ansible,Ansible,对我的剧本deploy.yml运行-list标记,我得到以下结果: play #1 (localhost): Extract Variable Data From Jenkins Call TAGS: [main,extract] TASK TAGS: [extract, main] play #2 (localhost): Pull Repo to Localhost TAGS: [main,pullRepo] TASK TAGS: [main, pul
play #1 (localhost): Extract Variable Data From Jenkins Call TAGS: [main,extract]
TASK TAGS: [extract, main]
play #2 (localhost): Pull Repo to Localhost TAGS: [main,pullRepo]
TASK TAGS: [main, pullRepo]
play #3 (gms): Deploy GMS TAGS: [main,gmsDeploy]
TASK TAGS: [gmsDeploy, main]
play #4 (localhost): Execute SQL Scripts TAGS: [main,sql]
TASK TAGS: [main, sql]
play #5 (localhost): Configure Hosts for Copying TAGS: [main,config,tibco]
TASK TAGS: [config, main, tibco]
play #6 (copy_group): Copy Files between servers TAGS: [main,tibco]
TASK TAGS: [main, tibco]
play #7 (localhost): Deploy TIBCO TAGS: [main,tibco]
TASK TAGS: [main, tibco]
play #8 (localhost): Validate Deployment with GMS Heartbeat TAGS: [validate,main]
TASK TAGS: [main, validate]
play #9 (tibco): Clean Up TAGS: [main,cleanup]
TASK TAGS: [cleanup, main]
但是,当我运行ansible playbook deploy.yml-tags pullRepo试图只执行第二个重头戏时,它仍然试图执行重头戏4中的include任务。我认为问题在于包含的行为,而不是包含的任务,因为在第4场比赛中包含之前的任务不会执行。我希望问题是包含的任务没有标记。不幸的是,将sql标记添加到包含的任务中仍然会显示执行包含的任务的尝试
我知道我可以避免使用-skip标签运行它们,但我不应该这样做。有什么建议可以解释为什么会发生这种情况吗
我会发布剧本,但它有数百行包含专有信息,我似乎无法用一个简单的例子来复制这个问题
注意:我没有在剧本中使用任何角色,因此由于角色而应用于任务的标记不是一个因素。所有标签要么在整个剧本上,要么在剧本中的任务上,主要是前者
非常接近MCVE:
---
#:PLAY 2 - PULL REPO (LOCALLY)
- name: Pull Repo to Localhost
hosts: localhost
any_errors_fatal: true
tags:
- pullRepo
- main
gather_facts: no
tasks:
# Save the repository to the control machine (this machine) for distribution
- name: Pulling Git Repo for Ansible...
git:
repo: 'https://github.com/ansible/ansible.git'
dest: '/home/dholt2/ansi'
accept_hostkey: yes
force: yes
recursive: no
# Execute the sql files in the 'Oracle' directory after
## checking if the directory exists
#:PLAY 4 - TEST AND EXECUTE SQL (LOCALLY)
- name: Test & Execute SQL Scripts
hosts: localhost
any_errors_fatal: true
tags:
- sql
gather_facts: no
tasks:
# Check if the 'Oracle' directory exists. Save the
## output to deploy 'Oracle/*' if it does exist.
- name: Check for presence of the Oracle directory
stat:
path: '/home/dholt2/testing/Oracle'
register: st3
# Get a list of all sql scripts (using 'ls -v') to run ONLY if the 'Oracle'
## directory exists; exclude the rollback script; -v ensures natural ordering of the scripts
- name: Capture All Scripts To Run
shell: 'ls -v /home/dholt2/testing/Oracle -I rollback.sql'
register: f1
when: st3.stat.isdir is defined
# Test that the deployment scripts run without error
- name: Testing SQL Deployment Scripts...
include: testDeploySql.yml
any_errors_fatal: true
loop_control:
loop_var: deploy_item
with_items: "{{ f1.stdout_lines }}"
register: sqlDepl
when: st3.stat.isdir is defined
执行ansible playbook deploy2.yml-tags pullRepo-vvv的输出:
2017年10月16日编辑2:我一直遇到类似的问题。我经常在一起使用with_,其中一个项目总是用值填充,但另一个项目可能与上面的f1不同。因此,下面使用默认过滤器的答案不起作用,因为include总是有一些东西需要循环。幸运的是,我在Ansible的Github上发现了官方问题。它讨论了为什么会发生这种情况以及如何避免这种情况
从本质上讲,您仍然应该有默认的过滤器,但是您还需要向任务中添加static:no
这支持Konstantin关于为什么Ansible可能正在运行这些标记的声明,即使它们没有被指定
从Github问题:
…这是静态与动态include、继承以及在with循环中执行时的问题
问题是,对于静态包含和交互,Ansible不知道是否会跳过任务
我明白了
include不是一个常见的任务,它是一种语句,当以静态和动态方式使用时,该语句的行为会有所不同。
为了消除这种歧义,Ansible 2.4中引入了导入任务和包含任务。
请参阅最近关于serverfault的问题:
当您指定标记时,Ansible仍会在后台完成所有任务,并悄悄跳过那些没有相应标记的任务。但在您的例子中,当它遇到动态include时,它必须处理这个include语句,因为包含的文件可能包含用该标记标记的任务
但在include语句中有一些未定义的变量,所以它失败了。这是预期的行为。在第4场比赛中,你在包括的任务文件中有pullRepo标记吗?@KonstantinSuvorov没有,我只在第4场比赛中包括的任务上有单标记sql如果包括的任务中没有pullRepo或always标记,我想应该尝试提供MCVE…@KonstantinSuvorov是的。。。我希望我不必这么做,但看起来这是必须的。如果我想做一个更新,我会在这里发布。@KonstantinSuvorov我已经添加了示例和运行它的输出。我想使用include_tasks模块,但我目前正在运行Ansible 2.3.1.0。你认为值得为Ansible在GitHub上做一个bug报告吗?我知道你说它是一个动态包含,但是包含本身是静态的,是with_items指令使它成为动态的是吗?因此,它应该仍然能够正确地注入新任务,并按照标签说明进行操作。我知道它应该这样做,因为如果我在sql中添加-skip标记,那么它就不会执行include任务。只需将循环语句调整为包含以下项:{{f1 | defaultdictstdout_lines=[].stdout_lines}},您就安全了。如果f1未定义,这将跳过包含。我的原始剧本中有另一个项目,但为了简单起见,没有包含在MCVE中。我把它作为编辑放在主要问题中。本质上,它是一个不包含with_项的include,因为它是第一个脚本失败时的回滚。也许我可以修改when语句,使用类似于您对_items?when所做的操作:st3 | default{}.stat | default{}.isdir已定义。谢谢。你能告诉我dict在文件里的位置吗?谷歌搜索没有找到它。
PLAYBOOK: deploy2.yml *********************************************************************************************************************************************************************************************************************************************************
2 plays in deploy2.yml
PLAY [Pull Repo to Localhost] *************************************************************************************************************************************************************************************************************************************************
META: ran handlers
TASK [Pulling Git Repo for Ansible...] ********************************************************************************************************************************************************************************************************************************************
task path: /home/dholt2/test/deploy2.yml:17
Using module file /usr/lib/python2.6/site-packages/ansible/modules/source_control/git.py
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: dholt2
<127.0.0.1> EXEC /bin/sh -c 'echo ~ && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/dholt2/.ansible/tmp/ansible-tmp-1506540242.05-264367109987412 `" && echo ansible-tmp-1506540242.05-264367109987412="` echo /home/dholt2/.ansible/tmp/ansible-tmp-1506540242.05-264367109987412 `" ) && sleep 0'
<127.0.0.1> PUT /tmp/tmpVWukIT TO /home/dholt2/.ansible/tmp/ansible-tmp-1506540242.05-264367109987412/git.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /home/dholt2/.ansible/tmp/ansible-tmp-1506540242.05-264367109987412/ /home/dholt2/.ansible/tmp/ansible-tmp-1506540242.05-264367109987412/git.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '/usr/bin/python2.6 /home/dholt2/.ansible/tmp/ansible-tmp-1506540242.05-264367109987412/git.py; rm -rf "/home/dholt2/.ansible/tmp/ansible-tmp-1506540242.05-264367109987412/" > /dev/null 2>&1 && sleep 0'
changed: [localhost] => {
"after": "5c3bbd4620c4a94ece7741ecfe5514a1bd06422b",
"before": null,
"changed": true,
"invocation": {
"module_args": {
"accept_hostkey": true,
"bare": false,
"clone": true,
"depth": null,
"dest": "/home/dholt2/ansi",
"executable": null,
"force": true,
"key_file": null,
"recursive": false,
"reference": null,
"refspec": null,
"remote": "origin",
"repo": "https://github.com/ansible/ansible.git",
"ssh_opts": null,
"track_submodules": false,
"umask": null,
"update": true,
"verify_commit": false,
"version": "HEAD"
}
}
}
META: ran handlers
META: ran handlers
PLAY [Test & Execute SQL Scripts] *********************************************************************************************************************************************************************************************************************************************
META: ran handlers
TASK [Testing SQL Deployment Scripts...] **************************************************************************************************************************************************************************************************************************************
task path: /home/dholt2/test/deploy2.yml:54
fatal: [localhost]: FAILED! => {
"failed": true,
"msg": "'f1' is undefined"
}
NO MORE HOSTS LEFT ************************************************************************************************************************************************************************************************************************************************************
PLAY RECAP ********************************************************************************************************************************************************************************************************************************************************************
localhost : ok=1 changed=1 unreachable=0 failed=1
# Test that the rollback script runs without error
- name: Testing SQL Rollback Script...
any_errors_fatal: true
include: testRollbackSql.yml
register: sqlRoll
when: st3.stat.isdir is defined