Use with_first_found to selectively run tasks.

This commit is contained in:
Brian Lee 2023-11-08 10:15:51 -08:00
parent c261677c0b
commit 6770baf7f9
7 changed files with 314 additions and 73 deletions

View File

@ -7,7 +7,7 @@ Tested on:
* Archlinux
* Debian 10, 11, 12
* Ubuntu 20.04, 22.04
* !RedHat 7.9
* RedHat 7.9
## Requirements

56
tasks/ArchLinux.yml Normal file
View File

@ -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
}

55
tasks/Debian.yml Normal file
View File

@ -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
}

58
tasks/RedHat-7.yml Normal file
View File

@ -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
# }

50
tasks/Ubuntu.yml Normal file
View File

@ -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
}

84
tasks/default.yml Normal file
View File

@ -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
}

View File

@ -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
}
- 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"