My personal dotfiles
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

_ansible-playbook 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #compdef ansible-playbook
  2. # ------------------------------------------------------------------------------
  3. # Copyright (c) 2011 Github zsh-users - http://github.com/zsh-users
  4. # All rights reserved.
  5. #
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions are met:
  8. # * Redistributions of source code must retain the above copyright
  9. # notice, this list of conditions and the following disclaimer.
  10. # * Redistributions in binary form must reproduce the above copyright
  11. # notice, this list of conditions and the following disclaimer in the
  12. # documentation and/or other materials provided with the distribution.
  13. # * Neither the name of the zsh-users nor the
  14. # names of its contributors may be used to endorse or promote products
  15. # derived from this software without specific prior written permission.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  18. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. # DISCLAIMED. IN NO EVENT SHALL ZSH-USERS BE LIABLE FOR ANY
  21. # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. # ------------------------------------------------------------------------------
  28. # Description
  29. # -----------
  30. #
  31. # Completion script for ansible-playbook v2.0.0.2 (http://ansible.org)
  32. #
  33. # ------------------------------------------------------------------------------
  34. # Authors
  35. # -------
  36. #
  37. # * Romain Bossart (https://github.com/bosr)
  38. # * Adam Stevko (https://github.com/xen0l)
  39. #
  40. # ------------------------------------------------------------------------------
  41. #
  42. # Needs either ANSIBLE_HOSTS or /etc/ansible/hosts on linux
  43. # (or /usr/local/etc/ansible/hosts on OSX)
  44. #
  45. # Note 1: the following gist (https://gist.github.com/15ed54a438a36d67fd99.git)
  46. # has some files to help improve the hostfile shell parsing
  47. #
  48. # Note 2: I tried to use `_arguments --`, but the output of `ansible --help`
  49. # is not parsed entirely correctly, and anyway no modules or host would available.
  50. #
  51. # ansible-playbook zsh completion
  52. #
  53. __host_file_location () {
  54. # find the location of the host file:
  55. # 1. check $ANSIBLE_HOSTS
  56. # 2. else check /etc/ansible/hosts or /usr/local/etc/...
  57. # (depending on platform)
  58. #
  59. [[ "$OSTYPE" == darwin* ]] && FALLBACK="/usr/local/etc/ansible/hosts"
  60. [[ "$OSTYPE" == linux* ]] && FALLBACK="/etc/ansible/hosts"
  61. HOST_FILE=${ANSIBLE_HOSTS:=${FALLBACK}}
  62. [[ -f ${HOST_FILE} ]] || HOST_FILE=/dev/null
  63. echo ${HOST_FILE}
  64. }
  65. __ll_group_list () {
  66. # parses the ini hostfile for groups only: [...]
  67. HOST_FILE=$(__host_file_location)
  68. local -a group_list
  69. group_list=$(command \
  70. cat ${HOST_FILE} \
  71. | awk '$1 ~ /^\[.*\]$/ && !/=/ && !/:vars/ \
  72. { gsub(/[\[\]]/, "", $1); gsub(/:children/, "", $1) ; print $1 }' \
  73. | uniq )
  74. echo ${group_list}
  75. }
  76. __host_list ()
  77. {
  78. # parses the ini hostfile for hosts only
  79. # but then has to remove all group occurrences
  80. HOST_FILE=$(__host_file_location)
  81. # this will also contain groups if they are referenced in other groups
  82. local -a mixed_host_list
  83. mixed_host_list=$(command \
  84. cat ${HOST_FILE} \
  85. | awk 'NF && $1 !~ /[\[:=]/ { print $1 }' \
  86. | sort | uniq)
  87. # compute set difference h1 - h2
  88. local -a h1 h2 host_list
  89. h1=${mixed_host_list}
  90. h2=$(__ll_group_list)
  91. host_list=($(command \
  92. sort <(echo $h1) <(echo $h2) <(echo $h2) \
  93. | uniq -u \
  94. | paste -s -d ' ' - )
  95. )
  96. _wanted application expl 'hosts' compadd ${host_list}
  97. # method that delegates to ansible (slow)
  98. # _wanted application expl 'hosts' compadd $(command ansible \
  99. # all --list-hosts\
  100. # 2>/dev/null)
  101. }
  102. __group_list ()
  103. {
  104. gl=($(command echo $(__ll_group_list) | paste -s -d ' ' - )) # 'a\nb\nc' -> (a b c)
  105. _wanted application2 expl 'groups' compadd $gl
  106. }
  107. _ansible-playbook ()
  108. {
  109. local curcontext="$curcontext" state line
  110. typeset -A opt_args
  111. _arguments -C -W \
  112. "1:playbook yml file:_files -g '*.yml'"\
  113. '--ask-become-pass[ask for privilege escalation password]'\
  114. "(-k --ask-pass)"{-k,--ask-pass}"[ask for connection password]"\
  115. '--ask-su-pass[ask for su password (deprecated, use become)]'\
  116. "(-K --ask-sudo-pass)"{-K,--ask-sudo-pass}"[ask for sudo password (deprecated, use become)]"\
  117. '--ask-vault-pass[ask for vault password]'\
  118. "(-b --become)"{-b,--become}"[run operations with become (nopasswd implied)]"\
  119. '--become-method[privilege escalation method to use (default=sudo)]:method:(sudo su pbrun pfexec runas doas)'\
  120. '--become-user[run operations as this user (default=root)]:user:(USER)'\
  121. "(-C --check)"{-C,--check}"[don't make any changes]"\
  122. "(-c --connection)"{-c,--connection}"[CONNECTION connection type to use (default=smart)]:connection type:(smart ssh local chroot)"\
  123. "(-D --diff)"{-D,--diff}"[when changing (small files and templates, show the diff in those. Works great with --check)]"\
  124. "(-e --extra-vars)"{-e,--extra-vars}"[EXTRA_VARS set additional variables as key=value or YAML/JSON]:extra vars:(EXTRA_VARS)"\
  125. '--flush-cache[clear the fact cache]'\
  126. '--force-handlers[run handlers even if a task fails]'\
  127. "(-f --forks)"{-f,--forks}"[FORKS number of parallel processes to use (default=5)]:forks:(5)"\
  128. "(-h --help)"{-h,--help}"[help message]"\
  129. "(-i --inventory-file)"{-i,--inventory-file}"[INVENTORY specify inventory host file]:inventory file:_files"\
  130. "(-l --limit)"{-l,--limit}"[SUBSET further limit selected hosts to an additional pattern]:subset pattern:->pattern"\
  131. '--list-hosts[outputs a list of matching hosts. Does not execute anything else]'\
  132. '--list-tags[list all available tags]'\
  133. '--list-tasks[list all tasks that would be executed]'\
  134. "(-M --module-path)"{-M,--module-path}"[MODULE_PATH specify path to module library (default=None)]:module path:_files -/"\
  135. '--new-vault-password-file[new vault password file for rekey]:new vault password file:_files'\
  136. '--output[output file name for encrypt or decrypt; use - for stdout]:output file:_files'\
  137. '--private-key[PRIVATE_KEY_FILE use this file to authenticate the connection]:private key file:_files'\
  138. '--scp-extra-args[specify extra arguments to pass to scp only]'\
  139. '--sftp-extra-args[specify extra arguments to pass to sftp only]'\
  140. "--skip-tags[SKIP_TAGS only run plays and tasks whose tags do not match these values]:skip tags:(SKIP_TAGS)"\
  141. '--ssh-common-args[specify common arguments to pass to sftp/scp/ssh]'\
  142. '--ssh-extra-args[specify extra arguments to pass to ssh only]'\
  143. "--start-at-task[START_AT start the playbook at the task matching this name]:name:(TASK_NAME)"\
  144. '--step[one-step-at-a-time: confirm each task before running]'\
  145. "(-S --su)"{-S,--su}"[run operations with su (deprecated, use become)]"\
  146. "(-R --su-user)"{-R,--su-user}"[SU_USER run operations with su as this user (default=root) (deprecated, use become)]:su user:(root)"\
  147. "(-s --sudo)"{-s,--sudo}"[run operations with sudo (nopasswd) (deprecated, use become)]"\
  148. "(-U --sudo-user)"{-U,--sudo-user}"[SUDO_USER desired sudo user (default=root) (deprecated, use become)]:su user:(root)"\
  149. '--syntax-check[perform a syntax check on the playbook, but do not execute it]'\
  150. "(-t --tags)"{-t,--tags}"[TAGS only run plays and tasks gagged with these values]:task tags:(TAGS)"\
  151. "(-T --timeout)"{-T,--timeout}"[TIMEOUT override the SSH timeout (s) (default=10)]:ssh timeout:(10)"\
  152. "(-u --user)"{-u,--user}"[REMOTE_USER connect as this user (default=${USER})]:connect as user:(${USER})"\
  153. "--vault-password-file[VAULT_PASSWORD_FILE vault password file]:vault password file:_files"\
  154. "(-v --verbose)"{-v,--verbose}"[verbose mode (-vvv for more, -vvvv to enable connection debugging)]"\
  155. "--version[show program's version number and exit]"\
  156. case $state in
  157. pattern)
  158. _arguments '*:feature:__host_list'
  159. _arguments '*:feature:__group_list'
  160. ;;
  161. esac
  162. }
  163. _ansible-playbook "$@"
  164. # Local Variables:
  165. # mode: Shell-Script
  166. # sh-indentation: 2
  167. # indent-tabs-mode: nil
  168. # sh-basic-offset: 2
  169. # End:
  170. # vim: ft=zsh sw=2 ts=2 et