X-Git-Url: https://gerrit.fd.io/r/gitweb?p=csit.git;a=blobdiff_plain;f=resources%2Flibraries%2Fbash%2Ffunction%2Fansible.sh;h=1263412dd51ca6acab9eecce2bf563ca6c9c365f;hp=5bf122e4b0c85d256f0a6c7627a1521065333808;hb=8e712c8f59fbc0d5488a452540d1acfb3162b942;hpb=36d56bdb7f9f394047e2df3f29bf47db877b649c diff --git a/resources/libraries/bash/function/ansible.sh b/resources/libraries/bash/function/ansible.sh index 5bf122e4b0..1263412dd5 100644 --- a/resources/libraries/bash/function/ansible.sh +++ b/resources/libraries/bash/function/ansible.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash -# Copyright (c) 2019 Cisco and/or its affiliates. +# Copyright (c) 2020 Cisco and/or its affiliates. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at: @@ -16,10 +16,9 @@ set -exuo pipefail -function ansible_hosts () { +function ansible_adhoc () { - # Run ansible playbook on hosts in working topology file. Ansible scope is - # determined by tags passed as parameters to this function. + # Run ansible ad-hoc command module on hosts in working topology file. # # Variable read: # - ${WORKING_TOPOLOGY} - Reserved working topology. @@ -28,20 +27,47 @@ function ansible_hosts () { set -exuo pipefail if ! installed sshpass; then - sudo apt-get update -y || die "apt-get update failed!" - sudo apt-get install -y sshpass || die "Install sshpass failed!" + die "Please install sshpass!" fi - if ! installed ansible-playbook; then - # TODO: Consider moving to requirements.txt? - pip install ansible==2.7.8 || die "Install ansible via PIP failed!" + hosts=($(fgrep host "${WORKING_TOPOLOGY}" | cut -d ":" -f 2)) || { + die "Failed to read hosts from working topology!" + } + pushd "${TOOLS_DIR}"/testbed-setup/ansible || die "Pushd failed!" + ANSIBLE_STDOUT_CALLBACK=yaml \ + ANSIBLE_PIPELINING=true \ + ansible \ + --vault-password-file=vault_pass \ + --extra-vars '@vault.yml' \ + --inventory inventories/lf_inventory/hosts site.yaml \ + --limit "$(echo ${hosts[@]//\"})" \ + --module-name shell \ + --args \"$(echo $@)\" || die "Failed to run ansible on host!" + popd || die "Popd failed!" +} + +function ansible_playbook () { + + # Run ansible playbook on hosts in working topology file. Ansible scope is + # determined by tags passed as parameters to this function. + # + # Variable read: + # - ${WORKING_TOPOLOGY} - Reserved working topology. + # - ${TOOLS_DIR} - CSIT tools directory, where testbed-setup is located. + + set -exuo pipefail + + if ! installed sshpass; then + die "Please install sshpass!" fi hosts=($(fgrep host "${WORKING_TOPOLOGY}" | cut -d ":" -f 2)) || { die "Failed to read hosts from working topology!" } pushd "${TOOLS_DIR}"/testbed-setup/ansible || die "Pushd failed!" - ANSIBLE_STDOUT_CALLBACK=yaml ansible-playbook \ + ANSIBLE_STDOUT_CALLBACK=yaml \ + ANSIBLE_PIPELINING=true \ + ansible-playbook \ --vault-password-file=vault_pass \ --extra-vars '@vault.yml' \ --inventory inventories/lf_inventory/hosts site.yaml \ @@ -50,6 +76,7 @@ function ansible_hosts () { popd || die "Popd failed!" } + function installed () { # Check if the given utility is installed. Fail if not installed.