【问题标题】:How to run plays using Ansible AWX on all hosts in a group using localhost如何使用 localhost 在组中的所有主机上使用 Ansible AWX 运行播放
【发布时间】:2020-02-20 21:07:40
【问题描述】:

我正在尝试创建一个脚本来创建一些 AWS Windows 服务器的 EC2 快照。我在理解如何获得正确的指令以确保一切正常运行时遇到了很多麻烦。我需要做的是:

  • 在本地运行 AWS 命令​​,即在 AWX 主机上(因为这比必须在每台服务器上配置凭据更可取)
  • 对清单中的每个主机运行命令

我过去曾在另一组 Linux 服务器上完成此操作,没有任何问题。但是我遇到这些问题的事实让我认为这并没有像我想象的那样工作(我对 Ansible/AWX 的所有事情都很陌生)。

第一步是我需要识别通常关闭的实例并打开它们,然后拍摄快照,如果它们通常关闭,则再次关闭它们。所以这是我的main.yml

---
- name: start the instance if the default state is stopped
  import_playbook: start-instance.yml
  when: default_state is defined and default_state == 'stopped'

- name: run the snapshot script
  import_playbook: instance-snapshot.yml

- name: stop the instance if the default state is stopped
  import_playbook: stop-instance.yml
  when: default_state is defined and default_state == 'stopped'

这是start-instance.yml

---
- name: make sure instances are running
  hosts: all
  gather_facts: false
  connection: local
  tasks:
    - name: start the instances
      ec2:
        instance_id: "{{ instance_id }}"
        region: "{{ aws_region }}"
        state: running
        wait: true
      register: ec2
    - name: pause for 120 seconds to allow the instance to start
      pause: seconds=120

当我运行它时,我收到以下错误:

fatal: [myhost,mydomain.com]: UNREACHABLE! => {
    "changed": false,
    "msg": "ssl: HTTPSConnectionPool(host='myhost,mydomain.com', port=5986): Max retries exceeded with url: /wsman (Caused by ConnectTimeoutError(<requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x7f99045e6bd0>, 'Connection to myhost,mydomain.com timed out. (connect timeout=30)'))",
    "unreachable": true

我知道这意味着它确实是在尝试连接到我不想要的每个 Windows 主机。但是,我认为 connection: local 解决了这个问题,就像我拥有的​​另一个模板一样,它使用了相同的剧本。

因此,如果我将 start-instance.yml 更改为“connection: localhost”,则播放会运行 - 但会跳过这些步骤,因为它确定没有主机满足条件(即 default_state is defined and default_state == 'stopped')。这告诉我,该剧正在使用 localhost 运行该剧,但也在针对 localhost 而不是 AWX 清单中的主机列表运行它。

我的主机在 AWX 中定义了变量,例如实例 ID、区域、默认状态。我知道在正常的 Ansible 使用中,我们会在 playbook 中有实例 ID,这就是 Ansible 知道要启动哪些 AWS 实例的方式,但在这种情况下,我需要它从 AWX 获取这些信息。

有没有办法做到这一点?那么,使用 localhost 对我的 AWX 清单中的所有主机运行任务(启动实例,暂停 120 秒)?

【问题讨论】:

    标签: ansible ansible-awx


    【解决方案1】:

    最后我为此使用了delegate_to。我将代码更改为:

    ---
    - name: make sure instances are running
      hosts: all
      gather_facts: false
      tasks:
        - name: start the instances
          delegate_to: localhost
          ec2:
            instance_id: "{{ instance_id }}"
            region: "{{ aws_region }}"
            state: running
            wait: true
          register: ec2
        - name: pause for 120 seconds to allow the instance to start
          pause: seconds=120
    

    并且 AWX 正确使用 localhost 来运行我添加委托的任务。

    值得注意的是,在意识到我的模板需要两组凭证之前,我被卡住了一段时间——一个具有正确权限的 IAM 用户运行 AWS 命令​​,另一个用于 Windows 实例的机器凭证。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-19
      • 2018-11-23
      相关资源
      最近更新 更多