[ANSIBLE] Add 'bash-completion' role 25/126325/1
authorBartek Grzybowski <b.grzybowski@partner.samsung.com>
Fri, 17 Dec 2021 10:17:55 +0000 (11:17 +0100)
committerBartek Grzybowski <b.grzybowski@partner.samsung.com>
Fri, 17 Dec 2021 10:17:55 +0000 (11:17 +0100)
Role installs bash-completion package and generates the
shell completion code for binary given as a role parameter

Change-Id: I95af7b7a16b0dec1dd7841f1db7afdb3738fe6bc
Issue-ID: OOM-2902
Signed-off-by: Bartek Grzybowski <b.grzybowski@partner.samsung.com>
ansible/roles/bash-completion/.yamllint [new file with mode: 0644]
ansible/roles/bash-completion/README.md [new file with mode: 0644]
ansible/roles/bash-completion/defaults/main.yml [new file with mode: 0644]
ansible/roles/bash-completion/molecule/default/converge.yml [new file with mode: 0644]
ansible/roles/bash-completion/molecule/default/molecule.yml [new file with mode: 0644]
ansible/roles/bash-completion/molecule/default/prepare.yml [new file with mode: 0644]
ansible/roles/bash-completion/molecule/default/tests/test_default.py [new file with mode: 0644]
ansible/roles/bash-completion/tasks/main.yml [new file with mode: 0644]

diff --git a/ansible/roles/bash-completion/.yamllint b/ansible/roles/bash-completion/.yamllint
new file mode 100644 (file)
index 0000000..c5ae64b
--- /dev/null
@@ -0,0 +1,12 @@
+---
+extends: default
+
+rules:
+  braces:
+    max-spaces-inside: 1
+    level: error
+  brackets:
+    max-spaces-inside: 1
+    level: error
+  line-length: disable
+  truthy: disable
diff --git a/ansible/roles/bash-completion/README.md b/ansible/roles/bash-completion/README.md
new file mode 100644 (file)
index 0000000..d96e916
--- /dev/null
@@ -0,0 +1,15 @@
+Role to install bash-completion
+===============================
+
+Role that installs the bash-completion package and generates the completion code for binary which name is passed via role parameter.
+
+Requirements
+------------
+
+For the role to operate properly it is expected that the binary for which the completion code is generated supports "completion bash" option.
+
+Role Variables
+--------------
+
+- completion\_bin (role's parameter) - name of the binary for which the completion code will be generated
+
diff --git a/ansible/roles/bash-completion/defaults/main.yml b/ansible/roles/bash-completion/defaults/main.yml
new file mode 100644 (file)
index 0000000..cc95f20
--- /dev/null
@@ -0,0 +1,3 @@
+---
+completion_dir: /etc/bash_completion.d
+completion_package: bash-completion
diff --git a/ansible/roles/bash-completion/molecule/default/converge.yml b/ansible/roles/bash-completion/molecule/default/converge.yml
new file mode 100644 (file)
index 0000000..a58fee0
--- /dev/null
@@ -0,0 +1,7 @@
+---
+- name: Converge
+  hosts: all
+  tasks:
+    - name: "Include bash-completion"
+      include_role:
+        name: "bash-completion"
diff --git a/ansible/roles/bash-completion/molecule/default/molecule.yml b/ansible/roles/bash-completion/molecule/default/molecule.yml
new file mode 100644 (file)
index 0000000..d30a084
--- /dev/null
@@ -0,0 +1,25 @@
+---
+dependency:
+  name: galaxy
+driver:
+  name: docker
+lint: |
+  set -e
+  yamllint .
+  ansible-lint .
+  flake8
+platforms:
+  - name: bash-completion
+    image: centos:7
+provisioner:
+  name: ansible
+  env:
+    ANSIBLE_ROLES_PATH: ../../../../test/roles
+    ANSIBLE_LIBRARY: ../../../../library
+  inventory:
+    group_vars:
+      all:
+        kubectl_install: true
+        completion_bin: kubectl
+verifier:
+  name: testinfra
diff --git a/ansible/roles/bash-completion/molecule/default/prepare.yml b/ansible/roles/bash-completion/molecule/default/prepare.yml
new file mode 100644 (file)
index 0000000..5acc39c
--- /dev/null
@@ -0,0 +1,7 @@
+---
+- name: Prepare
+  hosts: all
+  roles:
+    # This role is played in prepare stage only to install kubectl which will be
+    # used as a binary to test the bash-completion role
+    - prepare-kubectl
diff --git a/ansible/roles/bash-completion/molecule/default/tests/test_default.py b/ansible/roles/bash-completion/molecule/default/tests/test_default.py
new file mode 100644 (file)
index 0000000..5a66ee2
--- /dev/null
@@ -0,0 +1,7 @@
+def test_bash_completion(host):
+    assert host.package("bash-completion").is_installed
+
+
+def test_bash_completion_kubectl(host):
+    f = host.file('/etc/bash_completion.d/kubectl')
+    assert f.exists
diff --git a/ansible/roles/bash-completion/tasks/main.yml b/ansible/roles/bash-completion/tasks/main.yml
new file mode 100644 (file)
index 0000000..ef402e5
--- /dev/null
@@ -0,0 +1,22 @@
+---
+- name: Install completion for the bash shell
+  package:
+    name: "{{ completion_package }}"
+    state: present
+
+- name: Ensure bash completion dir exists
+  file:
+    path: "{{ completion_dir }}"
+    state: directory
+    mode: 0755
+
+- name: Generate shell autocompletion code for {{ completion_bin }}
+  command: "{{ completion_bin }} completion bash"
+  register: bash_completion
+  changed_when: false
+
+- name: Install bash autocompletion code for {{ completion_bin }}
+  copy:
+    content: "{{ bash_completion.stdout }}"
+    dest: "{{ completion_dir }}/{{ completion_bin }}"
+    mode: 0644