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:
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
19 function ansible_adhoc () {
21 # Run ansible ad-hoc command module on hosts in working topology file.
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
32 INVENTORY_PATH="cloud_inventory"
35 INVENTORY_PATH="lf_inventory"
39 if ! installed sshpass; then
40 die "Please install sshpass!"
43 hosts=($(fgrep host "${WORKING_TOPOLOGY}" | cut -d ":" -f 2)) || {
44 die "Failed to read hosts from working topology!"
46 pushd "${CSIT_DIR}"/fdio.infra.ansible || die "Pushd failed!"
47 export ANSIBLE_HOST_KEY_CHECKING=False
48 export ANSIBLE_STDOUT_CALLBACK=yaml
49 export ANSIBLE_PIPELINING=true
51 --vault-password-file=vault_pass \
52 --extra-vars '@vault.yml' \
53 --inventory inventories/$INVENTORY_PATH/hosts site.yaml \
54 --limit "$(echo ${hosts[@]//\"})" \
56 --args \"$(echo $@)\" || die "Failed to run ansible on host!"
57 popd || die "Popd failed!"
60 function ansible_playbook () {
62 # Run ansible playbook on hosts in working topology file. Ansible scope is
63 # determined by tags passed as parameters to this function.
66 # - ${WORKING_TOPOLOGY} - Reserved working topology.
67 # - ${CSIT_DIR} - CSIT main directory, where ansible playbooks are located.
68 # - ${FLAVOR} - Node flavor string, see common.sh
74 INVENTORY_PATH="cloud_inventory"
77 INVENTORY_PATH="lf_inventory"
81 if ! installed sshpass; then
82 die "Please install sshpass!"
85 hosts=($(fgrep host "${WORKING_TOPOLOGY}" | cut -d ":" -f 2)) || {
86 die "Failed to read hosts from working topology!"
88 pushd "${CSIT_DIR}"/fdio.infra.ansible || die "Pushd failed!"
89 export ANSIBLE_HOST_KEY_CHECKING=False
90 export ANSIBLE_STDOUT_CALLBACK=yaml
91 export ANSIBLE_PIPELINING=true
93 --vault-password-file=vault_pass \
94 --extra-vars '@vault.yml' \
95 --inventory inventories/$INVENTORY_PATH/hosts site.yaml \
96 --limit "$(echo ${hosts[@]//\"})" \
97 --tags "$(echo $@)" || die "Failed to run ansible on host!"
98 popd || die "Popd failed!"
102 function installed () {
104 # Check if the given utility is installed. Fail if not installed.
107 # - ${1} - Utility to check.
108 # Returns (implicitly):
109 # - 0 - If command is installed.
110 # - 1 - If command is not installed.