Skip to content

Commit

Permalink
Add role prompt_password
Browse files Browse the repository at this point in the history
  • Loading branch information
huyz committed Mar 10, 2023
1 parent 0d3b194 commit 50bc091
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 2 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@ huyz.general Release Notes
.. contents:: Topics


v1.0.3
======

Release Summary
---------------

| Release Date: 2023-03-09
| Initial release of the password_prompt role

Major Changes
-------------

- prompt_password - initial commit

v1.0.2
======

Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ Roles:
- `huyz.general.add_to_config_file` ([README](https://github.com/huyz/ansible-collection-huyz-general/blob/master/roles/add_to_config_file/README.md))
- Function: Safely insert a block into one or more shell config files
- Use case: Mainly intended to be re-used by other roles
- `huyz.general.prompt_password` ([README](https://github.com/huyz/ansible-collection-huyz-general/blob/master/roles/prompt_password/README.md))
- Function: Prompts for the `ansible_password` if not defined.
- Use case: Avoid the need to call `ansible-playbook` with `--ask-pass` and `--ask-become-pass`

---

Expand Down
13 changes: 13 additions & 0 deletions changelogs/changelog.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,16 @@ releases:
- 2023-02-25_add_to_config_file.yml
- 2023-02-25_v1.0.2_summary.yml
release_date: '2023-02-25'
1.0.3:
changes:
major_changes:
- prompt_password - initial commit
release_summary: '| Release Date: 2023-03-09
| Initial release of the password_prompt role
'
fragments:
- 2023-03-09_prompt_password.yml
- 2023-03-09_v1.0.3_summary.yml
release_date: '2023-03-09'
2 changes: 2 additions & 0 deletions changelogs/fragments/2023-03-09_prompt_password.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
major_changes:
- prompt_password - initial commit
3 changes: 3 additions & 0 deletions changelogs/fragments/2023-03-09_v1.0.3_summary.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
release_summary: |
| Release Date: 2023-03-09
| Initial release of the password_prompt role
2 changes: 1 addition & 1 deletion galaxy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace: huyz
name: general

# The version of the collection. Must be compatible with semantic versioning
version: 1.0.2
version: 1.0.3

# The path to the Markdown (.md) readme file. This path is relative to the root of the collection
readme: README.md
Expand Down
2 changes: 1 addition & 1 deletion roles/add_to_config_file/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Safely insert a block into one or more shell config files:

## Installation

This repo uses the FCQN convention.
This repo uses the FQCN convention.

Include the collection in the Ansible Galaxy `requirements.yml`:

Expand Down
31 changes: 31 additions & 0 deletions roles/prompt_password/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
TODO: test with multiple hosts

# Ansible role: huyz.general.password_prompt

Prompts for the `ansible_password` if not defined, with the option to also set the `ansible_become_password` if not defined.

This avoids the need to call `ansible-playbook` with `--ask-pass` and `--ask-become-pass`.

## Installation

This repo uses the FQCN convention.

Include the collection in the Ansible Galaxy `requirements.yml`:

```shell
---
collections:
- name: huyz.general
```

You can then include the role `huyz.general.prompt_password`.

## Example

```yaml
- name: Prompt for ansible_password if needed
ansible.builtin.include_role:
name: huyz.generalprompt_password
vars:
copy_to_become_password: true
```
24 changes: 24 additions & 0 deletions roles/prompt_password/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Accepts vars:
# - copy_to_become_password: true to automatically set `become_password`
# as well (if not yet defined).
---
- name: Prompt for ssh password if necessary
when: ansible_password is undefined
block:
- name: Conditionally prompt for ssh password
ansible.builtin.pause:
prompt: "Password for {{ ansible_user_id | d('unknown') }}@{{ ansible_host | d('unknown') }}"
echo: false
register: password_prompt
no_log: true

- name: Set ansible_password
ansible.builtin.set_fact:
ansible_password: "{{ password_prompt.user_input }}"
no_log: true

- name: Set ansible_become_password
ansible.builtin.set_fact:
ansible_become_password: "{{ ansible_password }}"
no_log: true
when: copy_to_become_password | d(false) and ansible_become_password is undefined

0 comments on commit 50bc091

Please sign in to comment.