Ansible_クイックスタート

Ansible

1 Ansibleはじめに

Ansibleは、自動化ツールの簡単な操作とメンテナンスで、ちょうどsshプロトコルは、そのシステム管理、自動化されたオーダー実行、展開、および他のタスクに接続することができます。

Ansible機能

ansibleは別のクライアントをインストールする必要はありませんすべてのサービスを開始する必要はありません1、
2、ansibleは完全なオートメーションタスクモジュール内のpythonである
3、オートメーションタスクのためのYAML設定を使用してansible脚本が一目で行われ

Ansible組成構造

  • nsibleは
    Ansibleコマンドツール、ツールの実行コアであり、ワンタイムまたは一時的な動作は、コマンドを実行することにより行われます。
  • Ansibleハンドブック
    タスクスクリプト(タスクも知られているセット)は、タスクスケジューリングが順Ansible、YAML形式で実行Ansibleセットプロファイルを、定義されています。
  • 在庫
    リストAnsible管理ホスト、デフォルトでは、/ etc / ansible / hostsファイルです。
  • モジュール
    Ansibleは、これまでAnsible2.3版、1039個のモジュールの合計をコマンド機能モジュールを実行します。また、カスタムモジュールであってもよいです。
  • プラグインは、
    アドオンモジュール、モジュールの機能を、多くの場合、型コネクタ、プラグ・ループ、可変プラグフィルタインサート、より少ない特徴を有するインサートプラグ。
  • APIが
    呼ばれるサードパーティ製プログラムにアプリケーション・プログラミング・インターフェースを提供します。

ビルドに2環境

環境の準備

IP システム ホスト名 説明
192.168.1.30 CentOS7 ansible ansible管理ノード
192.168.1.31 CentOS7 linux.node01.com 管理対象ノード1
192.168.1.32 CentOS7 linux.node02.com ノード2マネージド
192.168.1.33 CentOS7 linux.node03.com 管理対象ノード3

3 Ansibleインストール

1)コンフィギュレーション・ソースEPEL

[root@ansible ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
[root@ansible ~]# yum clean all
[root@ansible ~]# yum makecache

2)インストールansible

[root@ansible ~]# yum -y install ansible

//查看ansible版本
[root@ansible ~]# ansible --version
ansible 2.8.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Aug  4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]

4 Ansibleインベントリファイル

インベントリ中国のドキュメント

インベントリファイルは通常、認証情報を定義するために使用されているようにSSHログインユーザー名、パスワード、およびキー関連情報として、ホストを管理すること。複数のホストのグループの同時動作は、グループとグループの間の関係は、ホスト目録ファイルによって構成されています。コンフィギュレーションファイルのパス:は/ etc / ansible /ホスト

4.1パスワードベースの接続

[root@ansible ~]# vim /etc/ansible/hosts
# 方法一 主机+端口+密码
[webserver]
192.168.1.31 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456"
192.168.1.32 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456"
192.168.1.33 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456"
192.168.1.36 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass="123456"


# 方法二 主机+端口+密码
[webserver]
192.168.1.3[1:3] ansible_ssh_user=root ansible_ssh_pass="123456"


# 方法二 主机+端口+密码
[webserver]
192.168.1.3[1:3]
[webserver:vars]
ansible_ssh_pass="123456"

秘密鍵に基づいて4.2接続

同時に基づいて、公開鍵と秘密鍵を作成するには、管理アプライアンスの接続が必要に秘密鍵、

1)公開鍵と秘密鍵を生成し、

[root@ansible ~]# ssh-keygen
[root@ansible ~]# for i in {1,2,3,6}; do ssh-copy-id -i 192.168.1.3$i ; done

2)接続を設定

[root@ansible ~]# vim /etc/ansible/hosts
# 方法一 主机+端口+密钥
[webserver]
192.168.1.31:22
192.168.1.32
192.168.1.33
192.168.1.36

# 方法一 别名主机+端口+密钥
[webserver]
node1 ansible_ssh_host=192.168.1.31 ansible_ssh_port=22
node2 ansible_ssh_host=192.168.1.32 ansible_ssh_port=22
node3 ansible_ssh_host=192.168.1.33 ansible_ssh_port=22
node6 ansible_ssh_host=192.168.1.36 ansible_ssh_port=22

4.3ホストグループを使用します

# 主机组变量名+主机+密码
[apache]
192.168.1.36
192.168.1.33
[apache.vars]
ansible_ssh_pass='123456'

# 主机组变量名+主机+密钥
[nginx]
192.168.1.3[1:2]

# 定义多个组,把一个组当另外一个组的组员
[webserver:children]  #webserver组包括两个子组:apache nginx
apache
nginx

4.4仮指定インベントリ

1)最初の編集定義のマスターリスト

[root@ansible ~]# vim /etc/dockers
[dockers]
192.168.1.31 ansible_ssh_pass='123456'
192.168.1.32
192.168.1.33

2)実行順序は、インベントリを指定します

[root@ansible ~]# ansible dockers -m ping -i /etc/dockers -o 
192.168.1.33 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
192.168.1.32 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
192.168.1.31 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}

4.5インベントリが内蔵されたパラメータ

ここに画像を挿入説明

5 Ansibleアドホック

アドホック中国のドキュメント

アドホック-一時的、ansibleでは、すぐに実行する必要がある、とコマンドを保存する必要性を指します。コマンド-それは、単純なコマンドを実行することを意味します。複雑なコマンドの場合は、脚本状態SLS状態ファイルをsaltstackと同様でした。
1)共通のコマンド・パラメータ・

[root@ansible ~]# ansible -h
Usage: ansible <host-pattern> [options]
-a MODULE_ARGS   #模块参数
-C, --check  #检查语法
-f FORKS #并发
--list-hosts #列出主机列表
-m MODULE_NAME #模块名字
-o 使用精简的输出

2)実施例

[root@ansible ~]# ansible webserver -m shell -a 'uptime' -o
192.168.1.36 | CHANGED | rc=0 | (stdout)  13:46:14 up 1 day,  9:20,  4 users,  load average: 0.00, 0.00, 0.00
192.168.1.33 | CHANGED | rc=0 | (stdout)  21:26:33 up 1 day,  8:51,  3 users,  load average: 0.00, 0.01, 0.05
192.168.1.31 | CHANGED | rc=0 | (stdout)  21:26:33 up 1 day,  8:50,  3 users,  load average: 0.00, 0.01, 0.05
192.168.1.32 | CHANGED | rc=0 | (stdout)  21:26:33 up 1 day,  8:59,  3 users,  load average: 0.00, 0.01, 0.05

3)コマンド説明
ここに画像を挿入説明

5.1ホスト・パターン・フォーマット

ターゲットターゲットホストは、ホストグループは、道一致する
マッチホストを

#  一台目标主机
[root@ansible ~]# ansible 192.168.1.31 -m ping

# 多台目标主机
[root@ansible ~]# ansible 192.168.1.31,192.168.1.32 -m ping

# 所有目标主机
[root@ansible ~]# ansible all -m ping

グループマッチ

# 组的配置信息如下:这里定义了一个nginx组和一个apache组
[root@ansible ~]# ansible nginx --list
  hosts (2):
    192.168.1.31
    192.168.1.32
[root@ansible ~]# ansible apache --list
  hosts (3):
    192.168.1.36
    192.168.1.33
    192.168.1.32

# 一个组的所有主机匹配
[root@ansible ~]# ansible apache -m ping

# 匹配apache组中有,但是nginx组中没有的所有主机
[root@ansible ~]# ansible 'apache:!nginx' -m ping -o
192.168.1.36 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
192.168.1.33 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}

# 匹配apache组和nginx组中都有的机器(并集)
[root@ansible ~]# ansible 'apache:&nginx' -m ping -o
192.168.1.32 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}

# 匹配apache组nginx组两个组所有的机器(并集);等于ansible apache,nginx -m ping
[root@ansible ~]# ansible 'apache:nginx' -m ping -o
192.168.1.32 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
192.168.1.31 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
192.168.1.33 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
192.168.1.36 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
公開された102元の記事 ウォン称賛12 ビュー6292

おすすめ

転載: blog.csdn.net/qq_43141726/article/details/104373611