From 740883e7fddf2e82b8276a98c186edd66650a888 Mon Sep 17 00:00:00 2001 From: Dani Hodovic Date: Mon, 11 Jan 2021 13:08:20 +0100 Subject: [PATCH] cloudflare_dns: read API token from environment (#1238) * cloudflare_dns: read API token from environment * Update plugins/modules/net_tools/cloudflare_dns.py Co-authored-by: Andrew Klychkov * Update plugins/modules/net_tools/cloudflare_dns.py Co-authored-by: Andrew Klychkov Co-authored-by: Abhijeet Kasurde Co-authored-by: Andrew Klychkov --- changelogs/fragments/cloudflare_dns.yml | 2 ++ plugins/modules/net_tools/cloudflare_dns.py | 20 +++++++++++++------- 2 files changed, 15 insertions(+), 7 deletions(-) create mode 100644 changelogs/fragments/cloudflare_dns.yml diff --git a/changelogs/fragments/cloudflare_dns.yml b/changelogs/fragments/cloudflare_dns.yml new file mode 100644 index 0000000000..1e873fa51c --- /dev/null +++ b/changelogs/fragments/cloudflare_dns.yml @@ -0,0 +1,2 @@ +minor_changes: +- cloudflare_dns - add support for environment variable ``CLOUDFLARE_TOKEN`` (https://github.com/ansible-collections/community.general/pull/1238). diff --git a/plugins/modules/net_tools/cloudflare_dns.py b/plugins/modules/net_tools/cloudflare_dns.py index fc62aa702c..c91df99556 100644 --- a/plugins/modules/net_tools/cloudflare_dns.py +++ b/plugins/modules/net_tools/cloudflare_dns.py @@ -16,13 +16,14 @@ requirements: - python >= 2.6 short_description: Manage Cloudflare DNS records description: - - "Manages dns records via the Cloudflare API, see the docs: U(https://api.cloudflare.com/)" + - "Manages dns records via the Cloudflare API, see the docs: U(https://api.cloudflare.com/)." options: api_token: description: - API token. - Required for api token authentication. - - "You can obtain your API token from the bottom of the Cloudflare 'My Account' page, found here: U(https://dash.cloudflare.com/)" + - "You can obtain your API token from the bottom of the Cloudflare 'My Account' page, found here: U(https://dash.cloudflare.com/)." + - Can be specified in C(CLOUDFLARE_TOKEN) environment variable since community.general 2.0.0. type: str required: false version_added: '0.2.0' @@ -30,13 +31,13 @@ options: description: - Account API key. - Required for api keys authentication. - - "You can obtain your API key from the bottom of the Cloudflare 'My Account' page, found here: U(https://dash.cloudflare.com/)" + - "You can obtain your API key from the bottom of the Cloudflare 'My Account' page, found here: U(https://dash.cloudflare.com/)." type: str required: false aliases: [ account_api_token ] account_email: description: - - Account email. Required for api keys authentication. + - Account email. Required for API keys authentication. type: str required: false algorithm: @@ -100,7 +101,7 @@ options: service: description: - Record service. - - Required for C(type=SRV) + - Required for I(type=SRV). type: str solo: description: @@ -357,7 +358,7 @@ record: import json -from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.basic import AnsibleModule, env_fallback from ansible.module_utils.six.moves.urllib.parse import urlencode from ansible.module_utils._text import to_native, to_text from ansible.module_utils.urls import fetch_url @@ -788,7 +789,12 @@ class CloudflareAPI(object): def main(): module = AnsibleModule( argument_spec=dict( - api_token=dict(type='str', required=False, no_log=True), + api_token=dict( + type="str", + required=False, + no_log=True, + fallback=(env_fallback, ["CLOUDFLARE_TOKEN"]), + ), account_api_key=dict(type='str', required=False, no_log=True, aliases=['account_api_token']), account_email=dict(type='str', required=False), algorithm=dict(type='int'),