【问题标题】:Stopping ansible from a playbook (site.yml)从剧本中停止 ansible (site.yml)
【发布时间】:2019-06-13 14:39:37
【问题描述】:

根据@Zeitounator 的建议,我已将问题重新编写得更具体,而不是使用我想要实现的通用示例。

我使用 ansible 通过在 hosts.ini 文件中添加一个新条目并运行 ansible-playbook -i inventory/dev/hosts.ini --limit SomeGroup playbooks/site.yml 来启动 VMware 中的虚拟机

vmware 角色(称为vmware)将 * 检查虚拟机是否已经存在。 * 如果是,那么显然它不会创建 VM。 * 如果不存在,则从模板创建虚拟机。

为了销毁一个虚拟机,我运行这个:ansible-playbook -i inventory/dev/hosts.ini --limit SomeGroup playbooks/site.yml -e 'vmware_destroy=true'

按预期工作。现在解决我的问题。

当这个变量被设置时(vmware_destroy=true),它会成功销毁虚拟机,但是 ansible 会尝试在刚刚被销毁的主机上继续执行 playbook 的其余部分。显然它失败了。由于失败,剧本确实停止了。但并不优雅。

这里是一个逻辑流程的例子:

$ cat playbooks/site.yml
---
- hosts: all
  gather_facts: no
  roles:
  - { role: vmware, tags: vmware }

- hosts: all
  gather_facts: yes
  roles:
  - { role: bootstrap, tags: bootstrap }
  - { role: common, tags: common }

- hosts: AppServers
  gather_facts: no
  roles:
  - { role: application }
# and so on.
$ cat playbooks/roles/vmware/main.yml
---
# Checks to see if the VM exists already.
# A variable `found_vm` is registered in this task.
- import_tasks: find.yml

# Only import this task when all of the `when` conditions are met.
- import_tasks: destroy.yml
  when:
    - vmware_destroy is defined
    - vmware_destroy # Meaning 'True'
    - found_vm

# If the above is true, it will not import this task.
- import_tasks: create.yml
  when:
    - found_vm.failed
    - vmware_destroy is not defined

所以,关键是,当我指定 -e 'vmware_destroy=true' 时,ansible 将尝试运行 playbook 的其余部分并失败。

我不希望 ansible 失败。我希望它在完成基于命令行上提供的-e 'vmware_destroy=true 标志的vmware 角色后优雅地停止。

我知道我可以为此使用不同的剧本,例如: ansible-playbook -i inventory/dev/hosts.ini --limit SomeGroup playbooks/VMWARE_DESTROY.yml。但我宁愿使用变量而不是单独的剧本。如果有强烈的论据以这种方式拆分剧本,请提供参考。

如果需要更多说明,请告诉我。

提前谢谢你。

【问题讨论】:

  • 我以为我很清楚我想做什么。在剧本到达终点之前停止剧本。我会进一步澄清这一点。
  • meta: end_play 结束一出戏,顾名思义,而不是剧本。由于第一次播放后没有停止的条件,只需从文件中删除您不想运行的其他播放(KISS 原则)......或者尝试更好地解释您想要实现的目标的根源,因为您可能不会走上正轨。

标签: ansible


【解决方案1】:

剧本是 Ansible 的顶层抽象层(剧本 -> 角色 -> 任务 -> ...)。 AWX 中还有 2 个层(工作流程 -> 作业模板 -> 剧本...)来控制剧本。要遵循架构,应使用 AWX 或任何其他与 Ansible 交互的工具(例如ansible-runner,或脚本)来控制剧本。

在 Ansible 中控制 playbook 相当尴尬。创建 2 个剧本 vmware-create.ymlvmware-destroy.yml

$ cat vmware-create.yml
- hosts: all
  gather_facts: yes  # include cached variables
  tasks:
    - block:
        - debug:
            msg: Don't create VM this time. End of play.
        - meta: end_play
      when: not hostvars['localhost'].vmware_create
    - debug:
        msg: Create VM.

$ cat vmware-destroy.yml
- hosts: all
  gather_facts: yes  # include cached variables
  tasks:
    - block:
        - debug:
            msg: Don't destroy VM this time. End of play.
        - meta: end_play
      when: not hostvars['localhost'].vmware_destroy
    - debug:
        msg: Destroy VM.

并将它们导入 playbook vmware_control.yml。见下文

$ cat vmware-control.yml
- hosts: localhost
  vars:
    vmware_create: true
    vmware_destroy: false
  tasks:
    - set_fact:
       vmware_create: "{{ vmware_create }}"     # cache variable
    - set_fact:
        vmware_destroy: "{{ vmware_destroy }}"  # cache variable

- import_playbook: vmware-create.yml
- import_playbook: vmware-destroy.yml

使用变量 vmware_createvmware_destroy 控制流程。在 localhost 运行 vmware_control.yml 并在 vmware-create.ymlvmware 中声明 hosts: all -destroy.yml.

【讨论】:

  • 来自docs.ansible.com/ansible/latest/modules/meta_module.html:“end_play(在 Ansible 2.2 中添加)会导致播放结束而不会使主机失败。请注意,这会影响所有主机。”所以它结束了剧本,而不是剧本。
  • @Vladimir,感谢您花时间为这个问题写答案。我已将问题改写为更具体而不是通用。干杯。
  • 我只能假设您没有尝试过条件导入。它行不通。 ERROR! this task 'import_playbook' has extra params, which is only allowed in the following modules: shell, win_shell, include_vars, add_host, raw, include_role, meta, set_fact, include, import_tasks, script, import_role, include_tasks, group_by, command, win_command 有条件地导入剧本是当前的功能请求。请参阅github.com/ansible/ansible/issues/34281#issuecomment-479013767 www 上的任何其他建议?我还没有找到一个不会造成不必要混乱的可行解决方案。
  • 你是对的。条件应在剧本中。我已经更新了答案。
猜你喜欢
  • 1970-01-01
  • 2020-05-08
  • 2023-03-17
  • 2022-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多