DEV Community

Cover image for [AIAnsible]In the era of AIOps, learn to debug Ansible tasks with AIAnsbile in just 10 minutes.
A Jun
A Jun

Posted on • Updated on

[AIAnsible]In the era of AIOps, learn to debug Ansible tasks with AIAnsbile in just 10 minutes.

AIAnsible:

https://www.youtube.com/watch?v=kEFZLp8IEmQ

Debugging Ansible with AI for automatic commenting, error analysis, and providing suggestions for improvement.

  git clone https://github.com/sunnycloudy/aiansible.git
  cd aiansible
  pip install .  #=> will generate dir: ~/.aiansible_plugin
Enter fullscreen mode Exit fullscreen mode

QA:

qq group: 937374915

ai prompt example:

If you want to use English:

export AIANSIBLE_LANG=EN
Enter fullscreen mode Exit fullscreen mode

Image description

or use ":cn"and":en" to switch language:

Image description

install and start using aiansible:

  • (1) download and install
  git clone https://github.com/sunnycloudy/aiansible.git
  cd aiansible
  pip install .  #=> will generate dir: ~/.aiansible_plugin
Enter fullscreen mode Exit fullscreen mode
  • (2) install dependents:
pip install  -r requirements.txt
Enter fullscreen mode Exit fullscreen mode
  • (3) create a:debug.cfg
[defaults]
callback_plugins = ~/.aiansible_plugin
callbacks_enabled = aiansible.py
Enter fullscreen mode Exit fullscreen mode
  • (4) Configure AI parameters:

If not set, the AI prompt feature will not be available, but it will not affect normal debugging.

Use a configuration file (recommended)

create config.yml: ~/.aiansible_plugin/config.yml

openai:
  api_key: https://api.moonshot.cn/v1   #or any other compatible OpenAI API address
  api_url: your_openai_api_url_here #or any other compatible OpenAI key
  model: moonshot-v1-8k  #or any other compatible OpenAI model name
Enter fullscreen mode Exit fullscreen mode

Or use environment variables to configure AI

export OPENAI_API_URL=https://api.moonshot.cn/v1   #or any other compatible OpenAI API address
export OPENAI_API_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxx #or any other compatible OpenAI key
export OPENAI_MODEL=moonshot-v1-8k #or any other compatible OpenAI model name
Enter fullscreen mode Exit fullscreen mode
  • (5) Run:
# Configure the plugin in debug.cfg
export ANSIBLE_CONFIG=./debug.cfg
ansible-playbook xxx_playbook.yml
Enter fullscreen mode Exit fullscreen mode

demo:

debug kubespray:

# find
kubespray/ansible.cfg
Enter fullscreen mode Exit fullscreen mode

edit kubespray default ansible.cfg:

[ssh_connection]
pipelining=True
ansible_ssh_args = -o ControlMaster=auto -o ControlPersist=30m -o ConnectionAttempts=100 -o UserKnownHostsFile=/dev/null
#control_path = ~/.ssh/ansible-%%r@%%h:%%p
[defaults]
# https://github.com/ansible/ansible/issues/56930 (to ignore group names with - and .)
force_valid_group_names = ignore

host_key_checking=False
gathering = smart
fact_caching = jsonfile
fact_caching_connection = /tmp
fact_caching_timeout = 86400
stdout_callback = default
display_skipped_hosts = no
library = ./library
# callbacks_enabled = profile_tasks,ara_default      #<= comment it   (・ω・)ノ
callback_plugins = ~/.aiansible_plugin               #<=  new line (。・ω・。)ノ
callbacks_enabled = aiansible.py                     #<=  new line ( ・ω・ )ノ

roles_path = roles:$VIRTUAL_ENV/usr/local/share/kubespray/roles:$VIRTUAL_ENV/usr/local/share/ansible/roles:/usr/share/kubespray/roles
deprecation_warnings=False
inventory_ignore_extensions = ~, .orig, .bak, .ini, .cfg, .retry, .pyc, .pyo, .creds, .gpg
[inventory]
ignore_patterns = artifacts, credentials

Enter fullscreen mode Exit fullscreen mode

run command:

If not set, the AI prompt feature will not be available, but it will not affect normal debugging.

Configure AI parameters:

Use a configuration file (recommended)

create config.yml: ~/.aiansible_plugin/config.yml

openai:
  api_key: https://api.moonshot.cn/v1   #or any other compatible OpenAI API address
  api_url: your_openai_api_url_here #or any other compatible OpenAI key
  model: moonshot-v1-8k  #or any other compatible OpenAI model name

Enter fullscreen mode Exit fullscreen mode

Or use environment variables to configure AI

export OPENAI_API_URL=https://api.moonshot.cn/v1   #or any other compatible OpenAI API address
export OPENAI_API_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxx #or any other compatible OpenAI key
export OPENAI_MODEL=moonshot-v1-8k #or any other compatible OpenAI model name
Enter fullscreen mode Exit fullscreen mode

Image description

edit mode:support emacs-mode or vim-mode, default is emacs-mode

export AIANSIBLE_EDITMODE=vi
#或
export AIANSIBLE_EDITMODE=emacs
Enter fullscreen mode Exit fullscreen mode

basic usage:

:cn            Set the language to Chinese
:en            Set the language to English
i              Annotate the code of the currently executing task
ir             Annotate the code of the currently executing task, analyze the results, and provide suggestions for improvement
ask            Please answer questions based on the current Ansible task
n    next      Run the next task
m              Do not stay at the same task again immediately
c    continue  Continue running until the next breakpoint
b              Create a breakpoint
p              View created breakpoints
d    delete    Delete a breakpoint
bt             View which tasks have been run
code           View the code of the currently running task
v              Open the corresponding file with VSCode
a    arg       View all arguments, or a single argument (assuming the task has not been skipped)
?    help      View the usage instructions
exit           Exit
Enter fullscreen mode Exit fullscreen mode

check the result of current ansible task:

Aiansible(CN) => result._result
{'msg': 'Check roles/kubespray-defaults/defaults/main.yml', '_ansible_verbose_always': True, '_ansible_no_log': False, 'changed': False}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)