有两个角色的剧本:运行角色 B 抱怨角色 A 的代码成功运行

如何解决有两个角色的剧本:运行角色 B 抱怨角色 A 的代码成功运行

我遇到了一个奇怪的行为:当我运行角色 B 时,它抱怨角色 A 的代码我可以成功运行!我已将此复制到这个最小示例中:

$ cat playbooka.yml 

- hosts:
    - host_a

  roles:
    - role: rolea
      tags:
        - taga
    - role: roleb
      tags:
        - tagb

我标记了两个角色,因为我想有选择地运行角色 A 或角色 B,它们由简单的任务组成,如下面的最小示例所示:

$ cat roles/rolea/tasks/main.yml

- name: Get service_facts
  service_facts:

- debug:
    msg: '{{ ansible_facts.services["amazon-ssm-agent"]["state"] }}'

- when: ansible_facts.services["amazon-ssm-agent"]["state"] != "running"
  meta: end_play

$ cat roles/roleb/tasks/main.yml

- debug:
    msg: "I am roleb"

预览确认我可以运行标签指定的单个角色:

$ ansible-playbook playbooka.yml -t taga -D -C --list-hosts --list-tasks

playbook: playbooka.yml

  play #1 (host_a): host_a  TAGS: []
    pattern: ['host_a']
    hosts (1):
      3.11.111.4
    tasks:
      rolea : Get service_facts TAGS: [taga]
      debug TAGS: [taga]

$ ansible-playbook playbooka.yml -t tagb -D -C --list-hosts --list-tasks

playbook: playbooka.yml

  play #1 (host_a): host_a  TAGS: []
    pattern: ['host_a']
    hosts (1):
      3.11.111.4
    tasks:
      debug TAGS: [tagb]

我可以运行角色 A 好的:

$ ansible-playbook playbooka.yml -t taga -D -C

PLAY [host_a] *************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************************************************************************************************************************
ok: [3.11.111.4]

TASK [rolea : Get service_facts] ******************************************************************************************************************************************************************************************************************
ok: [3.11.111.4]

TASK [rolea : debug] ******************************************************************************************************************************************************************************************************************************
ok: [3.11.111.4] => {
    "msg": "running"
}

PLAY RECAP ****************************************************************************************************************************************************************************************************************************************
3.11.111.4               : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

但是当我运行角色 B 时,它抱怨我刚刚成功运行的角色 A 中的代码!

$ ansible-playbook playbooka.yml -t tagb -D -C

PLAY [host_a] *************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************************************************************************************************************************
ok: [3.11.111.4]
ERROR! The conditional check 'ansible_facts.services["amazon-ssm-agent"]["state"] != "running"' failed. The error was: error while evaluating conditional (ansible_facts.services["amazon-ssm-agent"]["state"] != "running"): 'dict object' has no attribute 'services'

The error appears to be in '<path>/roles/rolea/tasks/main.yml': line 9,column 3,but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

- when: ansible_facts.services["amazon-ssm-agent"]["state"] != "running"
  ^ here
We could be wrong,but this one looks like it might be an issue with
unbalanced quotes. If starting a value with a quote,make sure the
line ends with the same set of quotes. For instance this arbitrary
example:

    foo: "bad" "wolf"

Could be written as:

    foo: '"bad" "wolf"'

我有两个问题:

  • 为什么要涉及角色 A 的代码?
  • 即使涉及,ansible_facts 也有 services,并且服务正在“运行”,如上图所示,运行角色 A。

PS:我在 MacOS 本地使用最新的 Ansible 2.10.2 和最新的 python 3.9.1。远程 python 可以是 2.7.12 或 3.5.2 (Ubuntu 16_04)。我通过测试字典是否有 services 键解决了这个问题:

ansible_facts.services is not defined or ansible_facts.services["amazon-ssm-agent"]["state"] != "running"

但仍然让我感到惊讶的是,角色 B 会解释角色 A 的代码并错误地解释它。这是我应该报告的错误吗?

解决方法

来自notes in meta module documentation

Ansible 2.11 之前不支持跳过带有标签的元任务。

自从您运行 ansible 2.10 以来,无论您使用什么标签,您在 when 中的 meta 任务的 rolea 条件都会被评估。当您使用 -t tagb 时,ansible_facts.services["amazon-ssm-agent"] 不存在,因为您跳过了 service_facts,然后您会收到您报告的错误。

您可以:

  • 升级到 ansible 2.11(我写这个答案可能有点快,因为它还不能通过 pip 使用...)
  • 重写您的条件,以便在 var 不存在时跳过 meta 任务,例如
    when:
      - ansible_facts.services["amazon-ssm-agent"]["state"] is defined
      - ansible_facts.services["amazon-ssm-agent"]["state"] != "running"
    

在任何情况下,第二个解决方案仍然是 IMO 的好做法(例如,与运行旧版本的人分享您的工作,在没有安装代理的情况下意外运行主机......)。

在您的特定情况下的另一种可能性是将 service_facts 任务移动到播放顺序更高的其他角色,或在您的剧本的 pre_tasks 部分,并将其标记为 always .在这种情况下,无论您使用什么标签,任务都将始终播放并且事实将始终存在。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res