FreezeJ' Blog

Ansible源码解析--cli

2021-04-01

Ansible命令行调用

参考文章:https://anyisalin.github.io/2017/06/16/ansible-code-reading-1/

ansible相关命令其实都是通过软连接指向了ansible命令本身,但是它巧妙的通过os.path.basename(sys.argv[0]).split('-')获取要调用的模块(如playbook、doc),通过__import__动态引入模块。

cd /usr/bin/ && ll ansible* | grep  "\-> ansible"    
lrwxrwxrwx 1 root root     7 Nov 12 14:10 ansible-config -> ansible
lrwxrwxrwx 1 root root     7 Nov 12 14:10 ansible-console-2.6 -> ansible
lrwxrwxrwx 1 root root     7 Nov 12 14:10 ansible-doc-2.6 -> ansible
lrwxrwxrwx 1 root root     7 Nov 12 14:10 ansible-galaxy-2.6 -> ansible
lrwxrwxrwx 1 root root     7 Nov 12 14:10 ansible-inventory -> ansible
lrwxrwxrwx 1 root root     7 Nov 12 14:10 ansible-playbook-2.6 -> ansible
lrwxrwxrwx 1 root root     7 Nov 12 14:10 ansible-pull-2.6 -> ansible
lrwxrwxrwx 1 root root     7 Nov 12 14:10 ansible-vault-2.6 -> ansible

调用结构

图片来自:https://anyisalin.github.io/2017/06/16/ansible-code-reading-1/

ansible-cli调用