Skip to content

Commit 0461ec9

Browse files
author
Dmitriy Rabotyagov
committed
Add autocomplete script for playbooks
Since we've moved all our playbooks to collection, and we know where collection path is for sure, we can come up with a convenience autocomplete script, which will scan required paths and complete with a correct FQCN for playbooks. Change-Id: Id934f59ffd536ed3dc6f1e6d0cd767bfcf0ef293
1 parent 5adb8c8 commit 0461ec9

3 files changed

Lines changed: 106 additions & 0 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
features:
3+
- |
4+
Added a bash auto-completion script which will assist with running
5+
openstack-ansible commands. It is placed as
6+
``/etc/bash_completion.d/openstack-ansible``, so please make sure
7+
your .bashrc is configured to load completion scripts from there.
8+
As of today it can help with completing playbook names, which are part
9+
of collections, Ansible native flags and hosts in case of ``--limit``
10+
flag is used.

scripts/bash-completion

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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

scripts/bootstrap-ansible.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ sed -i "s|OSA_CLONE_DIR|${OSA_CLONE_DIR}|g" /usr/local/bin/openstack-ansible
184184
# Mark the current OSA version in the wrapper, so we don't need to compute it every time.
185185
sed -i "s|CURRENT_OSA_VERSION|${CURRENT_OSA_VERSION}|g" /usr/local/bin/openstack-ansible
186186

187+
# Create an auto-completion script
188+
cp -v scripts/bash-completion /etc/bash_completion.d/openstack-ansible
189+
187190
# Ensure wrapper tool is executable
188191
chmod +x /usr/local/bin/openstack-ansible
189192

0 commit comments

Comments
 (0)