Ansible EC2 模块未检索实例信息
Ansible EC2 Module is not retrieving instance information
我写了一个 ansible 任务来创建 ec2 实例并将主机添加为动态主机。该任务运行良好并创建了实例,但无法检索实例信息。
我的 Ansible 版本:2.2.0.0 / Ubuntu 14.04
这是我的代码
- name: launch ec2 instance for QA
local_action:
module: ec2
key_name: "{{ ec2_keypair }}"
group: "{{ ec2_security_group }}"
instance_type: "{{ ec2_instance_type }}"
image: "{{ ec2_image }}"
vpc_subnet_id: "{{ ec2_subnet_ids }}"
region: "{{ ec2_region }}"
instance_tags: '{"Name":"{{ec2_tag_Name}}","Type":"{{ec2_tag_Type}}","Environment":"{{ec2_tag_Environment}}"}'
assign_public_ip: yes
wait: true
count: 1
register: ec2
- debug: var=item
with_items: ec2.instances
- add_host: name={{ item.public_ip }} >
groups=dynamically_created_hosts
with_items: ec2.instances
- name: Wait for the instances to boot by checking the ssh port
wait_for: host={{item.public_ip}} port=22 delay=60 timeout=320 state=started
with_items: ec2.instances
得到的输出是:
TASK [launch ec2 instance for QA] **********************************************
changed: [localhost -> localhost]
TASK [debug] *******************************************************************
ok: [localhost] => (item=ec2.instances) => {
"item": "ec2.instances"
}
TASK [add_host] ****************************************************************
fatal: [localhost]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'unicode object' has no attribute 'public_ip'\n\nThe error appears to have been in '/var/lib/jenkins/jobs/QA/workspace/dynamic-ec2.yml': line 37, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - add_host: name={{ item.public_ip }} >\n ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n with_items:\n - {{ foo }}\n\nShould be written as:\n\n with_items:\n - \"{{ foo }}\"\n"}
还有其他方法吗?
您不能在 2.2 中使用裸变量。该语法已被弃用,并从 2.0 版开始警告用户。
您应该阅读您粘贴的错误消息,虽然它提出了不同的原因,但您应该按照给出的示例进行操作:
Should be written as:
with_items:
- "{{ foo }}"
在您的情况下,将所有 with_items: ec2.instances
替换为:
就足够了
with_items: "{{ ec2.instances }}"
我写了一个 ansible 任务来创建 ec2 实例并将主机添加为动态主机。该任务运行良好并创建了实例,但无法检索实例信息。
我的 Ansible 版本:2.2.0.0 / Ubuntu 14.04
这是我的代码
- name: launch ec2 instance for QA
local_action:
module: ec2
key_name: "{{ ec2_keypair }}"
group: "{{ ec2_security_group }}"
instance_type: "{{ ec2_instance_type }}"
image: "{{ ec2_image }}"
vpc_subnet_id: "{{ ec2_subnet_ids }}"
region: "{{ ec2_region }}"
instance_tags: '{"Name":"{{ec2_tag_Name}}","Type":"{{ec2_tag_Type}}","Environment":"{{ec2_tag_Environment}}"}'
assign_public_ip: yes
wait: true
count: 1
register: ec2
- debug: var=item
with_items: ec2.instances
- add_host: name={{ item.public_ip }} >
groups=dynamically_created_hosts
with_items: ec2.instances
- name: Wait for the instances to boot by checking the ssh port
wait_for: host={{item.public_ip}} port=22 delay=60 timeout=320 state=started
with_items: ec2.instances
得到的输出是:
TASK [launch ec2 instance for QA] **********************************************
changed: [localhost -> localhost]TASK [debug] *******************************************************************
ok: [localhost] => (item=ec2.instances) => { "item": "ec2.instances" }TASK [add_host] ****************************************************************
fatal: [localhost]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'unicode object' has no attribute 'public_ip'\n\nThe error appears to have been in '/var/lib/jenkins/jobs/QA/workspace/dynamic-ec2.yml': line 37, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - add_host: name={{ item.public_ip }} >\n ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n with_items:\n - {{ foo }}\n\nShould be written as:\n\n with_items:\n - \"{{ foo }}\"\n"}
还有其他方法吗?
您不能在 2.2 中使用裸变量。该语法已被弃用,并从 2.0 版开始警告用户。
您应该阅读您粘贴的错误消息,虽然它提出了不同的原因,但您应该按照给出的示例进行操作:
Should be written as: with_items: - "{{ foo }}"
在您的情况下,将所有 with_items: ec2.instances
替换为:
with_items: "{{ ec2.instances }}"