如果不满足某些条件,如何跳过ansible剧本中的所有其他剧本?

2024-04-21

我在下面的剧本中有多个剧本。如果不满足某些条件,我想忽略所有其他戏剧。

因此,对于下面的示例 - 如果我在中找不到任何新文件Play1然后我不想执行Play2 and Play3根本没有(它应该跳过它)。我怎样才能做到这一点?

I have end_play在 Play1 中,但它只是跳过Play1它仍然执行Play2 and Play3

---
- name: Play 1
  hosts: 127.0.0.1
  tasks:
      - name: find the latest file
        find: paths=/var/lib/jenkins/jobs/process/workspace/files
              file_type=file
              age=-1m
              age_stamp=mtime
        register: files

      - meta: end_play
        when: files.files|count == 0

      - name: Copy file, if found
        copy:
          src: "some stuff here"
          dest: "some other stuff here"
        when: files.files|count > 0

- name: Play 2
  hosts: all
  serial: 5
  tasks:
      - name: copy latest file
        copy: src=data_init/goldy.init.qa dest=/data01/admin/files/goldy.init.qa owner=golden group=golden

      - name: copy latest file
        copy: src=data_init/goldy.init.qa dest=/data02/admin/files/goldy.init.qa owner=golden group=golden

- name: Play 3
  hosts: 127.0.0.1
  tasks:
      - name: execute command
        shell: ./data_init --init_file ./goldy.init.qa
        args:
          chdir: /var/lib/jenkins/jobs/process/workspace/data_init/

Update:

所以块模块解决方案看起来不起作用,因为你不能像这样在块中使用嵌套播放。


有两种方法可以解决您的问题:

  1. 尽管您尝试定义一个是可以理解的hosts: localhost要在控制器上运行某些任务,您实际上并不需要这个。使用delegate_to: localhost https://docs.ansible.com/ansible/latest/user_guide/playbooks_delegation.html#delegation and run_once: true https://docs.ansible.com/ansible/latest/user_guide/playbooks_delegation.html#run-once在您想要在控制器上运行的步骤上是一个更好的方法。
  2. 如果你真的想走这条路(使用hosts: localhost),出于某种原因,那么您需要保存您的回报find, with set_fact https://docs.ansible.com/ansible/latest/modules/set_fact_module.html?highlight=set_fact为了在全局的帮助下在其他主机上重用它hostvars https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html?highlight=hostvars#accessing-information-about-other-hosts-with-magic-variables

使用委托

剧中的任何任务都可以委托给另一位主持人。这做起来很简单,只需要额外使用一个delegate_to您的任务选项:

- name: Delegate a find to localhost
  find: 
    path: /test
    file_type: any
  register: localhost_find
  delegate_to: localhost
  run_once: true

这样做时,我建议您也使用run_once: true,因为,如果您将任务委托给另一台主机,则无需运行该任务(假设您的主机组中有十台主机)。

这是一个关于这个的小例子

---
- hosts: hosts
  become: true
  gather_facts: false

  tasks:
    - name: Delegate directory creation to localhost
      file: 
        path: /test
        state: directory
      delegate_to: localhost
      run_once: true

    - name: Create directory on hosts
      file:
        path: /test                 
        state: directory

    - name: Delegate file creation to localhost
      file:
        path: /test/localhost.txt                 
        state: touch
      delegate_to: localhost
      run_once: true

    - name: Create file on hosts
      file:
        path: /test/host.txt
        state: touch

    - name: Delegate a find to localhost
      find: 
        path: /test
        file_type: any
      register: localhost_find
      delegate_to: localhost
      run_once: true

    - name: Find in the hosts for comparison
      find:
        path: /test
        file_type: any
      register: host_find

    - name: List /test of localhost
      debug:
        msg: "{{ localhost_find.files | map(attribute='path') | list }}"

    - name: List /test of host
      debug:    
        msg: "{{ host_find.files | map(attribute='path') | list }}"      

    - name: Remove /test folder on localhost
      file:
        path: /test
        state: absent
      delegate_to: localhost
      run_once: true

    - name: Delegate an empty find to localhost
      find:
        path: /test
        file_type: any
      register: empty_find
      delegate_to: localhost
      run_once: true

    - name: Here are our hostnames from the inventory
      debug:
        msg: "{{ inventory_hostname }}"

    - name: I am the evil host killer
      meta: end_host
      when: empty_find.files | count == 0 and inventory_hostname != 'host1' 

    - debug:
        msg: "I am a sad message, because I will never display :'( But hopefully host1 likes me :')"

    - name: I am the evil playbook killer
      meta: end_play
      when: empty_find.files | count == 0

    - debug:
        msg: "I am a sad message, because I will never display :'( No one likes me..."

您可以看到,当我在之前的步骤中结束了主机组中三台主机中的两台时,我通过结束播放来跳过最后一条调试消息。

该剧本的输出:

PLAY [hosts] **************************************************************************************************************************

TASK [Delegate directory creation to localhost] ***************************************************************************************
ok: [host1 -> localhost]

TASK [Create directory on hosts] ****************************************************************************************************
ok: [host3]
ok: [host2]
ok: [host1]

TASK [Delegate file creation to localhost] ********************************************************************************************
changed: [host1 -> localhost]

TASK [Create file on hosts] ****************************************************************************************************
changed: [host2]
changed: [host1]
changed: [host3]

TASK [Delegate a find to localhost] ***************************************************************************************************
ok: [host1 -> localhost]

TASK [Find in the host for comparison] ************************************************************************************************
ok: [host1]
ok: [host3]
ok: [host2]

TASK [List /test of localhost] ********************************************************************************************************
ok: [host1] => {
    "msg": [
        "/test/localhost.txt"
    ]
}
ok: [host2] => {
    "msg": [
        "/test/localhost.txt"
    ]
}
ok: [host3] => {
    "msg": [
        "/test/localhost.txt"
    ]
}

TASK [List /test of host] *************************************************************************************************************
ok: [host1] => {
    "msg": [
        "/test/host.txt"
    ]
}
ok: [host2] => {
    "msg": [
        "/test/host.txt"
    ]
}
ok: [host3] => {
    "msg": [
        "/test/host.txt"
    ]
}

TASK [Remove /test folder on localhost] ***********************************************************************************************
changed: [host1 -> localhost]

TASK [Delegate an empty find to localhost] ********************************************************************************************
ok: [host1 -> localhost]

TASK [Here are our hostnames from the inventory] **************************************************************************************
ok: [host1] => {
    "msg": "host1"
}
ok: [host2] => {
    "msg": "host2"
}
ok: [host3] => {
    "msg": "host3"
}

TASK [debug] **************************************************************************************************************************
ok: [host1] => {
    "msg": "I am a sad message, because I will never display :'( But hopefully host1 likes me :')"
}

PLAY RECAP ****************************************************************************************************************************
host1                      : ok=12   changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
host2                      : ok=6    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
host3                      : ok=6    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

在输出中,您可以轻松地发现委托,因为将输出

changed: [host1 -> localhost]

当未委派的任务直接执行时

changed: [host1]

使用主机变量

我想说,这种方法不太先进,但如果您想委托处理边缘情况,它可能会变得很方便。我以前用过它,所以并不是所有情况下它都不能成为正确的解决方案。

这是例子

---
- hosts: localhost
  gather_facts: false

  tasks:
    - name: Find in localhost
      find:
        path: /not/existing/folder
        file_type: any
      register: find

    - name: This will register the list under find.files as a variable on the host, making it accessible via hostvars
      set_fact:
        find_result: "{{ find.files }}"

    - meta: end_play
      when: find.files | count == 0

    - debug:
        msg: I am the sanity check message, proving the end_play did happen


- hosts: hosts
  gather_facts: false

  tasks:
    - name: Just to show we are not cheating with an empty host group, we display the hosts names
      debug:
        msg: "{{ inventory_hostname }}"

    - name: To show it is really our empty list and not an empty string or a null variable
      debug:
        msg: "{{ hostvars['localhost']['find_result'] }}"

    - meta: end_play
      when: "hostvars['localhost']['find_result'] | count == 0"

    - debug:
        msg: I am a first sanity check message, proving the end_play did happen

    - debug:
        msg: I am a second sanity check message, proving the end_play did happen


- hosts: localhost
  gather_facts: false

  tasks:
    - name: To show it is really our empty list and not an empty string or a null variable
      debug:
        msg: "{{ hostvars['localhost']['find_result'] }}"

    - meta: end_play
      when: "hostvars['localhost']['find_result'] | count == 0"

    - debug:
        msg: I am a first sanity check message, proving the end_play did happen

    - debug:
        msg: I am a second sanity check message, proving the end_play did happen

您可以看到,在主机的每个新任务组开始时,我只是运行meta根据变量结束播放find_result注册出find result

这是这个的输出

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

TASK [Find in localhost] **************************************************************************************************************
ok: [localhost]

TASK [This will register the list under find.files as a variable on the host, making it accessible via hostvars] *********************
ok: [localhost]

PLAY [hosts] **************************************************************************************************************************

TASK [Just to show we are not cheating with an empty host group, we display the hosts names] ******************************************
ok: [host1] => {
    "msg": "host1"
}
ok: [host2] => {
    "msg": "host2"
}
ok: [host3] => {
    "msg": "host3"
}

TASK [To show it is really our empty list and not an empty string or a null variable] ************************************************
ok: [host1] => {
    "msg": []
}
ok: [host2] => {
    "msg": []
}
ok: [host3] => {
    "msg": []
}

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

TASK [To show it is really our empty list and not an empty string or a null variable] ************************************************
ok: [localhost] => {
    "msg": []
}

PLAY RECAP ****************************************************************************************************************************
host1                      : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
host2                      : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
host3                      : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如果不满足某些条件,如何跳过ansible剧本中的所有其他剧本? 的相关文章

随机推荐

  • 如何获取指向引用成员的指针?

    考虑这段代码 struct AA int rr 有没有办法获取指向的指针 或引用 AA rr为了获得这个 AA aa auto mm AA rr error cannot create pointer to reference member
  • CMake:如何在多个文件上运行自定义命令来生成源文件?

    我有以下情况 我想编译一些Scheme文件Gambit https github com gambit gambit成可执行文件 为此 我使用 gambit 将所有计划文件翻译 生成为 C 和目标文件 然后将其编译并链接为可执行文件 假设我
  • 数据库关系 1:1 vs 1:0..1

    我正在努力了解这些关系 正如我下面所示 它是否正确 1 我会有一个1 0 1通过简单地使父级的身份密钥也是子级的身份密钥来建立关系 2 为了做到这一点1 1 我在父表中添加一个FK 连接两个Identity列 这就是全部内容了吗 如果我的措
  • 在 Numpy 中预处理 Tensorflow 张量

    我在 Tensorflow 中设置了一个 CNN 用 TFRecordReader 读取数据 它工作得很好 但我想做一些比提供的更多的预处理和数据增强tf image功能 我特别想做一些随机缩放 是否可以在 Numpy 中处理 Tensor
  • 无法定义静态抽象字符串属性

    我遇到了一个有趣的问题 正在寻找一些关于如何最好地处理这个问题的建议 我有一个抽象类 其中包含一个静态方法 该方法接受我想定义为抽象属性的静态字符串 问题是 C 不支持以下内容 请参阅配置部分名称 and Current特性 public
  • 如何使用WebView中的id填充HTML输入中的值

    我的里面有 HTML 页面WebView有输入 输入有id 姓名 如何用一些字符串填充活动的输入 您可以尝试以下操作 mWebView loadUrl javascript document forms myform id value ne
  • 神经网络中“特征”的定义是什么?

    我是神经网络的初学者 我对这个词很困惑feature 你能给我一个定义吗feature 这些特征是隐藏层中的神经元吗 这些特征是输入向量的元素 特征的数量等于网络输入层的节点数量 如果您使用神经网络根据物理属性的测量将动物分类为猫或狗 那么
  • 构建失败 - TFS2008 上的 VS2010 解决方案

    我已将 VS2008 ASP NET MVC 解决方案迁移到 VS2010 MVC2 NET 4 0 该解决方案在本地构建 并且所有单元测试都通过 我们的 TFS 服务器仍然是 TFS2008 我在通过 CI 构建时遇到问题 项目全部构建成
  • 如何检测何时使用history.pushState和history.replaceState? [复制]

    这个问题在这里已经有答案了 当历史状态修改时 我可以订阅一些事件吗 如何 我曾经用它来通知何时pushState and replaceState叫做 Add this var wr function type var orig histo
  • 我可以删除[]一个指向已分配数组但不指向其开头的指针吗?

    我特别想知道以下情况 我在一些我必须使用的代码中发现的 SomeClass ar new SomeClass 2 ar delete ar 这段代码似乎工作正常 即没有崩溃 win32 用 VS2005 构建 这 合法 吗 感觉肯定不对 不
  • Windows 窗体应用程序的退出代码

    如何从 Windows 窗体应用程序返回非零退出代码 Application Exit 是退出应用程序的首选方法 但没有退出代码参数 我知道Environment Exit 但这不是关闭应用程序循环的好方法 Application Exit
  • 如果 Spring 应用程序无法连接到其配置服务器,则会快速失败

    假设您有一个 Spring 应用程序 它从配置服务器获取其配置 如果无法连接到配置服务器 应用程序将继续启动 但由于所有配置都丢失 它最终将失败并出现潜在的误导性错误 是否可以配置 Spring 使其在启动期间无法连接到其配置服务器时立即中
  • 将用户输入的字符串转换为正则表达式

    我正在用 HTML 和 JavaScript 设计一个正则表达式测试器 用户将输入正则表达式 字符串 并通过单选按钮选择他们想要测试的函数 例如搜索 匹配 替换等 程序将在使用指定参数运行该函数时显示结果 当然 会有额外的文本框用于替换额外
  • 为什么我的 UITableView 从 UITableViewStyleGrouped 更改为 UITableViewStylePlain

    我的应用程序有一个扩展的视图控制器UITableViewController 初始化方法如下所示 id initWithCoder NSCoder coder if self super initWithCoder coder self t
  • MySQL 用通配符替换

    我正在尝试编写 SQL 更新以用新字符串替换特定的 xml 节点 UPDATE table SET Configuration REPLACE Configuration
  • 如果 editable false,fullCalendar eventClick 处理程序将不起作用

    jQuery fullCalendar 插件似乎有问题可编辑模式 http arshaw com fullcalendar docs event ui editable 在某些情况下 如果事件点击处理程序 http arshaw com f
  • 从 android 中的 firebase 发送通知时没有通知声音

    我正在从 firebase 向我的 Android 应用程序发送推送通知 但是当我的应用程序处于后台时 不会调用 firebase onMessageReceived 方法 而是 firebase 向系统发送通知以在系统托盘中显示通知 通知
  • 无法通过 exec() 语句更改函数中的全局变量?

    为什么我不能使用 exec 从函数内部更改全局变量 当赋值语句位于 exec 之外时 它可以正常工作 这是我的问题的一个例子 gt gt gt myvar test gt gt gt def myfunc global myvar exec
  • 范围和插入符位置不匹配

    我在 MS Edge 中注意到这个问题 插入符号位置和范围在可编辑内容内不匹配或错误 c click function e var sel window getSelection var range sel getRangeAt 0 res
  • 如果不满足某些条件,如何跳过ansible剧本中的所有其他剧本?

    我在下面的剧本中有多个剧本 如果不满足某些条件 我想忽略所有其他戏剧 因此 对于下面的示例 如果我在中找不到任何新文件Play1然后我不想执行Play2 and Play3根本没有 它应该跳过它 我怎样才能做到这一点 I have end