|
| 1 | +# Bash autocomplete script for `openstack-ansible` command |
| 2 | + |
| 3 | +source /usr/local/bin/openstack-ansible.rc |
| 4 | +COLLECTION_PATH="${ANSIBLE_COLLECTIONS_PATH:-/etc/ansible}/ansible_collections/" |
| 5 | + |
| 6 | + |
| 7 | +_ansible_hosts_inventory(){ |
| 8 | + local hosts current_time cache_creation cache_age cache_lifetime cache_path |
| 9 | + |
| 10 | + cache_lifetime=86400 |
| 11 | + cache_path="${OSA_CONFIG_DIR}/ansible_facts/openstack-ansible-hosts-completion" |
| 12 | + |
| 13 | + if [ -f ${cache_path} ]; then |
| 14 | + cache_creation=$(stat --printf="%Y" ${cache_path}) |
| 15 | + cache_age=$(($(date +%s) - cache_creation)) |
| 16 | + |
| 17 | + if [[ $cache_age -gt $cache_lifetime ]]; then |
| 18 | + rm ${cache_path} |
| 19 | + hosts=$(ansible all --list-hosts 2>/dev/null | sed '1d' | awk '{ print $1 }' | tee ${cache_path}) |
| 20 | + else |
| 21 | + hosts=$(cat ${cache_path}) |
| 22 | + fi |
| 23 | + else |
| 24 | + cache_path_dirname=$(dirname ${cache_path}) |
| 25 | + if [ ! -d ${cache_path_dirname} ]; then |
| 26 | + mkdir -p ${cache_path_dirname} |
| 27 | + fi |
| 28 | + hosts=$(ansible all --list-hosts 2>/dev/null | sed '1d' | awk '{ print $1 }' | tee ${cache_path}) |
| 29 | + fi |
| 30 | + echo "${hosts}" |
| 31 | +} |
| 32 | + |
| 33 | + |
| 34 | +_normalize_collection_playbook_paths(){ |
| 35 | + local converted_paths=() |
| 36 | + for path in "$@"; do |
| 37 | + # Remove leading directory path completely |
| 38 | + path=${path##$COLLECTION_PATH} |
| 39 | + |
| 40 | + # Remove "playbooks" folder |
| 41 | + path=${path//playbooks\//} |
| 42 | + |
| 43 | + # Remove ".yml" extension (if present) |
| 44 | + path=${path%.yml} |
| 45 | + |
| 46 | + # Replace slashes with dots |
| 47 | + path=${path//\//.} |
| 48 | + |
| 49 | + # Append the converted path |
| 50 | + converted_paths+=("$path") |
| 51 | + done |
| 52 | + echo "${converted_paths[@]}" |
| 53 | +} |
| 54 | + |
| 55 | + |
| 56 | +_openstack_ansible() { |
| 57 | + local cur prev opts short_opts completions playbooks |
| 58 | + COMPREPLY=() |
| 59 | + # Get the current word and the previous word |
| 60 | + cur="${COMP_WORDS[COMP_CWORD]}" |
| 61 | + prev="${COMP_WORDS[COMP_CWORD-1]}" |
| 62 | + opts="--ask-become-pass --ask-pass --ask-vault-pass |
| 63 | + --ask-vault-password |
| 64 | + --become --become-method --become-user |
| 65 | + --become-password-file --connection-password-file |
| 66 | + --check --connection --diff --extra-vars |
| 67 | + --flush-cache --force-handlers --forks --help |
| 68 | + --inventory --limit --list-hosts |
| 69 | + --list-tags --list-tasks --module-path |
| 70 | + --private-key --skip-tags --start-at-task |
| 71 | + --step --syntax-check |
| 72 | + --scp-extra-args --sftp-extra-args |
| 73 | + --ssh-common-args --ssh-extra-args |
| 74 | + --tags --timeout |
| 75 | + --user --vault-id --vault-password-file |
| 76 | + --verbose --version" |
| 77 | + short_opts="-b -C -c -D -e -f -h -i -J -K -k -l -M -T -t -u -v" |
| 78 | + |
| 79 | + if [[ "$cur" == -* ]]; then |
| 80 | + completions=$(echo ${short_opts} ${opts}) |
| 81 | + elif [[ "$cur" == --* ]]; then |
| 82 | + completions=$(echo ${opts}) |
| 83 | + elif [[ "$prev" == "-l" ]] || [[ "$prev" == "--limit" ]]; then |
| 84 | + completions=$(_ansible_hosts_inventory | grep -i "${cur}") |
| 85 | + else |
| 86 | + playbooks=$(find ${COLLECTION_PATH} -type f -name "*.yml" | grep playbooks) |
| 87 | + completions=$(_normalize_collection_playbook_paths ${playbooks[@]} | grep -i "${cur}") |
| 88 | + fi |
| 89 | + COMPREPLY=($(compgen -W '${completions}' -- ${cur})) |
| 90 | +} |
| 91 | + |
| 92 | + |
| 93 | +complete -F _openstack_ansible openstack-ansible |
0 commit comments