ホームページ  >  記事  >  バックエンド開発  >  ansibleでPythonモジュールライブラリとして使用されるメソッドの例

ansibleでPythonモジュールライブラリとして使用されるメソッドの例

高洛峰
高洛峰オリジナル
2017-02-21 10:29:431790ブラウズ

ansible は、完全な解凍と再生を行うソフトウェアである Python パッケージです。クライアントに必要なのは、ssh と Python が必要であり、python-simplejson パッケージがインストールされているだけです。デプロイメントは非常に簡単です。以下の記事では、Pythonモジュールライブラリとしてansibleを利用する方法の例を中心に紹介していますので、困っている方は参考にしてみてください。

はじめに

ansible は、Python に基づいて開発された新しい自動運用保守ツール (puppet、cfengine、chef、func、fabric) の利点を統合してバッチ システムを実現します。構成、バッチ プログラムのデプロイメントやコマンドのバッチ実行などの機能。 Ansible はモジュールに基づいて動作し、バッチでデプロイする機能はありません。実際にバッチ デプロイメントを行うのは、ansible によって実行されるモジュールであり、ansible はフレームワークのみを提供します。

主に以下が含まれます:

(1) 接続プラグイン: 監視対象との通信を担当します。

(2) ホスト インベントリ: 指定された操作のホストは、構成ファイルで監視用に定義されたホストです。
(3)、さまざまなモジュールコアモジュール、コマンドモジュール、カスタムモジュール;


(4)、ログメールの記録などの機能を完了するためのプラグインを使用します。


(5)、タスク実行時の複数のスクリプト実行。 、必要がなければ、ノードで一度に複数のタスクを実行させることができます。

Ansible は、運用保守ツールの中でも非常に優れたツールであり、クライアントをインストールする必要がないため、ニーズに応じて柔軟に設定でき、非常に簡単です。次に、ansible を Python のライブラリ コンポーネントとして独自のスクリプトに記述する必要がある場合があります。今日のスクリプトでは、ansible を Python スクリプトと組み合わせる方法、つまり Python スクリプトで ansible を使用する方法を説明します。 . 段階的に展開していきます。

まず最初の例を見てみましょう:

#!/usr/bin/python 
import ansible.runner
import ansible.playbook
import ansible.inventory
from ansible import callbacks
from ansible import utils
import json
 
# the fastest way to set up the inventory
 
# hosts list
hosts = ["10.11.12.66"]
# set up the inventory, if no group is defined then 'all' group is used by default
example_inventory = ansible.inventory.Inventory(hosts)
 
pm = ansible.runner.Runner(
 module_name = 'command',
 module_args = 'uname -a',
 timeout = 5,
 inventory = example_inventory,
 subset = 'all' # name of the hosts group 
 )
 
out = pm.run()
 
print json.dumps(out, sort_keys=True, indent=4, separators=(',', ': '))

この例は、Python スクリプトで ansible を介してシステム コマンドを実行する方法を示しています。次に、yml ファイルのドッキングを使用した 2 番目の例を見てみましょう。 。

単純な yml ファイルの内容は次のとおりです:

- hosts: sample_group_name
 tasks:
 - name: just an uname
 command: uname -a

Playbook を呼び出す Python スクリプトは次のとおりです:

#!/usr/bin/python 
import ansible.runner
import ansible.playbook
import ansible.inventory
from ansible import callbacks
from ansible import utils
import json
 
### setting up the inventory
 
## first of all, set up a host (or more)
example_host = ansible.inventory.host.Host(
 name = '10.11.12.66',
 port = 22
 )
# with its variables to modify the playbook
example_host.set_variable( 'var', 'foo')
 
## secondly set up the group where the host(s) has to be added
example_group = ansible.inventory.group.Group(
 name = 'sample_group_name'
 )
example_group.add_host(example_host)
 
## the last step is set up the invetory itself
example_inventory = ansible.inventory.Inventory()
example_inventory.add_group(example_group)
example_inventory.subset('sample_group_name')
 
# setting callbacks
stats = callbacks.AggregateStats()
playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)
runner_cb = callbacks.PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY)
 
# creating the playbook instance to run, based on "test.yml" file
pb = ansible.playbook.PlayBook(
 playbook = "test.yml",
 stats = stats,
 callbacks = playbook_cb,
 runner_callbacks = runner_cb,
 inventory = example_inventory,
 check=True
 )
 
# running the playbook
pr = pb.run() 
 
# print the summary of results for each host
print json.dumps(pr, sort_keys=True, indent=4, separators=(',', ': '))

のメソッド例に関するその他の関連記事については、 ansible を Python モジュール ライブラリとして使用する場合は、PHP の中国語 Web サイトに注目してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。