我正在尝试在创建AWS route53记录的循环中多次运行一个剧本。
创建route53记录的任务如下:
- name: Create Public DNS record
route53:
profile: "{{ route53_profile_id }}"
command: "{{ dns_command }}"
zone: "{{ dns_zone }}"
record: "{{ dns_record_name }}.{{ dns_zone }}"
type: "{{ dns_type }}"
value: "{{ dns_value }}"
overwrite: "{{ dns_overwrite }}"
ttl: "{{ dns_ttl }}"
health_check: "{{ healthcheck.health_check.id | default(omit) }}"
failover: "{{ dns_setting.failover | default(omit) }}"
weight: "{{ dns_setting.weight | default(omit) }}"
region: "{{ region | default(omit) }}"
identifier: "{{ identifier | default(omit) }}"
我的问题是每次都不总是定义健康状况检查。
创建健康检查如下所示:
- name: Create healthcheck with IP address for EC2 instance
route53_health_check:
state: "{{ healthcheck.state | default( healthcheck_defaults.state ) }}"
profile: "{{ route53_profile_id }}"
region: "{{ vpc.region }}"
ip_address: "{{ dns_value }}"
type: "{{ healthcheck.type | default( healthcheck_defaults.type ) }}"
resource_path: "{{ healthcheck.resource_path | default( omit ) }}"
port: "{{ healthcheck.port | default( omit ) }}"
security_token: "{{ healthcheck.security_token | default( omit ) }}"
validate_certs: "{{ healthcheck.validate_certs | default( omit ) }}"
string_match: "{{ healthcheck.string_match | default( omit ) }}"
request_interval: "{{ healthcheck.request_interval | default( healthcheck_defaults.request_interval ) }}"
failure_threshold: "{{ healthcheck.failure_threshold | default( healthcheck_defaults.failure_threshold ) }}"
register: healthcheck
when:
- dns_type == "A"
- dns_setting.healthcheck is defined
如果循环运行5次,则可能只在一次迭代中定义。如果运行状况检查,那么'healthcheck'变量包含运行状况检查的详细信息,例如身份证。如果不在给定循环上运行,则'healthcheck'变量包含以下内容:
"healthcheck": {
"changed": false,
"skip_reason": "Conditional check failed",
"skipped": true
}
在我的route53创建中,如果未定义'healthcheck'变量,则省略运行状况检查。但是,如果 定义,则ansible尝试取消引用healthcheck的health_check参数的id参数,该参数不存在。
如果我在不使用时尝试将运行状况检查设置为默认值,例如{}然后它仍然被定义,我的route53创建失败。
有没有办法强制变量或事实未定义?类似的东西:
- name: Undefine health check
set_fact:
healthcheck: undef
答案 0 :(得分:1)
尝试这样的事情:
- name: Create Public DNS record
route53:
... cut ...
health_check: "{{ healthcheck | skipped | ternary( omit, (healthcheck.health_check | default({})).id ) }}"
... cut ...
如果跳过omit
,则会传递healthcheck
,否则会传递healthcheck.health_check.id
。
根据我的经验,default
无法正常使用2 +级的嵌套dicts(即与mydict.myitem | default('ok')
一起使用但与mydict.mysubdict.myitem | default('ok')
一起使用,所以我使用hack默认subict为{}
首先要安全访问id
:(healthcheck.health_check | default({})).id