我編寫了一個非常簡單的任務來創建ec2實例並將主機添加爲動態主機。該任務正在完美工作並創建實例,但無法檢索實例信息。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
什麼我得到的輸出是:
任務[爲QA推出的EC2實例] **********************************
已更改: [localhost - > localhost]任務[調試] ********************************************* **********************
ok:[localhost] =>(item = ec2.instances)=> { 「item」:「ec2.instances 「 }任務[add_host] *************************************** *************************
致命:[localhost]:失敗! => {「failed」:true,「msg」:「該字段'args'有一個無效值,其中包含一個未定義的變量,錯誤是:'unicode object'沒有屬性'public_ip'\ n \ n錯誤似乎在'/var/lib/jenkins/jobs/QA/workspace/dynamic-ec2.yml'中:第37行,第7列,但可能在文件中的其他位置,具體取決於確切的語法問題。 \ n \ n違規行似乎是:\ n \ n \ n - add_host:name = {{item.public_ip}}> \ n^here \ n我們可能是錯的,但這看起來可能是一個問題例如:\ n \ n with_items:\ n - {{foo}} \ n \ n應將其寫爲:\ n \ n with_items:\ n - \「{{foo}} \」\ n「}
有沒有其他方法可以做到這一點?
@tehraf謝謝你的建議,它完美的工作。 – rolindroy