Authored by chunhua.zhang

modify

@@ -6,7 +6,7 @@ @@ -6,7 +6,7 @@
6 6
7 vars_prompt: 7 vars_prompt:
8 - name: "confirmation" 8 - name: "confirmation"
9 - prompt: "Modify DNS record . Please Enter YES to continue." 9 + prompt: "Start to change . Please Enter YES to continue."
10 private: no 10 private: no
11 11
12 vars_files: 12 vars_files:
@@ -21,9 +21,9 @@ @@ -21,9 +21,9 @@
21 msg: "user do not type YES" 21 msg: "user do not type YES"
22 when: confirmation != 'YES' 22 when: confirmation != 'YES'
23 23
24 - - name: 修改域名映射的IP或CNAME 24 + - name: 修改java域名解析
25 domain_modify: 25 domain_modify:
26 secretId: "{{ qcloud_key.SecretId }}" 26 secretId: "{{ qcloud_key.SecretId }}"
27 secretKey: "{{ qcloud_key.SecretKey }}" 27 secretKey: "{{ qcloud_key.SecretKey }}"
28 - domain_name: "{{ domain_name }}"  
29 - value: "{{ new_record }}" 28 + domain_name: "api.yoho.cn,service.yoho.cn,union.yoho.cn,webunion.yohobuy.com"
  29 + value: "java-public-lb-862332839.cn-north-1.elb.amazonaws.com.cn"
@@ -10,7 +10,7 @@ ANSIBLE_METADATA = { @@ -10,7 +10,7 @@ ANSIBLE_METADATA = {
10 DOCUMENTATION = ''' 10 DOCUMENTATION = '''
11 --- 11 ---
12 module: domain_modify 12 module: domain_modify
13 -short_description: 修改域名解析 13 +short_description: 修改域名解析(支持同时修改多个域名到一个解析值,域名以逗号分隔)
14 ''' 14 '''
15 15
16 EXAMPLES = ''' 16 EXAMPLES = '''
@@ -21,14 +21,14 @@ EXAMPLES = ''' @@ -21,14 +21,14 @@ EXAMPLES = '''
21 domain_modify: 21 domain_modify:
22 secretId: "{{ qcloud_key.SecretId }}" 22 secretId: "{{ qcloud_key.SecretId }}"
23 secretKey: "{{ qcloud_key.SecretKey }}" 23 secretKey: "{{ qcloud_key.SecretKey }}"
24 - domain_name: "switch.test.yohops.com" 24 + domain_name: "www.yohotest.xyz,*.yohotest.xyz"
25 value: "1.2.3.4" 25 value: "1.2.3.4"
26 26
27 - name: "修改域名对应的CNAME" 27 - name: "修改域名对应的CNAME"
28 domain_modify: 28 domain_modify:
29 secretId: "{{ qcloud_key.SecretId }}" 29 secretId: "{{ qcloud_key.SecretId }}"
30 secretKey: "{{ qcloud_key.SecretKey }}" 30 secretKey: "{{ qcloud_key.SecretKey }}"
31 - domain_name: "switch.test.yohops.com" 31 + domain_name: "2.yohotest.xyz"
32 value: "cname.dnspod.com." 32 value: "cname.dnspod.com."
33 33
34 ''' 34 '''
@@ -136,27 +136,47 @@ def run_module(): @@ -136,27 +136,47 @@ def run_module():
136 secretId = module.params['secretId'] 136 secretId = module.params['secretId']
137 secretKey = module.params['secretKey'] 137 secretKey = module.params['secretKey']
138 domain_name = module.params['domain_name'] 138 domain_name = module.params['domain_name']
139 - value = module.params['value'] 139 + value = module.params['value'].strip()
140 140
141 - ret = dns_inqure(secretId,secretKey,domain_name,value)  
142 - if ret['code'] != 0:  
143 - module.fail_json(msg='查询失败: code: %s err: %s' % (ret["code"],ret['err']), **result) 141 + domain_name_list = domain_name.split(',')
144 142
145 - result['message'] = {"需修改解析的DNS":domain_name ,"修改之前的解析地址":ret["value_old"],"修改之前的记录类型":ret["recordType_old"],"修改之后的解析地址":ret["value"] ,"修改之后的记录类型":ret["recordType_new"]}  
146 - if ret["value_old"] == ret["value"]:  
147 - module.fail_json(msg='Modify Failed: 修改的解析与原解析一致', **result) 143 + rets = {}
  144 + message_list = []
  145 + for domain_name in domain_name_list:
  146 + domain_name = domain_name.strip()
  147 + ret = dns_inqure(secretId,secretKey,domain_name,value)
  148 + if ret['code'] != 0:
  149 + module.fail_json(msg='查询失败: code: %s err: %s' % (ret["code"],ret['err']), **result)
  150 +
  151 + messages = {"需修改解析的DNS":domain_name ,"修改之前的解析地址":ret["value_old"],"修改之前的记录类型":ret["recordType_old"],"修改之后的解析地址":ret["value"] ,"修改之后的记录类型":ret["recordType_new"]}
  152 + message_list.append(messages)
  153 + result['message'] = message_list
  154 +
  155 + if ret["value_old"] == ret["value"]:
  156 + module.fail_json(msg='Modify Failed: 修改的解析与原解析一致', **result)
  157 +
  158 + rets[domain_name] = ret
  159 + # check模式下只执行查询接口 执行语句:ansible-playbook playbooks/domain.modify.yml -e '{"domain_name":"www.yohotest.xyz","new_record":"1.2.3.4"}' --check -vvv
148 160
149 - # check模式下只执行查询接口 执行语句:ansible-playbook playbooks/domain.modify.yml -e '{"domain_name":"www.yohotest.xyz","new_record":"1.2.3.4"}' --check -vvv  
150 if module.check_mode: 161 if module.check_mode:
151 #result['response'] = ret 162 #result['response'] = ret
152 module.exit_json(**result) 163 module.exit_json(**result)
153 # 非check模式下执行修改接口 执行语句:ansible-playbook playbooks/domain.modify.yml -e '{"domain_name":"www.yohotest.xyz","new_record":"1.2.3.4"}' 164 # 非check模式下执行修改接口 执行语句:ansible-playbook playbooks/domain.modify.yml -e '{"domain_name":"www.yohotest.xyz","new_record":"1.2.3.4"}'
154 else: 165 else:
155 - modify_result = dns_modify(secretId,secretKey,domain_name, ret["value"],ret["subDomain"],ret["domain"],ret["recordType_new"],ret["recordId"],ret["recordLine"],ret["ttl"])  
156 - if modify_result['code'] != 0:  
157 - module.fail_json(msg='Modify Failed: code: %i, err: %s' % (modify_result['code'], modify_result['err']), **result)  
158 - result['changed'] = True  
159 - #result['response'] = modify_result 166 + for domain_name in domain_name_list:
  167 + domain_name = domain_name.strip()
  168 + value = rets[domain_name]["value"]
  169 + subDomain = rets[domain_name]["subDomain"]
  170 + domain = rets[domain_name]["domain"]
  171 + recordType = rets[domain_name]["recordType_new"]
  172 + recordId = rets[domain_name]["recordId"]
  173 + recordLine = rets[domain_name]["recordLine"]
  174 + ttl = rets[domain_name]["ttl"]
  175 + modify_result = dns_modify(secretId,secretKey,domain_name,value,subDomain,domain,recordType,recordId,recordLine,ttl)
  176 + if modify_result['code'] != 0:
  177 + module.fail_json(msg='Modify Failed: code: %i, err: %s' % (modify_result['code'], modify_result['err']), **result)
  178 + result['changed'] = True
  179 + #result['response'] = modify_result
160 module.exit_json(**result) 180 module.exit_json(**result)
161 181
162 def main(): 182 def main():