amazon-web-services - ansible 将主机添加到route53

标签 amazon-web-services amazon-ec2 ansible amazon-route53

我正在使用ansible在ec2上配置服务器,创建服务器后我想在route53区域上创建一个主机条目

---
- hosts: all
  connection: local

  tasks:
  - name: create ec2 instance
    action: 
      module: ec2 
      zone: "{{ zone }}"
      image: "{{ image }}"
      instance_type: "{{instance_type}}"
      region: "{{ region }}"
      vpc_subnet_id: "{{ subnet }}"
      group: "{{ security_group }}"
      key_name: "{{ sshkey }}"
      instance_tags: 
        Name: "{{inventory_hostname}}"
        Environment: "{{ Environment  }}"
        Date: "{{ Date}}"
        Noderole: "{{ NodeRole }}"
        ConfigurationGroup: "{{ ConfigurationGroup}}"
        Backups: "{{ Backups }}"

      count_tag:
        Name: "{{inventory_hostname}}"
      exact_count: 1


  - name: Ensure DNS entry exists
    action:
      module:  route53
      command: create
      overwrite: "yes"
      record: "{{ inventory_hostname }}.{{ server_zone }}" 
      type: A 
      zone: "{{ server_zone }}"
      value: "{{ item.private_ip }}"
    with_items: "ec2.instances"

属性“inventory_hostname”、“server_zone”在主机的 list 文件中定义,以便它们在创建 EC2 实例时起作用。

[kshk:~/testing/ansible-ec2] master* ± ansible-playbook -i inventory/development/devcm_q/inventory.ini create-ec2-instance.yml --limit dcm-jmp-09 -v

PLAY [all] ******************************************************************** 

GATHERING FACTS *************************************************************** 
ok: [dcm-jmp-09]

TASK: [create ec2 instance] *************************************************** 
changed: [dcm-jmp-09] => {"changed": true, "instance_ids": ["i-7c9e89f1"], "instances": [{"ami_launch_index": "0", "architecture": "x86_64", "dns_name": "", "ebs_optimized": false, "groups": {"sg-0bf7d96f": "dev-jumpbox"}, "hypervisor": "xen", "id": "i-7c9e89f1", "image_id": "ami-33734044", "instance_type": "t2.micro", "kernel": null, "key_name": "bootstrap", "launch_time": "2016-02-21T04:28:38.000Z", "placement": "eu-west-1c", "private_dns_name": "ip-172-31-8-55.eu-west-1.compute.internal", "private_ip": "172.31.8.55", "public_dns_name": "", "public_ip": null, "ramdisk": null, "region": "eu-west-1", "root_device_name": "/dev/sda1", "root_device_type": "ebs", "state": "pending", "state_code": 0, "tags": {}, "tenancy": "default", "virtualization_type": "hvm"}], "tagged_instances": [{"ami_launch_index": "0", "architecture": "x86_64", "dns_name": "", "ebs_optimized": false, "groups": {"sg-0bf7d96f": "dev-jumpbox"}, "hypervisor": "xen", "id": "i-7c9e89f1", "image_id": "ami-33734044", "instance_type": "t2.micro", "kernel": null, "key_name": "bootstrap", "launch_time": "2016-02-21T04:28:38.000Z", "placement": "eu-west-1c", "private_dns_name": "ip-172-31-8-55.eu-west-1.compute.internal", "private_ip": "172.31.8.55", "public_dns_name": "", "public_ip": null, "ramdisk": null, "region": "eu-west-1", "root_device_name": "/dev/sda1", "root_device_type": "ebs", "state": "pending", "state_code": 0, "tags": {}, "tenancy": "default", "virtualization_type": "hvm"}]}

TASK: [Ensure DNS entry exists] *********************************************** 
fatal: [dcm-jmp-09] => One or more undefined variables: 'unicode object' has no attribute 'private_ip'

FATAL: all hosts have already failed -- aborting

PLAY RECAP ******************************************************************** 
           to retry, use: --limit @/home/kshk/create-ec2-instance.retry

dcm-jmp-09  

但是,当剧本运行时,它会抛出错误“no attribute 'private_ip”

有什么想法吗?

最佳答案

您没有注册ec2。您如何期望 ec2.instances 包含 private_ip

  - name: create ec2 instance
    action: 
      module: ec2 
      zone: "{{ zone }}"
      .....
      exact_count: 1
    register: ec2

  - name: Ensure DNS entry exists
    action:
      module:  route53
      ....
      zone: "{{ server_zone }}"
      value: {{ item.private_ip }}
    with_items: ec2.instances

关于amazon-web-services - ansible 将主机添加到route53,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35537266/

相关文章:

amazon-web-services - AWS SSM session 管理器不显示实例

ansible - 如何制作字典元素顶级变量

python - ansible 2.9 中未找到 openstacksdk

amazon-web-services - 字符串数据类型的 Elasticsearch 查询

sql-server - 尝试将 Amazon EC2 上的 SSRS 连接到 Amazon RDS MSSQL 时出现权限错误

python-3.x - Boto3:使用 boto3.resource ('s3' )列出所有 S3 存储桶

amazon-ec2 - 如何在 Spark Streaming EC2 集群应用程序中从 S3 读取输入

amazon-web-services - 使用 Ansible 向 AWS 安全组添加和删除多个 IP 地址

amazon-web-services - 我们可以删除 AWS API Gateway 中的 API 吗?如果是的话怎么办?

node.js - 在elasticbeanstalk上安装npm canvas - npm安装错误