首页 文章

Ansible EC2模块未检索实例信息[重复]

提问于
浏览
1

这个问题在这里已有答案:

我写了一个ansible任务来创建ec2实例并将主机添加为动态主机 . 该任务完美地工作并创建了实例,但我无法检索实例信息 .

My 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]任务[调试] ********************************* **********************************确定:[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 = {}> \ n ^ here \ n我们可能错了,但这个看起来可能是一个问题\ n当引用一个值时,总是引用模板表达式括号 . 例如:\ n \ n with_items:\ n - {} \ n \ n应该写成:\ n \ n with_items:\ n - \“{} \”\ n“}

有没有其他方法可以做到这一点?

1 回答

  • 5

    你不能在2.2中使用裸变量 . 不推荐使用该语法,并且自2.0版以来警告用户 .

    您应该阅读已粘贴的错误消息,虽然它表明了其他原因,但您应该按照给出的示例进行操作:

    应写成:
    with_items:

    • “{}”

    在你的情况下,它足以替换所有 with_items: ec2.instances

    with_items: "{{ ec2.instances }}"
    

相关问题