Last active
February 27, 2024 03:59
-
-
Save MasWag/e6e1d339e3429461293f6eae2216026d to your computer and use it in GitHub Desktop.
Run ansible-playbook with C-c C-c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(require 'leaf) | |
;; Define a customizable variable for the inventory file | |
(defcustom ansible-inventory-file "hosts" | |
"The default inventory file for ansible-playbook commands." | |
:type 'string | |
:group 'user) | |
(defcustom ansible-connection-password-file nil | |
"The file containing the password for the connection." | |
:type 'string | |
:group 'user) | |
(defcustom ansible-become-password-file nil | |
"The file containing the password for become." | |
:type 'string | |
:group 'user) | |
(defcustom ansible-vault-variable-file nil | |
"The file containing the variables encrypted with ansible-vault." | |
:type 'string | |
:group 'user) | |
(leaf ansible | |
:ensure t | |
:init | |
;; Define a customizable variable for the inventory file | |
(defcustom ansible-inventory-file "hosts" | |
"The default inventory file for ansible-playbook commands." | |
:type 'string | |
:group 'user) | |
(defun run-ansible-playbook () | |
"Run ansible-playbook on the current file with a specified inventory file." | |
(interactive) | |
;; Ensure that the buffer is associated with a file | |
(if (buffer-file-name) | |
(let ((command (format "ansible-playbook -i %s %s %s %s %s %s" | |
ansible-inventory-file | |
(if ansible-connection-password-file | |
(mapconcat #'shell-quote-argument | |
(list "--connection-password-file" | |
(file-truename ansible-connection-password-file)) | |
" ") | |
"") | |
(if ansible-become-password-file | |
(mapconcat #'shell-quote-argument | |
(list "--become-password-file" | |
(file-truename ansible-become-password-file)) | |
" ") | |
"") | |
(if ansible-vault-password-file | |
(mapconcat #'shell-quote-argument | |
(list "--vault-password-file" | |
(file-truename ansible-vault-password-file)) | |
" ") | |
"") | |
(if ansible-vault-variable-file | |
(mapconcat #'shell-quote-argument | |
(list "--extra-vars" | |
(concat "@" | |
(file-truename ansible-vault-variable-file))) | |
" ") | |
"") | |
(shell-quote-argument (buffer-file-name))))) | |
;; Use compile to run the command so that output is captured | |
;; in a *compilation* buffer, allowing for error navigation | |
(message (format "Running %s" command)) | |
(compile command)) | |
(message "Buffer is not associated with a file."))) | |
:bind ((:ansible-key-map | |
("C-c C-c" . run-ansible-playbook)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment