From cda6fc956f7db97cafda81cc54073e0bdab5efb5 Mon Sep 17 00:00:00 2001
From: k_e <niea.seven@gmail.com>
Date: Thu, 5 Oct 2023 01:47:37 +0900
Subject: [PATCH] Add TCP option for dig plugin. (#7343)

* Add TCP option for dig plugin.

* Add changelog of dig tcp option

* Apply suggestions from code review

Co-authored-by: Felix Fontein <felix@fontein.de>

---------

Co-authored-by: s-miyachi <s-miyachi@jocdn.co.jp>
Co-authored-by: Felix Fontein <felix@fontein.de>
---
 changelogs/fragments/7343-dig-tcp-option.yml |  2 ++
 plugins/lookup/dig.py                        | 10 +++++++++-
 2 files changed, 11 insertions(+), 1 deletion(-)
 create mode 100644 changelogs/fragments/7343-dig-tcp-option.yml

diff --git a/changelogs/fragments/7343-dig-tcp-option.yml b/changelogs/fragments/7343-dig-tcp-option.yml
new file mode 100644
index 0000000000..6c2415f041
--- /dev/null
+++ b/changelogs/fragments/7343-dig-tcp-option.yml
@@ -0,0 +1,2 @@
+minor_changes:
+  - dig lookup plugin - add TCP option to enable the use of TCP connection during DNS lookup (https://github.com/ansible-collections/community.general/pull/7343).
diff --git a/plugins/lookup/dig.py b/plugins/lookup/dig.py
index afeab28f44..5be57cec78 100644
--- a/plugins/lookup/dig.py
+++ b/plugins/lookup/dig.py
@@ -70,6 +70,11 @@ DOCUMENTATION = '''
           - "Class."
         type: str
         default: 'IN'
+      tcp:
+        description: Use TCP to lookup DNS records.
+        default: false
+        type: bool
+        version_added: 7.5.0
     notes:
       - ALL is not a record per-se, merely the listed fields are available for any record results you retrieve in the form of a dictionary.
       - While the 'dig' lookup plugin supports anything which dnspython supports out of the box, only a subset can be converted into a dictionary.
@@ -329,6 +334,7 @@ class LookupModule(LookupBase):
         flat = self.get_option('flat')
         fail_on_error = self.get_option('fail_on_error')
         real_empty = self.get_option('real_empty')
+        tcp = self.get_option('tcp')
         try:
             rdclass = dns.rdataclass.from_text(self.get_option('class'))
         except Exception as e:
@@ -375,6 +381,8 @@ class LookupModule(LookupBase):
                     fail_on_error = boolean(arg)
                 elif opt == 'real_empty':
                     real_empty = boolean(arg)
+                elif opt == 'tcp':
+                    tcp = boolean(arg)
 
                 continue
 
@@ -408,7 +416,7 @@ class LookupModule(LookupBase):
 
         for domain in domains:
             try:
-                answers = myres.query(domain, qtype, rdclass=rdclass)
+                answers = myres.query(domain, qtype, rdclass=rdclass, tcp=tcp)
                 for rdata in answers:
                     s = rdata.to_text()
                     if qtype.upper() == 'TXT':