From 6770baf7f94b1c8c920cbb07d0aec5f3409c2a40 Mon Sep 17 00:00:00 2001 From: Brian Lee Date: Wed, 8 Nov 2023 10:15:51 -0800 Subject: [PATCH] Use with_first_found to selectively run tasks. --- README.md | 2 +- tasks/ArchLinux.yml | 56 ++++++++++++++++++++++++++++++ tasks/Debian.yml | 55 +++++++++++++++++++++++++++++ tasks/RedHat-7.yml | 58 +++++++++++++++++++++++++++++++ tasks/Ubuntu.yml | 50 +++++++++++++++++++++++++++ tasks/default.yml | 84 +++++++++++++++++++++++++++++++++++++++++++++ tasks/main.yml | 82 ++++++------------------------------------- 7 files changed, 314 insertions(+), 73 deletions(-) create mode 100644 tasks/ArchLinux.yml create mode 100644 tasks/Debian.yml create mode 100644 tasks/RedHat-7.yml create mode 100644 tasks/Ubuntu.yml create mode 100644 tasks/default.yml diff --git a/README.md b/README.md index be01e2c..1972089 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Tested on: * Archlinux * Debian 10, 11, 12 * Ubuntu 20.04, 22.04 -* !RedHat 7.9 +* RedHat 7.9 ## Requirements diff --git a/tasks/ArchLinux.yml b/tasks/ArchLinux.yml new file mode 100644 index 0000000..cc33a08 --- /dev/null +++ b/tasks/ArchLinux.yml @@ -0,0 +1,56 @@ +--- +- import_tasks: vim.yml + +- name: Ensure Arch Linux sources bash_aliases + lineinfile: + path: /root/.bashrc + # regexp: '\.bash_aliases' + line: '[ -f ~/.bash_aliases ] && source ~/.bash_aliases' + create: yes + +- name: Copy shell configuration + ansible.builtin.copy: + src: inputrc + dest: "{{ item }}/.inputrc" + loop: + - /etc/skel + - /root + - "{{ ansible_env.HOME }}/" + +- name: Copy bash aliases + ansible.builtin.copy: + src: bash_aliases + dest: "{{ item }}/.bash_aliases" + loop: + - /etc/skel + - /root + - "{{ ansible_env.HOME }}/" + +- name: Add root bashrc shell history configuration + lineinfile: + path: /root/.bashrc + regexp: '^(export )?HISTCONTROL=' + line: 'HISTCONTROL=ignoreboth' + state: present + +- name: Add root bashrc and vim configuration for RedHat + lineinfile: + path: /root/.bashrc + regexp: "{{ item.regexp }}" + line: "{{ item.line }}" + state: present + with_items: + - regexp: '^(export )?VISUAL=' + line: 'export VISUAL=vim' + - regexp: '^(export )?EDITOR=' + line: 'export EDITOR=vim' + - regexp: '^(export )?SUDO_EDITOR=' + line: 'export SUDO_EDITOR=vim' + +- name: Add functional alias to restart a service and show the log + blockinfile: + path: /root/.bashrc + block: | + pheonix() { + systemctl restart $1;journalctl -fu $1 + } \ No newline at end of file diff --git a/tasks/Debian.yml b/tasks/Debian.yml new file mode 100644 index 0000000..ba9c739 --- /dev/null +++ b/tasks/Debian.yml @@ -0,0 +1,55 @@ +--- +- import_tasks: vim-Debian.yml + +- name: Ensure Debian sources .bash_aliases + ansible.builtin.lineinfile: + path: /root/.bashrc + # regexp: '\.bash_aliases' + line: 'source ~/.bash_aliases' + +- name: Copy shell configuration + ansible.builtin.copy: + src: inputrc + dest: "{{ item }}/.inputrc" + loop: + - /etc/skel + - /root + - "{{ ansible_env.HOME }}/" + +- name: Copy bash aliases + ansible.builtin.copy: + src: bash_aliases + dest: "{{ item }}/.bash_aliases" + loop: + - /etc/skel + - /root + - "{{ ansible_env.HOME }}/" + +- name: Add root bashrc shell history configuration + lineinfile: + path: /root/.bashrc + regexp: '^(export )?HISTCONTROL=' + line: 'HISTCONTROL=ignoreboth' + state: present + +- name: Add root bashrc and vim configuration. + lineinfile: + path: /root/.bashrc + regexp: "{{ item.regexp }}" + line: "{{ item.line }}" + state: present + with_items: + - regexp: '^(export )?VISUAL=' + line: 'export VISUAL=vim.basic' + - regexp: '^(export )?EDITOR=' + line: 'export EDITOR=vim.basic' + - regexp: '^(export )?SUDO_EDITOR=' + line: 'export SUDO_EDITOR=vim.basic' + +- name: Add functional alias to restart a service and show the log + blockinfile: + path: /root/.bashrc + block: | + pheonix() { + systemctl restart $1;journalctl -fu $1 + } \ No newline at end of file diff --git a/tasks/RedHat-7.yml b/tasks/RedHat-7.yml new file mode 100644 index 0000000..1f266a1 --- /dev/null +++ b/tasks/RedHat-7.yml @@ -0,0 +1,58 @@ +--- +- import_tasks: vim.yml + when: ansible_os_family != 'Debian' + +#- name: Ensure Arch Linux sources bash_aliases +# lineinfile: +# path: /root/.bashrc +# # regexp: '\.bash_aliases' +# line: '[ -f ~/.bash_aliases ] && source ~/.bash_aliases' +# create: yes +# when: ansible_os_family == 'Archlinux' + +- name: Copy shell configuration + ansible.builtin.copy: + src: inputrc + dest: "{{ item }}/.inputrc" + loop: + - /etc/skel + - /root + - "{{ ansible_env.HOME }}/" + +#- name: Copy bash aliases +# ansible.builtin.copy: +# src: bash_aliases +# dest: "{{ item }}/.bash_aliases" +# loop: +# - /etc/skel +# - /root +# - "{{ ansible_env.HOME }}/" + +#- name: Add root bashrc shell history configuration +# lineinfile: +# path: /root/.bashrc +# regexp: '^(export )?HISTCONTROL=' +# line: 'HISTCONTROL=ignoreboth' +# state: present +# +#- name: Add root bashrc and vim configuration for RedHat +# lineinfile: +# path: /root/.bashrc +# regexp: "{{ item.regexp }}" +# line: "{{ item.line }}" +# state: present +# with_items: +# - regexp: '^(export )?VISUAL=' +# line: 'export VISUAL=vim' +# - regexp: '^(export )?EDITOR=' +# line: 'export EDITOR=vim' +# - regexp: '^(export )?SUDO_EDITOR=' +# line: 'export SUDO_EDITOR=vim' +# +#- name: Add functional alias to restart a service and show the log +# blockinfile: +# path: /root/.bashrc +# block: | +# pheonix() { +# systemctl restart $1;journalctl -fu $1 +# } \ No newline at end of file diff --git a/tasks/Ubuntu.yml b/tasks/Ubuntu.yml new file mode 100644 index 0000000..d10e79a --- /dev/null +++ b/tasks/Ubuntu.yml @@ -0,0 +1,50 @@ +--- +- import_tasks: vim-Debian.yml + +- name: Copy shell configuration + ansible.builtin.copy: + src: inputrc + dest: "{{ item }}/.inputrc" + loop: + - /etc/skel + - /root + - "{{ ansible_env.HOME }}/" + +- name: Copy bash aliases + ansible.builtin.copy: + src: bash_aliases + dest: "{{ item }}/.bash_aliases" + loop: + - /etc/skel + - /root + - "{{ ansible_env.HOME }}/" + +- name: Add root bashrc shell history configuration + lineinfile: + path: /root/.bashrc + regexp: '^(export )?HISTCONTROL=' + line: 'HISTCONTROL=ignoreboth' + state: present + +- name: Add root bashrc and vim configuration. + lineinfile: + path: /root/.bashrc + regexp: "{{ item.regexp }}" + line: "{{ item.line }}" + state: present + with_items: + - regexp: '^(export )?VISUAL=' + line: 'export VISUAL=vim.basic' + - regexp: '^(export )?EDITOR=' + line: 'export EDITOR=vim.basic' + - regexp: '^(export )?SUDO_EDITOR=' + line: 'export SUDO_EDITOR=vim.basic' + when: ansible_os_family == 'Debian' + +- name: Add functional alias to restart a service and show the log + blockinfile: + path: /root/.bashrc + block: | + pheonix() { + systemctl restart $1;journalctl -fu $1 + } \ No newline at end of file diff --git a/tasks/default.yml b/tasks/default.yml new file mode 100644 index 0000000..e8eac5a --- /dev/null +++ b/tasks/default.yml @@ -0,0 +1,84 @@ +--- +- import_tasks: vim-Debian.yml + when: ansible_os_family == 'Debian' + +- import_tasks: vim.yml + when: ansible_os_family != 'Debian' + +- name: Ensure Arch Linux sources bash_aliases + lineinfile: + path: /root/.bashrc + # regexp: '\.bash_aliases' + line: '[ -f ~/.bash_aliases ] && source ~/.bash_aliases' + create: yes + when: ansible_os_family == 'Archlinux' + +- name: Ensure Debian sources .bash_aliases + ansible.builtin.lineinfile: + path: /root/.bashrc + # regexp: '\.bash_aliases' + line: 'source ~/.bash_aliases' + when: ansible_distribution == 'Debian' # Not Ubuntu, which should already have this + +- name: Copy shell configuration + ansible.builtin.copy: + src: inputrc + dest: "{{ item }}/.inputrc" + loop: + - /etc/skel + - /root + - "{{ ansible_env.HOME }}/" + +- name: Copy bash aliases + ansible.builtin.copy: + src: bash_aliases + dest: "{{ item }}/.bash_aliases" + loop: + - /etc/skel + - /root + - "{{ ansible_env.HOME }}/" + +- name: Add root bashrc shell history configuration + lineinfile: + path: /root/.bashrc + regexp: '^(export )?HISTCONTROL=' + line: 'HISTCONTROL=ignoreboth' + state: present + +- name: Add root bashrc and vim configuration. + lineinfile: + path: /root/.bashrc + regexp: "{{ item.regexp }}" + line: "{{ item.line }}" + state: present + with_items: + - regexp: '^(export )?VISUAL=' + line: 'export VISUAL=vim.basic' + - regexp: '^(export )?EDITOR=' + line: 'export EDITOR=vim.basic' + - regexp: '^(export )?SUDO_EDITOR=' + line: 'export SUDO_EDITOR=vim.basic' + when: ansible_os_family == 'Debian' + +- name: Add root bashrc and vim configuration for RedHat + lineinfile: + path: /root/.bashrc + regexp: "{{ item.regexp }}" + line: "{{ item.line }}" + state: present + with_items: + - regexp: '^(export )?VISUAL=' + line: 'export VISUAL=vim' + - regexp: '^(export )?EDITOR=' + line: 'export EDITOR=vim' + - regexp: '^(export )?SUDO_EDITOR=' + line: 'export SUDO_EDITOR=vim' + when: ansible_os_family != 'Debian' + +- name: Add functional alias to restart a service and show the log + blockinfile: + path: /root/.bashrc + block: | + pheonix() { + systemctl restart $1;journalctl -fu $1 + } \ No newline at end of file diff --git a/tasks/main.yml b/tasks/main.yml index dc42271..92d0816 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -1,74 +1,12 @@ --- -- import_tasks: vim-Debian.yml - when: ansible_os_family == 'Debian' +- name: Show (ansible_distribution)-(ansible_facts.distribution_major_version) + debug: + msg: "{{ ansible_distribution }}-{{ ansible_facts.distribution_major_version }}.yml" -- import_tasks: vim.yml - when: ansible_os_family != 'Debian' - -- name: Copy bash aliases - ansible.builtin.copy: - src: bash_aliases - dest: "{{ item }}/.bash_aliases" - loop: - - /etc/skel - - /root - -- name: Ensure Arch Linux sources bash_aliases - lineinfile: - path: /root/.bashrc - line: '[ -f ~/.bash_aliases ] && source ~/.bash_aliases' - create: yes - when: ansible_os_family == 'Archlinux' - -- name: Copy shell configuration - ansible.builtin.copy: - src: inputrc - dest: "{{ item }}/.inputrc" - loop: - - /etc/skel - - /root - -- name: Add root bashrc shell history configuration - lineinfile: - path: /root/.bashrc - regexp: '^(export )?HISTCONTROL=' - line: 'HISTCONTROL=ignoreboth' - state: present - -- name: Add root bashrc and vim configuration. - lineinfile: - path: /root/.bashrc - regexp: "{{ item.regexp }}" - line: "{{ item.line }}" - state: present - with_items: - - regexp: '^(export )?VISUAL=' - line: 'export VISUAL=vim.basic' - - regexp: '^(export )?EDITOR=' - line: 'export EDITOR=vim.basic' - - regexp: '^(export )?SUDO_EDITOR=' - line: 'export SUDO_EDITOR=vim.basic' - when: ansible_os_family == 'Debian' - -- name: Add root bashrc and vim configuration for RedHat - lineinfile: - path: /root/.bashrc - regexp: "{{ item.regexp }}" - line: "{{ item.line }}" - state: present - with_items: - - regexp: '^(export )?VISUAL=' - line: 'export VISUAL=vim' - - regexp: '^(export )?EDITOR=' - line: 'export EDITOR=vim' - - regexp: '^(export )?SUDO_EDITOR=' - line: 'export SUDO_EDITOR=vim' - when: ansible_os_family != 'Debian' - -- name: Add functional alias to restart a service and show the log - blockinfile: - path: /root/.bashrc - block: | - pheonix() { - systemctl restart $1;journalctl -fu $1 - } \ No newline at end of file +- name: Run tasks based on the OS type, or a default if not found. + include_tasks: "{{ item }}" + with_first_found: + - "{{ ansible_distribution }}-{{ ansible_facts.distribution_major_version }}.yml" + - "{{ ansible_distribution }}.yml" + - "{{ ansible_os_family }}.yml" + - "default.yml"