2015-07-10 75 views
0

Ansible版本:1.9.2Ansible add_host未添加到主机文件

我正在运行一个将启动EC2实例的操作手册。我拥有包含所有变量的site.yml,并使用角色启动EC2实例。下面是该ec2_launch

--- 
- 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选项运行main.yml,这是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]包含在hosts文件中。在运行过程中没有错误,不知道为什么它没有被添加到文件中。

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

回答

6

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

添加到主机文件在所有情况下都不起作用 - 例如,AWS用户经常使用./ec2.py脚本,该脚本是替代其基于文本的主机文件的二进制可执行文件。

+0

Gotcha - 我也可以使用lineinfile模块,对吗? – nocode

+0

lineinfile模块为我工作。谢谢(你的)信息! – nocode

+0

对于下一个来这里的人:这里是文档:http://docs.ansible.com/ansible/add_host_module.html。阅读标题。另外,考虑到最好的做法是利用动态库存与博托因此修改主机配置文件没有多大意义。 – einarc