首页 文章

ansible将主机添加到route53

提问于
浏览
1

我正在使用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"

有任何想法吗?

1 回答

  • 2

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

相关问题