Repository: ansible/ansible

Issue:
proxied option from in cloudflare_dns module not working in the ansible 2.3.0.0
##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
cloudflare_dns

##### ANSIBLE VERSION
```
$ ansible --version
ansible 2.3.0.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides
  python version = 2.7.9 (default, Jun 29 2016, 13:08:31) [GCC 4.9.2]
```


##### OS / ENVIRONMENT
```
$ cat /etc/debian_version
8.10
```

##### SUMMARY
proxied option from in cloudflare_dns module not working in the ansible 2.3.0.0:


##### STEPS TO REPRODUCE

$ ansible -i ./hosts subdomain.domain.com.yaml

```
$ cat subdomain.domain.com
- hosts: localhost
  vars:
    cloudflare_email: cloudflareaccount@email.com
    cloudflare_api_token: <cloudflare_api_token>
  tasks:
  - name: create dns record for subdomain.domain.com
    cloudflare_dns:
      zone: domain.com
      record: subdomain
      type: A
      value: IP.AD.DR.ESS
      proxied: yes
      account_email: "{{ cloudflare_email }}"
      account_api_token: "{{ cloudflare_api_token }}"
$ cat hosts
# Dummy inventory for ansible
localhost
```
And one more thing - that should be existing DNS record. 
e.g. for new DNS records desired proxied option are setting up fine, but for existing DNS records i just getting current result without any changes and errors from ansible side.

##### EXPECTED RESULTS
proxied should been switched to "true"

##### ACTUAL RESULTS
```
ok: [localhost] => {
    "changed": false,
    "invocation": {
        "module_args": {
            "account_api_token": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "account_email": "cloudflareaccount@email.com",
            "port": null,
            "priority": 1,
            "proto": null,
            "proxied": true, <-------- we are trying to set proxied:true
            "record": "login",
            "service": null,
            "solo": null,
            "state": "present",
            "timeout": 30,
            "ttl": 1,
            "type": "A",
            "value": "21.0.9.6",
            "weight": 1,
            "zone": domain.com"
        }
    },
    "result": {
        "record": {
            "content": "21.0.9.6",
            "created_on": "2018-01-22T15:09:38.728833Z",
            "id": "XXX",
            "locked": false,
            "meta": {
                "auto_added": false,
                "managed_by_apps": false
            },
            "modified_on": "2018-01-22T15:09:38.728833Z",
            "name": "subdomain.domain.com",
            "proxiable": true,
            "proxied": false, <-------- but result is false
            "ttl": 1,
            "type": "A",
            "zone_id": "YYY",
            "zone_name": "domain.com"
        }
    }
}
```
ps: email/token/domain and other private stuff was replaced in examples above.



Discussion:
Files identified in the description:
* [lib/ansible/modules/net_tools/cloudflare_dns.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/net_tools/cloudflare_dns.py)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
cc @andreaso @mgruener
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
I can confirm seeing this behavior using Ansible 2.5 (devel) as well.

Except that I can only reproduce it under _some_ circumstances, which I have yet to reliable determine.
@andreaso, I have updated issue description - for me it works with creating new records, but not works for updating existed DNS records.
Existing records, as in keeping the same value (ip address), just toggling the proxied boolean? Yeah, I think I can reproduce that that doesn't change the proxied status

If I on the other hand switch both the value and the proxied boolean at the same time that does seem to result in the proxied status being changed.

That is also a bug I can imagine existing based on how the module works. Except that I'm way too tired today to actually look closer at the code right now.
Yepp, this is definitely about new records vs. just trying to switch the proxied status.

In it's current form the cloudflare_dns module only cares about the proxied parameter during record creation, while being competently blind for it while evaluating existing records. Hence this isn't so much a minor parsing bug as it is about missing functionality.

Will see what I'll be able to accomplish during the weekend. 
Ok, thanks in advance!

Resolving patch:
diff --git a/lib/ansible/modules/net_tools/cloudflare_dns.py b/lib/ansible/modules/net_tools/cloudflare_dns.py
--- a/lib/ansible/modules/net_tools/cloudflare_dns.py
+++ b/lib/ansible/modules/net_tools/cloudflare_dns.py
@@ -557,6 +557,8 @@ def ensure_dns_record(self, **kwargs):
                 do_update = True
             if (params['priority'] is not None) and ('priority' in cur_record) and (cur_record['priority'] != params['priority']):
                 do_update = True
+            if ('proxied' in new_record) and ('proxied' in cur_record) and (cur_record['proxied'] != params['proxied']):
+                do_update = True
             if ('data' in new_record) and ('data' in cur_record):
                 if (cur_record['data'] != new_record['data']):
                     do_update = True


Test patch:
diff --git a/test/legacy/roles/test_cloudflare_dns/tasks/a_record.yml b/test/legacy/roles/test_cloudflare_dns/tasks/a_record.yml
--- a/test/legacy/roles/test_cloudflare_dns/tasks/a_record.yml
+++ b/test/legacy/roles/test_cloudflare_dns/tasks/a_record.yml
@@ -178,3 +178,153 @@
     that:
       - cloudflare_dns is successful
       - cloudflare_dns is not changed
+
+- name: "Test: proxiable A record creation"
+  cloudflare_dns:
+    account_email: "{{ cloudflare_email }}"
+    account_api_token: "{{ cloudflare_api_token }}"
+    zone: "{{ cloudflare_zone }}"
+    record: "{{ cloudflare_dns_record  }}"
+    type: A
+    value: 192.0.2.47
+    ttl: 150
+  register: cloudflare_dns
+
+- name: "Validate: proxiable A record creation"
+  assert:
+    that:
+      - cloudflare_dns is successful
+      - cloudflare_dns is changed
+      - cloudflare_dns.result.record.content == '192.0.2.47'
+      - cloudflare_dns.result.record.proxiable == true
+      - cloudflare_dns.result.record.ttl == 150
+      - cloudflare_dns.result.record.type == 'A'
+      - cloudflare_dns.result.record.name == "{{ cloudflare_dns_record }}.{{ cloudflare_zone }}"
+      - cloudflare_dns.result.record.zone_name == "{{ cloudflare_zone }}"
+
+- name: "Test: proxiable A record creation succeeded"
+  cloudflare_dns:
+    account_email: "{{ cloudflare_email }}"
+    account_api_token: "{{ cloudflare_api_token }}"
+    zone: "{{ cloudflare_zone }}"
+    record: "{{ cloudflare_dns_record  }}"
+    type: A
+    value: 192.0.2.47
+    ttl: 150
+  register: cloudflare_dns
+
+- name: "Validate: proxiable A record creation succeeded"
+  assert:
+    that:
+      - cloudflare_dns is successful
+      - cloudflare_dns is not changed
+
+- name: "Test: Enable A record proxied status"
+  cloudflare_dns:
+    account_email: "{{ cloudflare_email }}"
+    account_api_token: "{{ cloudflare_api_token }}"
+    zone: "{{ cloudflare_zone }}"
+    record: "{{ cloudflare_dns_record  }}"
+    type: A
+    value: 192.0.2.47
+    proxied: yes
+  register: cloudflare_dns
+
+- name: "Validate: Enable A record proxied status"
+  assert:
+    that:
+      - cloudflare_dns is successful
+      - cloudflare_dns is changed
+      - cloudflare_dns.result.record.content == '192.0.2.47'
+      - cloudflare_dns.result.record.proxied == true
+      - cloudflare_dns.result.record.type == 'A'
+      - cloudflare_dns.result.record.name == "{{ cloudflare_dns_record }}.{{ cloudflare_zone }}"
+      - cloudflare_dns.result.record.zone_name == "{{ cloudflare_zone }}"
+
+- name: "Test: Enable A record proxied status succeeded"
+  cloudflare_dns:
+    account_email: "{{ cloudflare_email }}"
+    account_api_token: "{{ cloudflare_api_token }}"
+    zone: "{{ cloudflare_zone }}"
+    record: "{{ cloudflare_dns_record  }}"
+    type: A
+    value: 192.0.2.47
+    proxied: yes
+  register: cloudflare_dns
+
+- name: "Validate: Enable A record proxied status succeeded"
+  assert:
+    that:
+      - cloudflare_dns is successful
+      - cloudflare_dns is not changed
+
+- name: "Test: Disable A record proxied status"
+  cloudflare_dns:
+    account_email: "{{ cloudflare_email }}"
+    account_api_token: "{{ cloudflare_api_token }}"
+    zone: "{{ cloudflare_zone }}"
+    record: "{{ cloudflare_dns_record  }}"
+    type: A
+    value: 192.0.2.47
+    proxied: no
+  register: cloudflare_dns
+
+- name: "Validate: Enable A record proxied status"
+  assert:
+    that:
+      - cloudflare_dns is successful
+      - cloudflare_dns is changed
+      - cloudflare_dns.result.record.content == '192.0.2.47'
+      - cloudflare_dns.result.record.proxied == false
+      - cloudflare_dns.result.record.type == 'A'
+      - cloudflare_dns.result.record.name == "{{ cloudflare_dns_record }}.{{ cloudflare_zone }}"
+      - cloudflare_dns.result.record.zone_name == "{{ cloudflare_zone }}"
+
+- name: "Test: Disable A record proxied status succeeded"
+  cloudflare_dns:
+    account_email: "{{ cloudflare_email }}"
+    account_api_token: "{{ cloudflare_api_token }}"
+    zone: "{{ cloudflare_zone }}"
+    record: "{{ cloudflare_dns_record  }}"
+    type: A
+    value: 192.0.2.47
+    proxied: no
+  register: cloudflare_dns
+
+- name: "Validate: Enable A record proxied status succeeded"
+  assert:
+    that:
+      - cloudflare_dns is successful
+      - cloudflare_dns is not changed
+
+- name: "Test: Full A record deletion"
+  cloudflare_dns:
+    account_email: "{{ cloudflare_email }}"
+    account_api_token: "{{ cloudflare_api_token }}"
+    zone: "{{ cloudflare_zone }}"
+    record: "{{ cloudflare_dns_record  }}"
+    type: A
+    state: absent
+  register: cloudflare_dns
+
+- name: "Validate: Full A record deletion"
+  assert:
+    that:
+      - cloudflare_dns is successful
+      - cloudflare_dns is changed
+
+- name: "Test: Full A record deletion succeeded"
+  cloudflare_dns:
+    account_email: "{{ cloudflare_email }}"
+    account_api_token: "{{ cloudflare_api_token }}"
+    zone: "{{ cloudflare_zone }}"
+    record: "{{ cloudflare_dns_record  }}"
+    type: A
+    state: absent
+  register: cloudflare_dns
+
+- name: "Validate: Full A record deletion succeeded"
+  assert:
+    that:
+      - cloudflare_dns is successful
+      - cloudflare_dns is not changed
diff --git a/test/legacy/roles/test_cloudflare_dns/tasks/aaaa_record.yml b/test/legacy/roles/test_cloudflare_dns/tasks/aaaa_record.yml
--- a/test/legacy/roles/test_cloudflare_dns/tasks/aaaa_record.yml
+++ b/test/legacy/roles/test_cloudflare_dns/tasks/aaaa_record.yml
@@ -178,3 +178,153 @@
     that:
       - cloudflare_dns is successful
       - cloudflare_dns is not changed
+
+- name: "Test: proxiable AAAA record creation"
+  cloudflare_dns:
+    account_email: "{{ cloudflare_email }}"
+    account_api_token: "{{ cloudflare_api_token }}"
+    zone: "{{ cloudflare_zone }}"
+    record: "{{ cloudflare_dns_record  }}"
+    type: AAAA
+    value: 2001:db8::47
+    ttl: 150
+  register: cloudflare_dns
+
+- name: "Validate: proxiable AAAA record creation"
+  assert:
+    that:
+      - cloudflare_dns is successful
+      - cloudflare_dns is changed
+      - cloudflare_dns.result.record.content == '2001:db8::47'
+      - cloudflare_dns.result.record.proxiable == true
+      - cloudflare_dns.result.record.ttl == 150
+      - cloudflare_dns.result.record.type == 'AAAA'
+      - cloudflare_dns.result.record.name == "{{ cloudflare_dns_record }}.{{ cloudflare_zone }}"
+      - cloudflare_dns.result.record.zone_name == "{{ cloudflare_zone }}"
+
+- name: "Test: proxiable AAAA record creation succeeded"
+  cloudflare_dns:
+    account_email: "{{ cloudflare_email }}"
+    account_api_token: "{{ cloudflare_api_token }}"
+    zone: "{{ cloudflare_zone }}"
+    record: "{{ cloudflare_dns_record  }}"
+    type: AAAA
+    value: 2001:db8::47
+    ttl: 150
+  register: cloudflare_dns
+
+- name: "Validate: proxiable AAAA record creation succeeded"
+  assert:
+    that:
+      - cloudflare_dns is successful
+      - cloudflare_dns is not changed
+
+- name: "Test: Enable AAAA record proxied status"
+  cloudflare_dns:
+    account_email: "{{ cloudflare_email }}"
+    account_api_token: "{{ cloudflare_api_token }}"
+    zone: "{{ cloudflare_zone }}"
+    record: "{{ cloudflare_dns_record  }}"
+    type: AAAA
+    value: 2001:db8::47
+    proxied: yes
+  register: cloudflare_dns
+
+- name: "Validate: Enable AAAA record proxied status"
+  assert:
+    that:
+      - cloudflare_dns is successful
+      - cloudflare_dns is changed
+      - cloudflare_dns.result.record.content == '2001:db8::47'
+      - cloudflare_dns.result.record.proxied == true
+      - cloudflare_dns.result.record.type == 'AAAA'
+      - cloudflare_dns.result.record.name == "{{ cloudflare_dns_record }}.{{ cloudflare_zone }}"
+      - cloudflare_dns.result.record.zone_name == "{{ cloudflare_zone }}"
+
+- name: "Test: Enable AAAA record proxied status succeeded"
+  cloudflare_dns:
+    account_email: "{{ cloudflare_email }}"
+    account_api_token: "{{ cloudflare_api_token }}"
+    zone: "{{ cloudflare_zone }}"
+    record: "{{ cloudflare_dns_record  }}"
+    type: AAAA
+    value: 2001:db8::47
+    proxied: yes
+  register: cloudflare_dns
+
+- name: "Validate: Enable AAAA record proxied status succeeded"
+  assert:
+    that:
+      - cloudflare_dns is successful
+      - cloudflare_dns is not changed
+
+- name: "Test: Disable AAAA record proxied status"
+  cloudflare_dns:
+    account_email: "{{ cloudflare_email }}"
+    account_api_token: "{{ cloudflare_api_token }}"
+    zone: "{{ cloudflare_zone }}"
+    record: "{{ cloudflare_dns_record  }}"
+    type: AAAA
+    value: 2001:db8::47
+    proxied: no
+  register: cloudflare_dns
+
+- name: "Validate: Enable AAAA record proxied status"
+  assert:
+    that:
+      - cloudflare_dns is successful
+      - cloudflare_dns is changed
+      - cloudflare_dns.result.record.content == '2001:db8::47'
+      - cloudflare_dns.result.record.proxied == false
+      - cloudflare_dns.result.record.type == 'AAAA'
+      - cloudflare_dns.result.record.name == "{{ cloudflare_dns_record }}.{{ cloudflare_zone }}"
+      - cloudflare_dns.result.record.zone_name == "{{ cloudflare_zone }}"
+
+- name: "Test: Disable AAAA record proxied status succeeded"
+  cloudflare_dns:
+    account_email: "{{ cloudflare_email }}"
+    account_api_token: "{{ cloudflare_api_token }}"
+    zone: "{{ cloudflare_zone }}"
+    record: "{{ cloudflare_dns_record  }}"
+    type: AAAA
+    value: 2001:db8::47
+    proxied: no
+  register: cloudflare_dns
+
+- name: "Validate: Enable AAAA record proxied status succeeded"
+  assert:
+    that:
+      - cloudflare_dns is successful
+      - cloudflare_dns is not changed
+
+- name: "Test: Full AAAA record deletion"
+  cloudflare_dns:
+    account_email: "{{ cloudflare_email }}"
+    account_api_token: "{{ cloudflare_api_token }}"
+    zone: "{{ cloudflare_zone }}"
+    record: "{{ cloudflare_dns_record  }}"
+    type: AAAA
+    state: absent
+  register: cloudflare_dns
+
+- name: "Validate: Full AAAA record deletion"
+  assert:
+    that:
+      - cloudflare_dns is successful
+      - cloudflare_dns is changed
+
+- name: "Test: Full AAAA record deletion succeeded"
+  cloudflare_dns:
+    account_email: "{{ cloudflare_email }}"
+    account_api_token: "{{ cloudflare_api_token }}"
+    zone: "{{ cloudflare_zone }}"
+    record: "{{ cloudflare_dns_record  }}"
+    type: AAAA
+    state: absent
+  register: cloudflare_dns
+
+- name: "Validate: Full AAAA record deletion succeeded"
+  assert:
+    that:
+      - cloudflare_dns is successful
+      - cloudflare_dns is not changed