AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 698647
Accepted
Thunder Rabbit
Thunder Rabbit
Asked: 2022-04-11 03:02:34 +0800 CST2022-04-11 03:02:34 +0800 CST 2022-04-11 03:02:34 +0800 CST

使用 Ansible 2.12 通过 host: 标签类访问 AWS EC2

  • 772

在我的本地硬件上,我有一个运行 Ubuntu 20 的 Vagrant 盒子,我正在使用 Ansible 2.12.2

我能够访问 AWS,甚至可以在 VPN 中创建 EC2 实例。

当我查看库存时,我可以看到 EC2 服务器为:

"ec2-64-135-69-12.us-west-1.compute.amazonaws.com": {
    ...,
    "tags": {
        "Details": "File server and api",
        "Name": "File server via Ansible",
        "OS": "Ubuntu20",
        "Type": "Image Server",
        "class": "classfileserver2022"
    },
    ...
},

在我的下一个剧本中,我可以通过以下方式访问服务器

hosts: "ec2-64-135-69-12.us-west-1.compute.amazonaws.com"

但我更愿意通过上面 json 中的任何标签来访问它。

我努力了

hosts: "tags_class_classfileserver2022"

和

hosts:
  - tags:Class="classfileserver2022"

但我收到类似的错误

[WARNING]: Could not match supplied host pattern, ignoring: tags_class_classfileserver2022
skipping: no hosts matched

如何使用类标签访问 EC2 主机?(或任何其他标签..)

我的剧本如下:

---
  - name: "Prepare base of {{ server_name }} box"
    vars_files:
      - vars/0000_vars.yml
      - vars/vars_for_base_provision.yml
      - vars/vars_for_geerling.security.yml
#    hosts: "ec2-54-153-39-10.us-west-1.compute.amazonaws.com"   <-- this works
    hosts: "tags_Class_{{ tag_class }}"
    remote_user: ubuntu
    become: yes
    gather_facts: no

    pre_tasks:
    - name: Check for single host
      fail: msg="Single host check failed.  Try --limit or change `hosts` above."
      when: "{{ ansible_play_batch|length }} != 1"

    roles:
      - { role: geerlingguy.security }
ansible amazon-ec2
  • 2 2 个回答
  • 155 Views

2 个回答

  • Voted
  1. Best Answer
    Panki
    2022-04-15T01:55:34+08:002022-04-15T01:55:34+08:00

    考虑阅读ansible 文档中的“库存插件”部分。

    要开始使用带有 YAML 配置源的清单插件,请使用为相关插件记录的可接受文件名架构创建一个文件,然后添加插件:plugin_name。如果插件在集合中,请使用完全限定名称。

    # demo.aws_ec2.yml
    plugin: amazon.aws.aws_ec2
    

    [...] 您可以使用带有构造的 keyed_groups 选项的主机变量创建动态组。选项组也可用于创建组和组合创建和修改主机变量。这是一个利用构造特征的 aws_ec2 示例:

    # demo.aws_ec2.yml
    plugin: amazon.aws.aws_ec2
    regions:
      - us-east-1
      - us-east-2
    keyed_groups:
      # add hosts to tag_Name_value groups for each aws_ec2 host's tags.Name variable
      - key: tags.Name
        prefix: tag_Name_
        separator: ""
    groups:
      # add hosts to the group development if any of the dictionary's keys or values is the word 'devel'
      development: "'devel' in (tags|list)"
    compose:
      # set the ansible_host variable to connect with the private IP address without changing the hostname
      ansible_host: private_ip_address
    

    [...] 您可以使用ansible-doc -t inventory -l来查看可用插件的列表。用于ansible-doc -t inventory <plugin name>查看特定于插件的文档和示例。

    • 2
  2. Thunder Rabbit
    2022-04-15T19:17:49+08:002022-04-15T19:17:49+08:00

    根据 Panki 的回答,这就是为我解决的问题

    # demo.aws_ec2.yml
    inventory-plugins
    plugin: amazon.aws.aws_ec2
    regions:
      - us-west-1
    keyed_groups:
      - key: tags.class    # <-- note: lowercase c
        prefix: tags_Class_
        separator: ""
    

    示例剧本匹配标签:类:uniqueclassname

    # example_playbook.yml
    ---
      - name: "Playbook for {{ server_name }} EC2 instance"
        vars_files:
          - vars/0000_vars.yml
        hosts: "tags_Class_{{ tag_class }}"
        remote_user: ubuntu
        become: yes
        gather_facts: no
    
        roles:
          - { role: xxxxxxx }
    

    与剧本一起创建的变量:

    # vars/0000_vars.yml
    tag_class: "uniqueclassname"
    server_name: "My Fancy Server"
    
    • 1

相关问题

  • ansible中事实变量的分隔符值

  • SSH 进入 linux AWS 盒子,如何退出终端而不关闭 AWS 盒子中运行的脚本?[复制]

  • 剧本给出语法错误

  • sshpass 在 alpine linux 中不起作用

  • Ansible shell 模块空响应

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve