はじめに
Route53でDNSを運用している方は多いと思いますが、ゾーンごとのレコード一覧とかサクっと調べたいことがあるかと思います。
マネコンだとエクスポートできないし、AWS CLIでやるにしてもjqなどで適切にパースしないと見づらいです。
今回は、Ansibleを利用してレコード一覧をYAML形式で見やすくファイル出力してみます。
特記事項
本記事の手順では、ゾーンあたり100件を超えるレコードは出力できません。
全レコードを出力する手順はこちらをご参照ください。
前提
- AWS関連のモジュール実行にはbotoが必要です。
- credential情報は環境変数か
aws configure
でセットしてある必要があります。
sample
やること
取得したレコードセット情報を、ゾーンごとにworkディレクトリにYAML形式で保存する
ディレクトリ構成
ディレクトリ構成
site.yml
roles/
|--route53_export/
| |--tasks/
| | |--main.yml
| |--templates/
| | |--save_record_sets.j2
hosts/aws #inventory
work/ #ゾーン単位でレコード情報が出力される
|--testdomain.com.txt #出力されたレコード情報
inventory
AWSリソース関連モジュールはすべてlocalhostで実行するので、下記のようなインベントリファイルを用意します。
hosts/aws
[aws]
localhost
Role
roles/route53_export/tasks/main.yml
---
- name: Create directory
file:
path: "{{ inventory_dir | dirname }}/work/route53_export"
state: directory
mode: '0775'
group: wheel
delegate_to: localhost
check_mode: no
changed_when: no
become: yes
- name: Get all hosted zones
route53_facts:
query: hosted_zone
register: hosted_zones
check_mode: no
- name: Get record sets in a given hosted zone
route53_facts:
profile: "{{ lookup('env', 'AWS_DEFAULT_PROFILE') }}"
query: record_sets
hosted_zone_id: "{{ item.Id | regex_replace('/hostedzone/','') }}"
check_mode: no
register: record_sets
with_items: "{{ hosted_zones.HostedZones }}"
- name: Save record sets
template:
src: save_record_sets.j2
dest: >-
{{ inventory_dir | dirname }}/work/route53_export/{{ item.item.Name | regex_replace('(^.*)\.$','\1') }}.txt
mode: '0664'
group: wheel
delegate_to: localhost
changed_when: no
become: yes
with_items: "{{ record_sets.results }}"
when: not ansible_check_mode
template
roles/route53_export/templates/save_record_sets.j2
{{ item.ResourceRecordSets | to_nice_yaml(indent=2) }}
site.yml
site.yml
---
- name: route53_export
hosts: localhost
connection: local
roles:
- role: route53_export
実行
Command
$ ansible-playbook -i hosts/aws -l localhost site.yml
出力イメージ
work/testdomain.com.txt
- Name: cname.testdomain.com.
ResourceRecords:
- Value: www.example.com
TTL: 300
Type: CNAME
- AliasTarget:
DNSName: testelb-xxxxxxxxx.ap-northeast-1.elb.amazonaws.com.
EvaluateTargetHealth: false
HostedZoneId: Z14GRHDCWA56QT
Name: elb.testdomain.com.
Type: A
- Name: text.testdomain.com.
ResourceRecords:
- Value: '"var"'
TTL: 300
Type: TXT
- Name: www.testdomain.com.
ResourceRecords:
- Value: 10.1.1.1
TTL: 7200
Type: A
まとめ
いかがでしょうか。結構見やすいと思います。
このファイルをgit管理するのもアリかと。