Skip to content

Commit 4f2e03f

Browse files
committed
add bashrc.d mechanism for running initialization scripts when a new bash shell is created
1 parent 474520f commit 4f2e03f

3 files changed

Lines changed: 36 additions & 0 deletions

File tree

roles/bashrc_d/tasks/main.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
- name: create ~/.bashrc.d directory
3+
file:
4+
path: ~/.bashrc.d
5+
state: directory
6+
mode: 0755
7+
become: yes
8+
become_user: "{{ ansible_env.SUDO_USER }}"
9+
10+
- name: Setup ~/.bashrc to load .bash files from ~/.bashrc.d
11+
blockinfile:
12+
dest: ~/.bashrc
13+
block: |
14+
# Load *.bash files from ~/.bashrc.d
15+
for config in "$HOME"/.bashrc.d/*.bash ; do
16+
. "$config"
17+
done
18+
unset -v config
19+
become: yes
20+
become_user: "{{ ansible_env.SUDO_USER }}"

site.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@
1212
- { role: readme, tags: "readme" }
1313
- { role: ansible-lint, tags: "ansible-lint" }
1414
- { role: testinfra, tags: "testinfra" }
15+
- { role: bashrc_d, tags: "bashrc_d" }

spec/test_bashrc_d.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import textwrap
2+
import os
3+
4+
def test_bashrc_loads_files_from_bashrc_d_(host):
5+
bashrc = host.file(f"{os.environ['HOME']}/.bashrc")
6+
snippet = textwrap.dedent(
7+
'''
8+
# Load *.bash files from ~/.bashrc.d
9+
for config in "$HOME"/.bashrc.d/*.bash ; do
10+
. "$config"
11+
done
12+
unset -v config
13+
'''
14+
)
15+
assert snippet in bashrc.content_string

0 commit comments

Comments
 (0)