【问题标题】:ansible add host to route53ansible 将主机添加到 route53
【发布时间】:2016-02-21 14:21:31
【问题描述】:

我正在使用 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”在主机的清单文件中定义,因此它们在创建 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  

但是,当 playbook 运行时,它会抛出错误 "no attribute 'private_ip"

有什么想法吗?

【问题讨论】:

  • 我猜ec2.instances 是由 ec2 模块创建的还是从动态库存生成的? (我对 ec2 的东西没有经验)你能在上面运行调试任务吗? - debug: var=ec2.instances - “unicode object”听起来像是一个字符串......
  • 我得到相同的调试输出

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


【解决方案1】:

您没有注册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

【讨论】:

    猜你喜欢
    • 2022-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-06
    • 1970-01-01
    • 2019-03-13
    • 1970-01-01
    相关资源
    最近更新 更多