由於小弟畢設作一個簡單的自動化運維繫統,因此入了ansible的坑,如今想把畢設過程當中一些關鍵技術的核心代碼作下記錄,也但願經過此平臺與你們相互交流學習,共同進步。前端
python
//一共有4個文件ios
[root@localhost playbook]# treejson
.bash
├── config.yml運維
├── exec.pyide
├── hosts學習
└── secrets.ymlui
// 1)放置Inventory的文件命令行
[root@localhost playbook]# cat hosts
[ios_device]
192.168.60.121
// 2)一些在playbook要用到的憑證參數變量
[root@localhost playbook]# cat secrets.yml
---
creds:
username: cisco
password: cisco
auth_pass: cisco
// 3) 在Python代碼中要引用的playbook
[root@localhost playbook]# cat config.yml
---
- hosts: ios_device
gather_facts: no
connection: local
tasks:
- name: OBTAIN LOGIN CREDENTIALS
include_vars: secrets.yml
- name: DEFINE PROVIDER
set_fact:
provider:
host: "{{ inventory_hostname }}"
username: "{{ creds['username'] }}"
password: "{{ creds['password'] }}"
auth_pass: "{{ creds['auth_pass'] }}"
- name: show run
ios_config:
provider: "{{ provider }}"
authorize: yes
commands:
- hostname cisco
- ip access-list extended test
- permit ip host 192.1.1.1 any log
- exit
- int f1/0
- ip address 1.1.1.2 255.255.255.0
- no shutdown
- ip route 0.0.0.0 0.0.0.0 192.168.60.2
- do write
// 4) 須要執行的Python 代碼
[root@localhost playbook]# cat exec.py
#!/usr/bin/env python # coding=utf-8 import json import shutil from collections import namedtuple from ansible.parsing.dataloader import DataLoader from ansible.vars.manager import VariableManager from ansible.inventory.manager import InventoryManager from ansible.executor.playbook_executor import PlaybookExecutor from ansible.executor.task_queue_manager import TaskQueueManager from ansible.plugins.callback import CallbackBase import ansible.constants as C class ResultCallback(CallbackBase): def __init__(self, *args, **kwargs): super(ResultCallback, self).__init__(*args, **kwargs) self.task_ok={} def v2_runner_on_ok(self, result, *args, **kwargs): self.task_ok[result._host.get_name()]=result results_callback = ResultCallback() #InventoryManager類 loader = DataLoader() #讀取yaml文件 inventory = InventoryManager(loader=loader, sources=['./hosts'])#這裏的路徑要正確 #variableManager類 variable_manager = VariableManager(loader=loader,inventory=inventory) #option 執行選項 Options = namedtuple('Optoins', ['connection', 'remote_user', 'ask_sudo_pass', 'verbosity', 'ack_pass', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check', 'listhosts', 'syntax', 'listtags', 'listtasks', 'sudo_user', 'sudo', 'diff']) options = Options(connection='smart', remote_user=None, ack_pass=None, sudo_user=None, forks=5, sudo=None, ask_sudo_pass=False, verbosity=5, module_path=None, become=None, become_method=None, become_user=None, check=False, diff=False, listhosts=None, listtasks=None, listtags=None, syntax=None) passwords=dict() #playbook的路徑要正確 playbook=PlaybookExecutor(playbooks=['config.yml'], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords) #playbook.run() playbook._tqm._stdout_callback=results_callback playbook.run() results_raw={'ok':{}} for host,result in results_callback.task_ok.items(): results_raw['ok'][host]=result._result print results_raw
——————————————————————
若是在命令行模式使用過ansible-playbook命令的話,能夠很清楚的瞭解上訴代碼編寫過程 1.設置Inventory -> 2.設置選項參數 -> 3.設置引用劇本
class ResultCallback(CallbackBase) 是爲了返回的結果是json格式,方便前端調用。 運行結果: //記得給.py文件於可執行權限:chmod +x exec.py [root@localhost playbook]# ./exec.py { 'ok': { u '192.168.60.121': { '_ansible_parsed': True, u 'src': u '/root/.ansible/tmp/ansible-tmp-1547048002.11-60187723412487/source', u 'md5sum': u '28e89502d2362427b660aaf7ce762c37', u 'group': u 'root', u 'uid': 0, '_ansible_delegated_vars': { 'ansible_delegated_host': u 'localhost', 'ansible_host': u 'localhost' }, u 'dest': u './192.168.60.121.txt', u 'checksum': u '2006c7082ad427e6ebb0d61c2aefd2859fa51b35', u 'changed': True, '_ansible_no_log': False, 'failed': False, u 'state': u 'file', u 'gid': 0, u 'mode': u '0644', u 'invocation': { u 'module_args': { u 'directory_mode': None, u 'force': True, u 'remote_src': None, u 'owner': None, u 'follow': False, u 'local_follow': None, u 'group': None, u 'unsafe_writes': None, u 'setype': None, u 'content': None, u 'serole': None, u 'dest': u './192.168.60.121.txt', u 'selevel': None, u 'original_basename': u 'tmpoMsJHe', u 'regexp': None, u 'validate': None, u 'src': u '/root/.ansible/tmp/ansible-tmp-1547048002.11-60187723412487/source', u 'seuser': None, u 'delimiter': None, u 'mode': None, u 'attributes': None, u 'backup': False } }, u 'owner': u 'root', 'diff': [], u 'size': 378 } } } -------------結束-------------