feat(bootstrap): Add ability to load external NODE_FLAVOR
[csit.git] / resources / libraries / bash / function / ansible.sh
1 #!/usr/bin/env bash
2
3 # Copyright (c) 2024 Cisco and/or its affiliates.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 set -exuo pipefail
17
18
19 function ansible_adhoc () {
20
21     # Run ansible ad-hoc command module on hosts in working topology file.
22     #
23     # Variable read:
24     # - ${WORKING_TOPOLOGY} - Reserved working topology.
25     # - ${CSIT_DIR} - CSIT main directory, where ansible playbooks are located.
26     # - ${FLAVOR} - Node flavor string, see common.sh
27
28     set -exuo pipefail
29
30     case "$FLAVOR" in
31         "aws" | "c6in" | "c6gn" | "c7gn")
32             INVENTORY_PATH="cloud_inventory"
33             ;;
34         "x-"*)
35             INVENTORY_PATH="external_inventory"
36             ;;
37         *)
38             INVENTORY_PATH="lf_inventory"
39             ;;
40     esac
41
42     if ! installed sshpass; then
43         die "Please install sshpass!"
44     fi
45
46     hosts=($(fgrep host "${WORKING_TOPOLOGY}" | cut -d ":" -f 2)) || {
47         die "Failed to read hosts from working topology!"
48     }
49     pushd "${CSIT_DIR}"/fdio.infra.ansible || die "Pushd failed!"
50     export ANSIBLE_HOST_KEY_CHECKING=False
51     export ANSIBLE_STDOUT_CALLBACK=yaml
52     export ANSIBLE_PIPELINING=true
53     ansible-playbook \
54         --vault-password-file=vault_pass \
55         --extra-vars '@vault.yml' \
56         --inventory inventories/$INVENTORY_PATH/hosts site.yaml \
57         --limit "$(echo ${hosts[@]//\"})" \
58         --module-name shell \
59         --args \"$(echo $@)\" || die "Failed to run ansible on host!"
60     popd || die "Popd failed!"
61 }
62
63 function ansible_playbook () {
64
65     # Run ansible playbook on hosts in working topology file. Ansible scope is
66     # determined by tags passed as parameters to this function.
67     #
68     # Variable read:
69     # - ${WORKING_TOPOLOGY} - Reserved working topology.
70     # - ${CSIT_DIR} - CSIT main directory, where ansible playbooks are located.
71     # - ${FLAVOR} - Node flavor string, see common.sh
72
73     set -exuo pipefail
74
75     case "$FLAVOR" in
76         "aws" | "c6in" | "c6gn" | "c7gn")
77             INVENTORY_PATH="cloud_inventory"
78             ;;
79         "x-"*)
80             INVENTORY_PATH="external_inventory"
81             ;;
82         *)
83             INVENTORY_PATH="lf_inventory"
84             ;;
85     esac
86
87     if ! installed sshpass; then
88         die "Please install sshpass!"
89     fi
90
91     hosts=($(fgrep host "${WORKING_TOPOLOGY}" | cut -d ":" -f 2)) || {
92         die "Failed to read hosts from working topology!"
93     }
94     pushd "${CSIT_DIR}"/fdio.infra.ansible || die "Pushd failed!"
95     export ANSIBLE_HOST_KEY_CHECKING=False
96     export ANSIBLE_STDOUT_CALLBACK=yaml
97     export ANSIBLE_PIPELINING=true
98     ansible-playbook \
99         --vault-password-file=vault_pass \
100         --extra-vars '@vault.yml' \
101         --inventory inventories/$INVENTORY_PATH/hosts site.yaml \
102         --limit "$(echo ${hosts[@]//\"})" \
103         --tags "$(echo $@)" || die "Failed to run ansible on host!"
104     popd || die "Popd failed!"
105 }
106
107
108 function installed () {
109
110     # Check if the given utility is installed. Fail if not installed.
111     #
112     # Arguments:
113     # - ${1} - Utility to check.
114     # Returns (implicitly):
115     # - 0 - If command is installed.
116     # - 1 - If command is not installed.
117
118     set -exuo pipefail
119
120     command -v "${1}"
121 }