Skip to content

Instantly share code, notes, and snippets.

@jimyhuang
Created July 10, 2015 14:20
Show Gist options
  • Save jimyhuang/f837ea01e7a427f9be23 to your computer and use it in GitHub Desktop.
Save jimyhuang/f837ea01e7a427f9be23 to your computer and use it in GitHub Desktop.
Ansible-playbook bash complete for specific directory
#!/bin/env bash
_ansible_playbook() {
local current_word=${COMP_WORDS[COMP_CWORD]}
local previous_word=${COMP_WORDS[COMP_CWORD - 1]}
if [[ "$current_word" == -* ]]; then
_ansible_complete_options "$current_word"
elif [[ "$current_word" == @* ]]; then
_ansible_target "$current_word"
else
_ansible_playbook_yml "$current_word"
fi
}
complete -F _ansible_playbook ansible-playbook
# Compute completion for the generics options
_ansible_complete_options() {
local current_word=$1
local options="-h --help -e --extra-vars -t --tags -v --verbose"
COMPREPLY=( $( compgen -W "$options" -- "$current_word" ) )
}
_ansible_playbook_yml(){
local word=`echo $1 | sed -e 's/\/etc\/ansible\/ansible-docker\/playbooks\///g'`
COMPREPLY=( $(
cd /etc/ansible/ansible-docker/playbooks && \
compgen -f -X '!*.yml' -- "$word" | awk '{print "/etc/ansible/ansible-docker/playbooks/"$word}'
) )
}
_ansible_target(){
local word=`echo $1 | sed -e 's/^@//g'`
word=`echo $word | sed -e 's/\/etc\/ansible\/target\///g'`
COMPREPLY=( $(
cd /etc/ansible/target && \
compgen -o plusdirs -f -X '!*' -- "$word" | awk '{print "@/etc/ansible/target/"$1}'
) )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment