首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Ansible:仅当前一个任务成功并创建输出时才触发任务

Ansible:仅当前一个任务成功并创建输出时才触发任务
EN

Stack Overflow用户
提问于 2020-05-15 00:20:11
回答 1查看 776关注 0票数 1

我正在使用ansible和在接下来的任务中创建的公共ip在azure中部署VM。但创建公网ip耗时过长,导致后续任务执行失败。创建ip的时间也不同,它不是固定的。我想介绍一些逻辑,其中下一个任务仅在创建ip时运行。

代码语言:javascript
运行
复制
- name: Deploy Master Node
  azure_rm_virtualmachine:
    resource_group: myResourceGroup
    name: testvm10
    admin_username: chouseknecht
    admin_password: <your password here>
    image:
      offer: CentOS-CI
      publisher: OpenLogic
      sku: '7-CI'
      version: latest

有人能帮我一下吗..!非常感谢。

EN

回答 1

Stack Overflow用户

发布于 2020-05-15 04:56:15

我认为wait_for模块是一个不好的选择,因为虽然它可以测试端口可用性,但它经常会给出误报,因为端口在服务真正准备好接受连接之前就已经打开了。

幸运的是,wait_for_connection模块正是为您所描述的情况而设计的:它将等待Ansible能够成功连接到您的目标。

这通常需要您使用Ansible清单注册您的Azure VM (例如,使用add_host模块)。我不使用Azure,但如果我用OpenStack做这件事,我可能会写成这样:

代码语言:javascript
运行
复制
- hosts: localhost
  gather_facts: false
  tasks:
    # This is the task that creates the vm, much like your existing task
    - os_server:
        name: larstest
        cloud: kaizen-lars
        image: 669142a3-fbda-4a83-bce8-e09371660e2c
        key_name: default
        flavor: m1.small
        security_groups: allow_ssh
        nics:
          - net-name: test_net0
        auto_ip: true
      register: myserver

    # Now we take the public ip from the previous task and use it
    # to create a new inventory entry for a host named "myserver".
    - add_host:
        name: myserver
        ansible_host: "{{ myserver.openstack.accessIPv4 }}"
        ansible_user: centos

# Now we wait for the host to finished booting. We need gather_facts: false here
# because otherwise Ansible will attempt to run the `setup` module on the target,
# which will fail if the host isn't ready yet.
- hosts: myserver
  gather_facts: false
  tasks:
    - wait_for_connection:
        delay: 10

# We could add additional tasks to the previous play, but we can also start
# new play with implicit fact gathering.
- hosts: myserver
  tasks:
    - ...other tasks here...
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61802327

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档