feat: enhance 1Password lookup functionality with vault ID support and concealment option
Some checks failed
Nix Format Check / check-format (push) Failing after 37s
Some checks failed
Nix Format Check / check-format (push) Failing after 37s
This commit is contained in:
parent
47fb912c15
commit
d787b25917
@ -14,8 +14,11 @@ password: "{{ lookup('onepassword', 'item-name') }}"
|
|||||||
# Fetch specific field
|
# Fetch specific field
|
||||||
api_key: "{{ lookup('onepassword', 'item-name', field='api_key') }}"
|
api_key: "{{ lookup('onepassword', 'item-name', field='api_key') }}"
|
||||||
|
|
||||||
# Fetch from specific vault
|
# Fetch from specific vault (using vault ID)
|
||||||
database_password: "{{ lookup('onepassword', 'database', field='password', vault='Development') }}"
|
database_password: "{{ lookup('onepassword', 'database', field='password', vault='j7nmhqlsjmp2r6umly5t75hzb4') }}"
|
||||||
|
|
||||||
|
# Fetch a field without revealing it (for non-password fields)
|
||||||
|
note: "{{ lookup('onepassword', 'item-name', field='notes', reveal=false) }}"
|
||||||
```
|
```
|
||||||
|
|
||||||
### Prerequisites
|
### Prerequisites
|
||||||
@ -24,5 +27,13 @@ database_password: "{{ lookup('onepassword', 'database', field='password', vault
|
|||||||
2. Sign in to 1Password using `op signin`
|
2. Sign in to 1Password using `op signin`
|
||||||
3. Service account should be properly configured
|
3. Service account should be properly configured
|
||||||
|
|
||||||
|
### Finding Vault IDs
|
||||||
|
|
||||||
|
To find your vault ID:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
op vault list
|
||||||
|
```
|
||||||
|
|
||||||
For more information, see the [1Password CLI documentation](https://developer.1password.com/docs/cli).
|
For more information, see the [1Password CLI documentation](https://developer.1password.com/docs/cli).
|
||||||
```
|
```
|
||||||
|
@ -17,14 +17,22 @@ DOCUMENTATION = """
|
|||||||
required: false
|
required: false
|
||||||
default: password
|
default: password
|
||||||
vault:
|
vault:
|
||||||
description: the vault to fetch from
|
description: the vault to fetch from (name or ID)
|
||||||
required: false
|
required: false
|
||||||
|
reveal:
|
||||||
|
description: whether to reveal concealed fields
|
||||||
|
required: false
|
||||||
|
default: true
|
||||||
"""
|
"""
|
||||||
|
|
||||||
EXAMPLES = """
|
EXAMPLES = """
|
||||||
- name: fetch password for an item
|
- name: fetch password for an item
|
||||||
debug:
|
debug:
|
||||||
msg: "{{ lookup('onepassword', 'storage-box', field='password') }}"
|
msg: "{{ lookup('onepassword', 'xxxx', field='password') }}"
|
||||||
|
|
||||||
|
- name: fetch password from specific vault
|
||||||
|
debug:
|
||||||
|
msg: "{{ lookup('onepassword', 'xxxx', field='password', vault='xxxx') }}"
|
||||||
"""
|
"""
|
||||||
|
|
||||||
RETURN = """
|
RETURN = """
|
||||||
@ -47,12 +55,17 @@ class LookupModule(LookupBase):
|
|||||||
item = terms[0]
|
item = terms[0]
|
||||||
field = kwargs.get('field', 'password')
|
field = kwargs.get('field', 'password')
|
||||||
vault = kwargs.get('vault', '')
|
vault = kwargs.get('vault', '')
|
||||||
|
reveal = kwargs.get('reveal', True)
|
||||||
|
|
||||||
vault_arg = []
|
cmd = ['op', 'item', 'get', item, '--field', field]
|
||||||
|
|
||||||
|
# Add vault parameter if specified
|
||||||
if vault:
|
if vault:
|
||||||
vault_arg = ['--vault', vault]
|
cmd.extend(['--vault', vault])
|
||||||
|
|
||||||
cmd = ['op', 'item', 'get', item, '--field', field] + vault_arg
|
# Add reveal flag for concealed fields
|
||||||
|
if reveal and field.lower() in ['password', 'secret', 'token', 'key']:
|
||||||
|
cmd.append('--reveal')
|
||||||
|
|
||||||
display.vvv(f"Executing command: {' '.join(cmd)}")
|
display.vvv(f"Executing command: {' '.join(cmd)}")
|
||||||
|
|
||||||
@ -65,4 +78,11 @@ class LookupModule(LookupBase):
|
|||||||
)
|
)
|
||||||
return [result.stdout.strip()]
|
return [result.stdout.strip()]
|
||||||
except subprocess.CalledProcessError as e:
|
except subprocess.CalledProcessError as e:
|
||||||
raise AnsibleError(f"Error fetching from 1Password: {e.stderr}")
|
error_msg = e.stderr.strip()
|
||||||
|
display.warning(f"Error executing 1Password CLI: {error_msg}")
|
||||||
|
display.warning(f"Command used: {' '.join(cmd)}")
|
||||||
|
|
||||||
|
if "not found" in error_msg:
|
||||||
|
return [f"Secret '{item}' not found in 1Password"]
|
||||||
|
|
||||||
|
raise AnsibleError(f"Error fetching from 1Password: {error_msg}")
|
||||||
|
@ -18,7 +18,7 @@
|
|||||||
dest: /root/.smbcredentials
|
dest: /root/.smbcredentials
|
||||||
content: |
|
content: |
|
||||||
username=u451316
|
username=u451316
|
||||||
password={{ lookup('onepassword', 'storage-box', field='password') | default('CHANGE_ME') }}
|
password={{ lookup('onepassword', '5j5y5axfjr3f3sn5nixb6htg4y', field='password', vault='j7nmhqlsjmp2r6umly5t75hzb4') | default('CHANGE_ME') }}
|
||||||
mode: '0600'
|
mode: '0600'
|
||||||
|
|
||||||
- name: Add fstab entry for storage-box
|
- name: Add fstab entry for storage-box
|
||||||
|
Loading…
x
Reference in New Issue
Block a user