【问题标题】:Ansible add_host not adding to hosts fileAnsible add_host 未添加到主机文件
【发布时间】:2015-07-10 21:21:07
【问题描述】:

Ansible 版本:1.9.2

我正在运行一个将启动 EC2 实例的剧本。我有包含所有变量的 site.yml 并使用角色来启动 EC2 实例。这是 ec2_launch 的 main.yml

---
- name: create EC2 instance
  ec2:
    key_name: "{{ keypair }}"
    group_id: "{{ security_group }}"
    instance_type: "{{ instance_type }}"
    image: "{{ image }}"
    region: "{{ region }}"
    vpc_subnet_id: "{{ vpc_subnet_id }}"
    assign_public_ip: yes
    wait: True
    instance_tags:
      Name: "{{ instance_tag }}"
      Environment: "{{ instance_tag2 }}"
    exact_count: 1
    count_tag:
      Name: "{{ instance_tag }}"
      Environment: "{{ instance_tag2 }}"
    monitoring: yes
  register: ec2
- name: add ec2 instance to hosts
  add_host: name={{ item.private_ip }} groups=group_name
  with_items: ec2.instances
- name: wait for SSH
  wait_for: host={{ item.private_ip }} port=22 delay=60 timeout=320 state=started
  with_items: ec2.instances

当我使用 -vvv 选项运行时,这是 add_hosts 期间的输出:

TASK: [ec2_launch | add ec2 instance to hosts] ********************************
creating host via 'add_host': hostname=10.50.101.93
added host to group via add_host module: group_name

IP 正确,组名正确,但是当我检查 ansible hosts 文件时,它没有被修改。我确保在主机文件中包含 [group_name]。运行期间没有错误,不确定为什么没有将其添加到文件中。

此页面上有一个“add_host”示例:http://docs.ansible.com/ec2_module.html 而不是名称和组,他们使用主机名,组名(我相信它们是旧的)。我也试过这些。不知道我错过了什么。

【问题讨论】:

    标签: amazon-ec2 ansible ansible-playbook ansible-inventory


    【解决方案1】:

    add_host 任务将其添加到主机文件中,而是将其添加到主机的内存列表中。

    添加到主机文件并非在所有情况下都有效 - 例如,AWS 用户经常使用 ./ec2.py 脚本,这是一个二进制可执行文件,可替换基于文本的主机文件。

    【讨论】:

    • 知道了——我也可以使用 lineinfile 模块,对吧?
    • lineinfile 模块对我有用。感谢您的信息!
    • 对于下一个来到这里的人:这是文档:docs.ansible.com/ansible/add_host_module.html。阅读标题。此外,考虑到最佳实践是使用带有 Boto 的动态清单,因此修改主机配置文件没有多大意义。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-22
    • 1970-01-01
    • 2019-03-13
    • 1970-01-01
    • 2021-12-06
    • 2021-12-16
    相关资源
    最近更新 更多