Weitere ähnliche Inhalte
Ähnlich wie Ansibleではじめるサーバー・ネットワークの自動化(2018/08/22) (20)
Ansibleではじめるサーバー・ネットワークの自動化(2018/08/22)
- 3. 自己紹介
3
名前 横地 晃 @akira6592
所属 株式会社エーピーコミュニケーションズ
担当 ネットワークの設計構築
最近の出来事 会議室の名前が Kingyo になった
- 8. 【参考】Infrastructure as Code(IaC)
構成定義ファイル
バージョン管理
継続的テストシステム、プロセス
一斉変更ではなく小刻みな変更
8
Infrastructure as Code - クラウドにおけるサーバ管理の原則とプラクティス
https://www.oreilly.co.jp/books/9784873117966/
ソフトウェア開発で培われてきたプラクティスをインフラに適用
【例】
主な Ansible の出番
- 12. 動作環境
12
pip install ansible
Python環境上で動作(ただしWindowsにはインストール不可)
インストール方法例
https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html
他、yum、dnf、apt-get 等でも可
例: junos_config モジュールであれば、Pythonパッケージ「ncclient」もインストール必要
https://docs.ansible.com/ansible/latest/modules/junos_config_module.html
※利用するモジュール(後述)によっては追加インストールも必要
(モジュールのドキュメントの「Requirements」を参照)
Linux: ○
Win: ×
Linux: ○
Win: ○
コントロール ターゲット
- 14. 主な構成要素
(1) インベントリ
(2) モジュール
(3) Playbook
(4) ansible.cfg
14
(1)インベントリ
(ホスト情報)
(3)Playbook
(構成定義)(2)モジュール
(機能)
(4)ansible.cfg
(環境設定)
host1
host2
host3 どこに
なにを
対象機器
操作
利用
参照
- 16. モジュールとは
Ansibleの機能単位
Playbook 等で必要なモジュール(とオプション)を指定
16
システム
• Linux
• Windows
クラウド等
• AWS
• Azure
• GCP
• Dcoker
• OpenStack
DB
• PostgreSQL
• MySQL
• MS SQL Server
監視
• Zabbix
• Sensu
• nagios
通知
• Mail
• Slack
• syslog
NW機器
• Cisco IOS
• Juniper Junos
• Ariststa EOS
【対応プラットフォーム例】
などなど、バージョンアップで増えていく
- 23. Playbook
23
---
- hosts: web
become: yes
tasks:
- name: httpd package
yum:
name: httpd
state: present
- name: deploy index.html
template:
src: index.html.j2
dest: /var/www/html/index.html
- name: httpd service
service:
name: httpd
state: started
enabled: yes
vars:
v_name: world
対象ホストを指定。インベントリファイル内で定義した
グループ「web」を指定。
ログイン先で sudo する
yum モジュールを利用
パッケージ「httpd」がインストールされた状態にする
template モジュールを利用
テンプレートファイル「index.html.j2」を利用して生成し
た内容を「/var/www/html/index.html」にデプロイ
service モジュールを利用
サービス「httpd」を起動状態にする。
変数「v_name」の内容を「world」として定義
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
利用例1
- 25. Playbook実行
25
[vagrant@centos7 demo]$ ansible-playbook -i inventory web.yml
PLAY [web] *******************************************************
TASK [Gathering Facts] **********************************************
ok: [172.16.0.10]
TASK [httpd package] ***********************************************
changed: [172.16.0.10]
TASK [deploy index.html] ********************************************
changed: [172.16.0.10]
TASK [httpd service] ************************************************
changed: [172.16.0.10]
PLAY RECAP ******************************************************
172.16.0.10 : ok=4 changed=3 unreachable=0 failed=0
[vagrant@centos7 demo]$
-i オプションでインベントリファイルを指定 Playbookファイル名
グループ「web」を対象とした処理の開始
基本情報(ファクト)の収集
httpd のインストール
(実際にインストールされたためchangedステータス)
Index.html ファイルのデプロイ
(実際にデプロイしためchangedステータス)
httpdのサービスの起動
(実際に開始したためchangedステータス)
実行結果のサマリ
利用例1
- 30. Playbook
30
---
- hosts: junos
gather_facts: no
connection: netconf
tasks:
- name: ntp config test
junos_config:
lines:
- set system ntp server 10.0.1.123
- name: config backup
junos_command:
commands:
- show configuration
register: result
- name: save config to file
copy:
content: "{{ result.stdout[0] }}"
dest: "show_config_{{ inventory_hostname }}.txt"
対象ホストを指定。インベントリファイル内で定義した
グループ「juons」を指定。
通常のSSHではなくNETCONFで接続する
junos_config モジュールを利用してコンフィグを投入
投入したいコンフィグを指定
(参照先NTPサーバー 10.0.1.123を設定)
junos_config モジュールを利用してshowコマンドを実行
copy モジュールを利用
変数「result」内の標準出力の0番目の内容を、
ファイル名「show_config_172.16.0.1.txt」で保存する
実行したいshowコマンドを指定
(コンフィグファイルの表示)
実行した結果を変数「result」に代入
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
利用例2
- 31. Playbook実行
31
[vagrant@centos7 demo]$ ansible-playbook -i inventory net.yml
PLAY [junos] ****************************************************
TASK [ntp config test]
***************************************************************
changed: [172.16.0.1]
TASK [config backup]
***************************************************************
ok: [172.16.0.1]
TASK [save config to file]
***************************************************************
changed: [172.16.0.1]
PLAY RECAP
***************************************************************
172.16.0.1 : ok=3 changed=2 unreachable=0 failed=0
[vagrant@centos7 demo]$
-i オプションでインベントリファイルを指定 Playbookファイル名
グループ「junos」を対象とした処理の開始
コンフィグの投入
(実際に投入されたためchangedステータス)
show configuration コマンドの実行
コンフィグのファイル保存の実行
実行結果のサマリ
利用例2
- 32. 確認
32
無事にコンフィグ投入とコンフィグのファイル保存ができた
root@vsrx1> show configuration system ntp | display set
set system ntp server 10.0.1.123
NW機器側
[vagrant@centos7 demo]$ cat show_config_172.16.0.1.txt
## Last changed: 2018-06-20 18:20:40 UTC
version 12.1X47-D15.4;
system {
host-name vsrx1;
root-authentication {
encrypted-password "$1$nq.....";
ssh-rsa "ssh-rsa AAAAB3.....";
}
login {
user vagrant {
uid 2000;
....
ntp {
server 10.0.1.123;
}
}
interfaces {
ge-0/0/0 {
.....
Ansibleホスト側
利用例2
- 36. 【付録】参考資料
36
公式ドキュメント
トップ
https://docs.ansible.com/
Getting Started
https://docs.ansible.com/ansible/latest/user_guide/intro_getting_started.html
モジュールインデックス
http://docs.ansible.com/ansible/modules_by_category.html
ハンズオンコンテンツ
https://github.com/ansible/lightbulb
Ansible by Red Hat
https://www.redhat.com/ja/explore/ansible
書籍
Ansible実践ガイド 第2版
https://book.impress.co.jp/books/1117101100
Ansible徹底入門
https://www.shoeisha.co.jp/book/detail/9784798149943
おそらく公式ドキュメントの中で
各モジュールの説明ページを見る時間が
一番長い(何ができる?どう使う?)