Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Ansible
Manage
Activity
Members
Labels
Plan
Issues
3
Issue boards
Milestones
Code
Merge requests
2
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Nounous
Ansible
Commits
c0e02b29
Commit
c0e02b29
authored
4 years ago
by
Maxime Bombar
Browse files
Options
Downloads
Patches
Plain Diff
[re2o_lookup] Add support for json, yaml, pickle and memcached cache plugins.
parent
f73b136b
No related branches found
Branches containing commit
No related tags found
1 merge request
!18
Cache lookup api
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
ansible.cfg
+5
-0
5 additions, 0 deletions
ansible.cfg
lookup_plugins/re2oapi.py
+50
-18
50 additions, 18 deletions
lookup_plugins/re2oapi.py
with
55 additions
and
18 deletions
ansible.cfg
+
5
−
0
View file @
c0e02b29
...
...
@@ -49,6 +49,11 @@ use_cpasswords = True
# Specify
cache plugin for re2o API. By default, cache nothing
cache
= jsonfile
# Only
used for memcached plugin
# List
of connection information for the memcached DBs
# Default
is ['
127.0.0.1
:11211']
# memcached_connection
= ['
127.0.0.1
:11211']
# Time
in second before the cache expired. 0 means never expire cache.
# Default
is 24 hours.
timeout
= 86400
...
...
This diff is collapsed.
Click to expand it.
lookup_plugins/re2oapi.py
+
50
−
18
View file @
c0e02b29
...
...
@@ -374,6 +374,27 @@ class LookupModule(LookupBase):
return
config
.
getint
(
section
,
key
)
else
:
return
config
.
get
(
section
,
key
)
else
:
return
default
def
_manage_cachedir
(
self
,
cachedir
=
None
,
plugin
=
None
):
try
:
self
.
_uri
=
cachedir
/
plugin
except
Exception
:
raise
AnsibleError
(
"
Undefined specification for cache plugin
"
)
display
.
vvv
(
"
Cache directory is {}
"
.
format
(
self
.
_uri
))
if
not
self
.
_uri
.
exists
():
# Creates Ansible cache directory with right permissions
# if it doesn't exist yet.
display
.
vvv
(
"
Cache directory doesn
'
t exist. Creating it.
"
)
try
:
self
.
_uri
.
mkdir
(
mode
=
0o700
,
parents
=
True
)
except
Exception
as
e
:
raise
AnsibleError
(
"""
Unable to create {dir}.
Original error was : {err}
"""
.
format
(
dir
=
self
.
_uri
,
err
=
to_native
(
e
)))
def
__init__
(
self
,
*
args
,
**
kwargs
):
super
().
__init__
(
*
args
,
**
kwargs
)
...
...
@@ -410,29 +431,36 @@ class LookupModule(LookupBase):
if
self
.
_cache_plugin
is
not
None
:
display
.
vvv
(
"
Using {} as cache plugin
"
.
format
(
self
.
_cache_plugin
))
cachedir
=
Path
.
home
()
/
"
.cache/ansible/re2oapi
"
if
self
.
_cache_plugin
==
'
jsonfile
'
:
self
.
_cachedir
=
Path
.
home
()
/
"
.cache/Ansible/re2oapi
"
display
.
vvv
(
"
Cache directory is {}
"
.
format
(
self
.
_cachedir
))
if
not
self
.
_cachedir
.
exists
():
# Creates Ansible cache directory with right permissions
# if it doesn't exist yet.
display
.
vvv
(
"
Cache directory doesn
'
t exist. Creating it.
"
)
try
:
self
.
_cachedir
.
mkdir
(
mode
=
0o700
,
parents
=
True
)
except
Exception
as
e
:
raise
AnsibleError
(
"""
Unable to create {dir}.
Original error was : {err}
"""
.
format
(
dir
=
self
.
_cachedir
,
err
=
to_native
(
e
)))
self
.
_cache
=
cache_loader
.
get
(
'
jsonfile
'
,
_uri
=
self
.
_cachedir
,
_timeout
=
self
.
_timeout
,
)
self
.
_manage_cachedir
(
cachedir
=
cachedir
,
plugin
=
'
json
'
)
elif
self
.
_cache_plugin
==
'
yaml
'
:
self
.
_manage_cachedir
(
cachedir
=
cachedir
,
plugin
=
'
yaml
'
)
elif
self
.
_cache_plugin
==
'
pickle
'
:
self
.
_manage_cachedir
(
cachedir
=
cachedir
,
plugin
=
'
pickle
'
)
elif
self
.
_cache_plugin
==
'
memcached
'
:
# requires packages python3-memcache and memcached
display
.
vvvv
(
"
Please make sure you have installed packages
"
"
python3-memcache and memcached
"
)
self
.
_uri
=
self
.
_readconfig
(
key
=
'
memcached_connection
'
,
default
=
[
'
127.0.0.1:11211
'
],
)
else
:
raise
AnsibleError
(
"
Cache plugin {} not supported
"
.
format
(
self
.
_cache_plugin
))
self
.
_cache
=
cache_loader
.
get
(
self
.
_cache_plugin
,
_uri
=
self
.
_uri
,
_timeout
=
self
.
_timeout
,
)
self
.
_cachetoken
=
cache_loader
.
get
(
self
.
_cache_plugin
,
_uri
=
self
.
_uri
,
_timeout
=
self
.
_timeouttoken
,
)
def
run
(
self
,
terms
,
variables
=
None
,
api_hostname
=
None
,
api_username
=
None
,
api_password
=
None
,
use_tls
=
True
):
...
...
@@ -546,7 +574,7 @@ class LookupModule(LookupBase):
zones_name
=
[
zone
[
"
name
"
][
1
:]
for
zone
in
zones
]
display
.
vvv
(
"
Storing dnszones in cache.
"
)
self
.
_set_cache
(
'
dnszones
'
,
zones_name
)
display
.
vvv
(
'
\n
'
)
return
zones_name
def
_getreverse
(
self
,
api_client
):
...
...
@@ -615,6 +643,7 @@ class LookupModule(LookupBase):
display
.
vvv
(
"
Storing dns reverse zones in cache.
"
)
self
.
_set_cache
(
'
dnsreverse
'
,
list
(
set
(
res
)))
display
.
vvv
(
'
\n
'
)
return
res
def
_rawquery
(
self
,
api_client
,
endpoint
):
...
...
@@ -629,6 +658,8 @@ class LookupModule(LookupBase):
res
=
api_client
.
list
(
endpoint
)
display
.
vvv
(
"
Storing result in cache.
"
)
self
.
_set_cache
(
endpoint
.
replace
(
'
/
'
,
'
_
'
),
res
)
display
.
vvv
(
'
\n
'
)
return
res
def
_get_role
(
self
,
api_client
,
role_name
):
...
...
@@ -655,4 +686,5 @@ class LookupModule(LookupBase):
display
.
vvv
(
"
Storing {} in cache.
"
.
format
(
role_name
))
self
.
_set_cache
(
role_name
,
res
)
display
.
vvv
(
'
\n
'
)
return
res
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment