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
44a59d11
Commit
44a59d11
authored
4 years ago
by
ynerant
Committed by
ynerant
4 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Add configuration to query become password from password store
Signed-off-by:
Yohann D'ANELLO
<
ynerant@crans.org
>
parent
39441c81
Loading
Loading
1 merge request
!231
Load passwords from password store
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
ansible.cfg
+2
-2
2 additions, 2 deletions
ansible.cfg
vars_plugins/pass.ini.example
+6
-2
6 additions, 2 deletions
vars_plugins/pass.ini.example
vars_plugins/pass.py
+41
-5
41 additions, 5 deletions
vars_plugins/pass.py
with
49 additions
and
9 deletions
ansible.cfg
+
2
−
2
View file @
44a59d11
...
...
@@ -41,8 +41,8 @@ fact_caching_timeout = 86400
# Use
sudo to get priviledge access
become
= True
#
Ask
for
password
become_ask_pass
=
Tru
e
#
Use
custom
password
request
become_ask_pass
=
Fals
e
[ssh_connection]
...
...
This diff is collapsed.
Click to expand it.
vars_plugins/pass.ini.example
+
6
−
2
View file @
44a59d11
[pass]
password_store_dir=/home/me/.password-store
crans_password_store_submodule=crans
# password_store_dir=/home/me/.password-store
# crans_password_store_submodule=crans
[pass_become]
# all=mdp-root
# adh_server=mdp-zamok
This diff is collapsed.
Click to expand it.
vars_plugins/pass.py
+
41
−
5
View file @
44a59d11
#!/usr/bin/env python
from
functools
import
lru_cache
from
getpass
import
getpass
import
os
from
pathlib
import
Path
import
subprocess
...
...
@@ -28,7 +29,7 @@ DOCUMENTATION = """
class
VarsModule
(
BaseVarsPlugin
):
@staticmethod
@lru_cache
def
vaul
t_password
s
():
def
decryp
t_password
(
name
,
crans_submodule
=
False
):
"""
Passwords are decrypted from the local password store, then are cached.
By that way, we don
'
t decrypt these passwords everytime.
...
...
@@ -39,14 +40,36 @@ class VarsModule(BaseVarsPlugin):
password_store
=
Path
(
config
.
get
(
'
pass
'
,
'
password_store_dir
'
,
fallback
=
os
.
getenv
(
'
PASSWORD_STORE_DIR
'
,
Path
.
home
()
/
'
.password-store
'
)))
crans_submodule
=
config
.
get
(
'
pass
'
,
'
crans_password_store_submodule
'
,
fallback
=
os
.
getenv
(
'
CRANS_PASSWORD_STORE_SUBMODULE
'
,
'
crans
'
))
full_command
=
[
'
gpg
'
,
'
-d
'
,
password_store
/
crans_submodule
/
'
ansible_vault.gpg
'
]
if
crans_submodule
:
password_store
/=
config
.
get
(
'
pass
'
,
'
crans_password_store_submodule
'
,
fallback
=
os
.
getenv
(
'
CRANS_PASSWORD_STORE_SUBMODULE
'
,
'
crans
'
))
full_command
=
[
'
gpg
'
,
'
-d
'
,
password_store
/
f
'
{
name
}
.gpg
'
]
proc
=
subprocess
.
run
(
full_command
,
capture_output
=
True
,
close_fds
=
True
)
clear_text
=
proc
.
stdout
.
decode
(
'
UTF-8
'
)
sys
.
stderr
.
write
(
proc
.
stderr
.
decode
(
'
UTF-8
'
))
return
clear_text
@staticmethod
@lru_cache
def
become_password
(
entity
):
"""
Query the become password that should be used for the given entity.
If entity is the whole group that has no default password,
the become password will be prompted.
The configuration should be given in pass.ini, in the `pass_become`
group. You have only to write `group=pass-filename`.
"""
# Load config
config
=
configparser
.
ConfigParser
()
config
.
read
(
os
.
path
.
join
(
os
.
path
.
dirname
(
os
.
path
.
realpath
(
__file__
)),
'
pass.ini
'
))
if
config
.
has_option
(
'
pass_become
'
,
entity
.
get_name
()):
return
VarsModule
.
decrypt_password
(
config
.
get
(
'
pass_become
'
,
entity
.
get_name
())).
split
(
'
\n
'
)[
0
]
if
entity
.
get_name
()
==
"
all
"
:
return
getpass
(
"
BECOME password:
"
,
stream
=
None
)
return
None
def
get_vars
(
self
,
loader
,
path
,
entities
):
"""
Get all vars for entities, called by Ansible.
...
...
@@ -63,4 +86,17 @@ class VarsModule(BaseVarsPlugin):
# It is way to much.
# So we cache the data into the DataLoader (see parsing/DataLoader).
return
{
'
vault
'
:
loader
.
load
(
VarsModule
.
vault_passwords
())}
passwords
=
{}
for
entity
in
entities
:
# Load vault passwords
if
entity
.
get_name
()
==
'
all
'
:
passwords
[
'
vault
'
]
=
loader
.
load
(
VarsModule
.
decrypt_password
(
'
ansible_vault
'
,
True
))
# Load become password
become_password
=
VarsModule
.
become_password
(
entity
)
if
become_password
is
not
None
:
passwords
[
'
ansible_become_password
'
]
=
become_password
return
passwords
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