Running ansible-playbook from inside a debugger

I have multiple installs of ansible (brew, pip and git source) on my mac.

I recently needed to debug some things and found it easy to do, with one exception.  I had to hack a line in ansible-playbook to put the order of ansible source repo before the other installations in the python path.
 Here is the result.
pycharm_ansible_playbook

 

 Here is a diff of my changes from the official ansible-playbook code.

<code>
[~/vcp/git/ansible (devel)]
git diff

diff –git a/bin/ansible-playbook b/bin/ansible-playbook

index 2a692a2..baa2e78 100755

— a/bin/ansible-playbook

+++ b/bin/ansible-playbook

@@ -39,7 +39,8 @@ import stat

 local_module_path = os.path.abspath(

     os.path.join(os.path.dirname(__file__), ‘..’, ‘lib’)

 )

-sys.path.append(local_module_path)

+sys.path = [local_module_path]  + sys.path

 </code>

Then to handle the fact that you don’t have a commandline to input args, I pass them in as follows:

<code>

 import ansible.playbook

 import ansible.constants as C

@@ -312,6 +313,11 @@ if __name__ == “__main__”:

     display(” “, log_only=True)

     display(” “.join(sys.argv), log_only=True)

     display(” “, log_only=True)

+    commandline = “””ansible-playbook -i /path/to/inventory/hosts

+                                  /path/to/site.yml

+                                    –private-key=/path/to/my/key

+                                  “””

+    sys.argv = commandline.split()

     try:

         sys.exit(main(sys.argv[1:]))

     except errors.AnsibleError, e:

</code>

Steps

1.  Install ansible from source. You can find instructions here for a variety of platforms.

2.  Create a python project in your IDE. (I used pycharm).  Set the python interpreter.  Import folder – the root of the ansible tree.  Either File -> Open or Preferences -> Project -> Project Structure -> Add content root.

3.  Modify the source for ansible-playbook as per the diff above.

4.  Click the tab for ansible-playbook source file.  Then click the debug icon or Run -> Debug.

 

Troubleshooting.

You may need to paste the following lines into your ~/.bash_profile, and restart pycharm so that it runs on the appropriate version of python.

export ANSIBLE_HOME=$HOME/path/to/ansible
export PATH=”${ANSIBLE_HOME}/bin:${PATH}”
export PYTHONPATH=”${ANSIBLE_HOME}/lib:${PYTHONPATH}”
export MANPATH=”${ANSIBLEHOME}/docs/man:${MANPATH}

If your playbook needs sduo password -K, i’m not sure how pycharm will handle this.  Hang or pop-up box is my guess.

One thought on “Running ansible-playbook from inside a debugger

  1. Pingback: Running ansible-playbook from inside a debugger | My Blog

Leave a comment