From 5907f1617a1c40b5544ffc08e357c2f629e5d487 Mon Sep 17 00:00:00 2001 From: Markus Pesch Date: Sat, 2 Mar 2024 19:20:05 +0100 Subject: [PATCH] feat: support bashrc aliases, envs and functions --- defaults/default.yml | 12 ++++++++++++ tasks/create_shell_rc_file.yml | 25 +++++++++++++++++++++++++ tasks/create_unix_user.yml | 7 +++++++ templates/shell_rc_file.j2 | 23 +++++++++++++++++++++++ 4 files changed, 67 insertions(+) create mode 100644 tasks/create_shell_rc_file.yml create mode 100644 templates/shell_rc_file.j2 diff --git a/defaults/default.yml b/defaults/default.yml index 64b93ea..281c1c1 100644 --- a/defaults/default.yml +++ b/defaults/default.yml @@ -25,6 +25,18 @@ unix_users: {} # home: /home/alice # btrfs: false # shell: /bin/bash +# shell_rc_files: +# - file: "/home/alice/.bashrc.d" +# aliases: +# - key: "dcd" +# value: "docker-compose down" +# envs: +# - export: true +# key: "PATH" +# value: "${XDG_BIN_DIR}:${PATH}" +# functions: +# - name: "foo" +# value: "echo \"bar\"" # group: alice # groups: [] # password: "" diff --git a/tasks/create_shell_rc_file.yml b/tasks/create_shell_rc_file.yml new file mode 100644 index 0000000..937e1aa --- /dev/null +++ b/tasks/create_shell_rc_file.yml @@ -0,0 +1,25 @@ +--- + +- name: "Create shell rc file directory {{ shell_rc_file.file | dirname }}" + ansible.builtin.file: + path: "{{ shell_rc_file.file | dirname }}" + owner: "{{ unix_user.key }}" + group: "{{ unix_user.value.group | default('users') }}" + mode: "0755" + state: "directory" + +- name: "Create shell rc file {{ shell_rc_file.file }}" + ansible.builtin.template: + src: shell_rc_file.j2 + dest: "{{ shell_rc_file.file }}" + owner: "{{ unix_user.key }}" + group: "{{ unix_user.value.group | default('users') }}" + mode: "0644" + +- name: "Source shell rc file {{ shell_rc_file.file }}" + ansible.builtin.lineinfile: + path: "{{ user_user_home }}/.bashrc" + line: "source \"{{ shell_rc_file.file }}\"" + owner: "{{ unix_user.key }}" + group: "{{ unix_user.value.group | default('users') }}" + state: "present" diff --git a/tasks/create_unix_user.yml b/tasks/create_unix_user.yml index 8c772c9..7f69398 100644 --- a/tasks/create_unix_user.yml +++ b/tasks/create_unix_user.yml @@ -187,3 +187,10 @@ - "{{ unix_user.value.xdg.dirs.config | default(user_user_home + '/.config') }}" - "{{ unix_user.value.xdg.dirs.data | default(user_user_home + '/.local/share') }}" - "{{ unix_user.value.xdg.dirs.state | default(user_user_home + '/.local/state') }}" + +- name: "Create shell rc files" + ansible.builtin.include_tasks: create_shell_rc_file.yml + with_items: + - "{{ unix_user.value.shell_rc_files }}" + loop_control: + loop_var: shell_rc_file diff --git a/templates/shell_rc_file.j2 b/templates/shell_rc_file.j2 new file mode 100644 index 0000000..b0569d4 --- /dev/null +++ b/templates/shell_rc_file.j2 @@ -0,0 +1,23 @@ +# +# {{ ansible_managed }} +# + +{% if shell_rc_file.functions is defined %} +{% for function in shell_rc_file.functions %} +function {{ function.name }} { +{{ function.body | indent(2, True) }} +} + +{% endfor %} +{% endif %} +{% if shell_rc_file.envs is defined %} +{% for env in shell_rc_file.envs %} +{{ 'export ' if env.export is defined and env.export }}{{ env.key }}="{{ env.value }}" +{% endfor %} +{% endif %} + +{% if shell_rc_file.aliases is defined %} +{% for alias in shell_rc_file.aliases %} +alias {{ alias.key }}='{{ alias.value }}' +{% endfor %} +{% endif %} \ No newline at end of file