Infra: Do not strict check keys in Ansible
[csit.git] / resources / libraries / bash / function / ansible.sh
1 #!/usr/bin/env bash
2
3 # Copyright (c) 2021 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     # - ${TOOLS_DIR} - CSIT tools directory, where testbed-setup is located.
26
27     set -exuo pipefail
28
29     if ! installed sshpass; then
30         die "Please install sshpass!"
31     fi
32
33     hosts=($(fgrep host "${WORKING_TOPOLOGY}" | cut -d ":" -f 2)) || {
34         die "Failed to read hosts from working topology!"
35     }
36     pushd "${TOOLS_DIR}"/testbed-setup/ansible || die "Pushd failed!"
37     export ANSIBLE_HOST_KEY_CHECKING=False
38     export ANSIBLE_STDOUT_CALLBACK=yaml
39     export ANSIBLE_PIPELINING=true
40     ansible-playbook \
41         --vault-password-file=vault_pass \
42         --extra-vars '@vault.yml' \
43         --inventory inventories/lf_inventory/hosts site.yaml \
44         --limit "$(echo ${hosts[@]//\"})" \
45         --module-name shell \
46         --args \"$(echo $@)\" || die "Failed to run ansible on host!"
47     popd || die "Popd failed!"
48 }
49
50 function ansible_playbook () {
51
52     # Run ansible playbook on hosts in working topology file. Ansible scope is
53     # determined by tags passed as parameters to this function.
54     #
55     # Variable read:
56     # - ${WORKING_TOPOLOGY} - Reserved working topology.
57     # - ${TOOLS_DIR} - CSIT tools directory, where testbed-setup is located.
58
59     set -exuo pipefail
60
61     if ! installed sshpass; then
62         die "Please install sshpass!"
63     fi
64
65     hosts=($(fgrep host "${WORKING_TOPOLOGY}" | cut -d ":" -f 2)) || {
66         die "Failed to read hosts from working topology!"
67     }
68     pushd "${TOOLS_DIR}"/testbed-setup/ansible || die "Pushd failed!"
69     export ANSIBLE_HOST_KEY_CHECKING=False
70     export ANSIBLE_STDOUT_CALLBACK=yaml
71     export ANSIBLE_PIPELINING=true
72     ansible-playbook \
73         --vault-password-file=vault_pass \
74         --extra-vars '@vault.yml' \
75         --inventory inventories/lf_inventory/hosts site.yaml \
76         --limit "$(echo ${hosts[@]//\"})" \
77         --tags "$(echo $@)" || die "Failed to run ansible on host!"
78     popd || die "Popd failed!"
79 }
80
81
82 function installed () {
83
84     # Check if the given utility is installed. Fail if not installed.
85     #
86     # Arguments:
87     # - ${1} - Utility to check.
88     # Returns (implicitly):
89     # - 0 - If command is installed.
90     # - 1 - If command is not installed.
91
92     set -exuo pipefail
93
94     command -v "${1}"
95 }