Authored by root

修改domain.modify.yml,通过传参执行脚本

---
# 修改腾讯云域名解析记录值: ansible-playbook playbooks/domain.modify.yml -e '{"domain_name":"www.yohotest.xyz","new_record":"1.2.3.4"}' --check -vvv
- hosts: localhost
... ... @@ -23,6 +24,5 @@
domain_modify:
secretId: "{{ qcloud_key.SecretId }}"
secretKey: "{{ qcloud_key.SecretKey }}"
domain_name: "www.yohotest.xyz"
#value: "java-public-lb-862332839.cn-north-1.elb.amazonaws.com.cn"
value: '1.11.11.22'
domain_name: "{{ domain_name }}"
value: "{{ new_record }}"
... ...
... ... @@ -45,6 +45,7 @@ sys.setdefaultencoding('utf8')
'''
def dns_inqure(SecretId,SecretKey,domain_name,value):
ret = {}
URLS_DNS = 'cns.api.qcloud.com/v2/index.php'
# 通过传入的域名来获取domain和subDomain值
domain = '.'.join(domain_name.split('.')[-2:])
subDomain = '.'.join(domain_name.split('.')[:-2])
... ... @@ -54,7 +55,7 @@ def dns_inqure(SecretId,SecretKey,domain_name,value):
# 通过调用域名解析接口RecordList来获取待修改的域名解析记录值(调用修改域名解析接口时必须要传入的参数值)
try:
domaininfo_query = QcloudApi(secretId=SecretId,secretKey=SecretKey)
domaininfo = domaininfo_query.do_query(params={'Action': 'RecordList','domain': domain })
domaininfo = domaininfo_query.do_query(params={'Action': 'RecordList','domain': domain },req_url=URLS_DNS)
if domaininfo["code"] == 0:
domaininfo = domaininfo["data"]["records"]
# 循环获取到的domain下的所有域名解析值,提取待修改的域名对应的所有的参数值,子域名, 当解析值匹配时ret["code"] = 0
... ... @@ -90,10 +91,11 @@ def dns_inqure(SecretId,SecretKey,domain_name,value):
'''
def dns_modify(SecretId,SecretKey,domain_name,value,subDomain,domain,recordType,recordId,recordLine,ttl):
ret = {}
URLS_DNS = 'cns.api.qcloud.com/v2/index.php'
# 调用域名解析接口RecordModify修改域名解析记录
try:
domain_modify_api = QcloudApi(secretId=SecretId,secretKey=SecretKey)
dns_info = domain_modify_api.do_query(params={'Action':'RecordModify','domain':domain,'recordId':recordId,'subDomain':subDomain,'recordType':recordType,'recordLine':recordLine,'value':value,'ttl':ttl})
dns_info = domain_modify_api.do_query(params={'Action':'RecordModify','domain':domain,'recordId':recordId,'subDomain':subDomain,'recordType':recordType,'recordLine':recordLine,'value':value,'ttl':ttl},req_url=URLS_DNS)
ret["code"] = dns_info["code"]
ret["err"] = dns_info['message']
except Exception,e:
... ... @@ -140,11 +142,11 @@ def run_module():
if ret["value_old"] == value:
module.fail_json(msg='Modify Failed: 修改的解析与原解析一致', **result)
# check模式下只执行查询接口 执行语句:ansible-playbook playbooks/domain.modify.yml --check -vvv
# check模式下只执行查询接口 执行语句:ansible-playbook playbooks/domain.modify.yml -e '{"domain_name":"www.yohotest.xyz","new_record":"1.2.3.4"}' --check -vvv
if module.check_mode:
#result['response'] = ret
module.exit_json(**result)
# 非check模式下执行修改接口 执行语句:ansible-playbook playbooks/domain.modify.yml
# 非check模式下执行修改接口 执行语句:ansible-playbook playbooks/domain.modify.yml -e '{"domain_name":"www.yohotest.xyz","new_record":"1.2.3.4"}'
else:
modify_result = dns_modify(secretId,secretKey,domain_name, value,ret["subDomain"],ret["domain"],ret["recordType_new"],ret["recordId"],ret["recordLine"],ret["ttl"])
if modify_result['code'] != 0:
... ...