嗨,我能够在下面的剧本中获得所需的输出,其中我使用了以下 2 个任务aws-create-rds
,aws-create-route53-record
---
# Playbook for creating aws rds instance and then creating route53 dns record.
- name: setup aws-rds-instances
hosts: localhost
roles:
- aws-create-rds
任务定义如下aws-create-rds
。
---
- name: create an rds instance
rds:
command: create
aws_access_key: "{{ aws_create_rds.access_key }}"
aws_secret_key: "{{ aws_create_rds.secret_key }}"
db_name: "{{ aws_create_rds.db_name }}"
instance_name: "{{ aws_create_rds.name }}"
db_engine: "{{ aws_create_rds.db_engine }}"
size: "{{ aws_create_rds.db_size }}"
instance_type: "{{ aws_create_rds.instance_type }}"
username: "{{ aws_create_rds.username }}"
password: "{{ aws_create_rds.password }}"
subnet: "{{ aws_create_rds.subnet }}"
region: "{{ aws_create_rds.region }}"
zone: "{{ aws_create_rds.zone }}"
publicly_accessible: "{{ aws_create_rds.access }}"
backup_retention: "{{ aws_create_rds.retention }}"
vpc_security_groups: "{{ aws_create_rds.aws_sg_name }}"
port: "{{ aws_create_rds.port }}"
wait: yes
wait_timeout: 900
tags:
created_by: ansible
register: rds
- name: Create a route53 record for RDS instance.
route53:
state: present
aws_access_key: "{{ aws_create_route53_record.access_key }}"
aws_secret_key: "{{ aws_create_route53_record.secret_key }}"
zone: "{{ aws_create_route53_record.zone }}"
hosted_zone_id: "{{ aws_create_route53_record.id }}"
type: "{{ aws_create_route53_record.type }}"
value: "{{ rds.instance.endpoint }}"
record: "{{ aws_create_route53_record.record }}"
private_zone: "{{ aws_create_route53_record.private_zone }}"
ttl: 30
现在,由于此任务中有 2 个任务,我希望将它们拆分成 2 个不同的角色。我希望将来在 ansible 中将它们用作独立角色aws-create-rds
,例如第 1 个角色和第 2 个角色aws-create-route53-record
,但是我不确定如何传递endpoint
来自aws-create-rds
任务的值并将其传递给dns 记录aws-create-route53-record
并将其用作value
dnsroute53
记录。我已检查 rds 模块文档,其中没有端点的返回值。此外,还有 2 个用例,如下所示。
1. route53 record value can be passed through group_vars OR
2. route53 record value can be passed from any previous ansible task executed.
我想处理aws-create-route53-record
任务中的两种情况。任何线索都可以实现这一点。环境变量或任何东西。提前致谢。
答案1
为了实现这一点,我编写了 2 个不同的角色/任务并在剧本中调用它们。以下是任务。第一个是 aws-create-rds
- name: create an rds instance
rds:
command: create
aws_access_key: "{{ aws_create_rds.access_key }}"
aws_secret_key: "{{ aws_create_rds.secret_key }}"
db_name: "{{ aws_create_rds.db_name }}"
instance_name: "{{ aws_create_rds.name }}"
db_engine: "{{ aws_create_rds.db_engine }}"
size: "{{ aws_create_rds.db_size }}"
instance_type: "{{ aws_create_rds.instance_type }}"
username: "{{ aws_create_rds.username }}"
password: "{{ aws_create_rds.password }}"
subnet: "{{ aws_create_rds.subnet }}"
region: "{{ aws_create_rds.region }}"
publicly_accessible: "{{ aws_create_rds.publicly_access }}"
backup_retention: "{{ aws_create_rds.retention }}"
vpc_security_groups: "{{ aws_create_rds.aws_sg_name }}"
multi_zone: "{{ aws_create_rds.multi_zone }}"
port: "{{ aws_create_rds.port }}"
wait: yes
wait_timeout: 900
tags:
created_by: ansible
register: rds
- name: Assigning rds endpoint value to variable.
set_fact:
endpoint_host: "{{ rds.instance.endpoint }}"
第二个任务是 aws-create-route53-record。
---
-
name: "Set facts of record value"
set_fact:
record_value: "{{ aws_create_route53_record.value }}"
-
name: "If record value not present, Look for endpoint-host variable"
set_fact:
record_value: "{{ endpoint_host }}"
when: "aws_create_route53_record.value == \"\""
-
name: "Create a route53 record for RDS instance."
route53:
aws_access_key: "{{ aws_create_route53_record.access_key }}"
aws_secret_key: "{{ aws_create_route53_record.secret_key }}"
hosted_zone_id: "{{ aws_create_route53_record.hosted_zone_id }}"
private_zone: "{{ aws_create_route53_record.private_zone }}"
record: "{{ aws_create_route53_record.record }}"
state: present
ttl: 30
type: "{{ aws_create_route53_record.type }}"
value: "{{ record_value }}"
zone: "{{ aws_create_route53_record.zone }}"