【问题标题】:Ansible aws_ec2 inventory never matching host groupAnsible aws_ec2 库存从不匹配主机组
【发布时间】:2020-09-02 09:01:15
【问题描述】:

首先,我可能在这里遗漏了一些非常简单的东西,我就是看不到它。我正在尝试将 ansible pull 与 aws_ec2 库存插件一起使用,以使用打包程序在 AWS 中的 EC2 实例上运行。我有以下库存文件:

plugin: aws_ec2
regions:
  - eu-west-1
  - eu-west-2
keyed_groups:
  - key: tags['role']
    prefix: enc
filters:
  instance-state-name: running
  tag:ami: "true

当我运行 ansible-inventory -i /tmp/ansible/inventories/dev/ami_inventory_aws_ec2.yml --list 时,我得到以下组:

"all": {
        "children": [
            "aws_ec2",
            "enc_web",
            "ungrouped"
        ]
    },
    "aws_ec2": {
        "hosts": [
            "ip-10-93-66-209.eu-west-1.compute.internal"
        ]
    },
    "enc_web": {
        "hosts": [
            "ip-10-93-66-209.eu-west-1.compute.internal"
        ]
    }

这是我期望看到的。然后在我的剧本中,我有以下内容:

---
- import_playbook: bootstrap.yml

- hosts: enc_web
  gather_facts: True
  roles:
    - name: enc.base
....

此时我认为运行 ansible-pullansible-playblook 有或没有 --limit enc_web ansible 会匹配组名并运行,但相反我得到了

[WARNING]: Could not match supplied host pattern, ignoring: ami-aws-web-01
ERROR! Specified hosts and/or --limit does not match any hosts

我在这里错过了一些非常基本的东西吗?它应该针对该组名中的所有主机运行吗?

我完整的 ansible 命令是:

ansible-pull --url=git@bitbucket.org:XXXXX/ansible.git --accept-host-key --directory=/tmp/ansible --full --checkout=web --private-key=/home/ubuntu/.ssh/ansible/ansible -e ami=true -e region=eu-west-1 --vault-password-file=~/.ssh/ansible/dev --inventory-file=/tmp/ansible/inventories/dev/ami_inventory_aws_ec2.yml playbooks/web.yml --limit enc_web

【问题讨论】:

  • 我在ansible-inventory 的输出中没有看到名为“ami-aws-web-01”的热门。无论如何,我认为在使用ansible-pull 时,更常见的是简单地定位localhost,因为您在它们要配置的主机上运行剧本。
  • 好的,所以您不能将整堆主机放入一个组并说部署到该组,而我不在乎主机的名称?
  • 您可以这样做,但主机需要是该组的成员,但它不是。这就是问题所在。
  • 正确,我更改了 aws_ec2 插件以将名称标签映射为主机名,现在一切正常。感谢@larsks 的帮助

标签: ansible ansible-inventory ansible-pull


【解决方案1】:

将库存文件更改为:

plugin: aws_ec2
regions:
 - eu-west-1
 - eu-west-2
keyed_groups:
 - key: tags['role']
    prefix: enc
filters:
  instance-state-name: running
  tag:ami: "true"
hostnames:
 - tag:Name

然后将主机名设置为需要匹配的名称。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-12
    • 1970-01-01
    相关资源
    最近更新 更多