Ansible - 在收集事实之前执行操作

57

在收集事实信息之前,有人知道如何执行某些操作(例如等待受控节点的端口/引导)吗?我知道我可以关闭收集事实信息。

gather_facts: no

那么如果我在等待节点启动的同时也需要了解相关信息怎么办呢?

3个回答

76

收集事实相当于运行setup模块。您可以通过手动运行它来收集信息。这并没有记录在文档中,但只需添加如下任务:

- name: Gathering facts
  setup:
gather_facts: no与playbook的组合将在执行以上任务时仅获取事实(facts)。 在示例playbook中两者皆可:
- hosts: all
  gather_facts: no
  tasks:

    - name: Some task executed before gathering facts
      # whatever task you want to run

    - name: Gathering facts
      setup:

24

应该可以这样做:

- hosts: my_hosts
  gather_facts: no

  tasks:
      - name: wait for SSH to respond on all hosts
        local_action: wait_for port=22

      - name: gather facts
        setup:

      - continue with my tasks...

wait_for 命令将在您的 ansible 主机上本地执行,等待服务器在端口22上响应,然后 setup 模块将执行事实收集,之后您可以执行其他任何需要的操作。


2

我正在尝试弄清楚如何从EC2中提供主机,等待SSH启动,然后运行我的Playbook。这与您的使用情况基本相同。最终我得出了以下结论:

- name: Provision App Server from Amazon
  hosts: localhost
  gather_facts: False
  tasks:  
    # ####  call ec2 provisioning tasks here  ####
    - name: Add new instance to host group
      add_host: hostname="{{item.private_ip}}" groupname="appServer"
      with_items: ec2.instances

- name: Configure App Server
  hosts: appServer
  remote_user: ubuntu
  gather_facts: True
  tasks:  ----configuration tasks here----

我认为ansible术语是我在playbook中有两个plays,每个play都在不同的主机组上操作(localhost和appServer组)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接