Más contenido relacionado
La actualidad más candente (20)
Similar a Ansible 2.10 と Collection (20)
Ansible 2.10 と Collection
- 1. Ansible 2.10 と Collection
株式会社エーピーコミュニケーションズ
技術開発部 自動化グループ
横地 晃
1
2020/10/07 AP Tech Fest
内部構造が変わった Ansible のおはなし
- 2. はじめに
先日、Ansible 2.10.0 がリリースされました。
Collection によるモジュールの配布が本格的となり、
標準モジュール は 70程度に減りました。
Ansible 2.10 と Collection の関係や、ポータビリティなどをご紹介します。
2
2.9 2.10
- 4. 内容
Ansible 2.10 の基本
Ansible 2.10 の導入
Collection の操作
今までの Playbook は動くの?
気になるアップデート
まとめ
4
- 6. Collection の本格運用により標準モジュールが激減
Ansible 2.9 までは 3000 以上あった標準モジュールは 2.10 では約 70 に
他のモジュールは「Collection」による配布に移行
ポータビリティはある程度確保されているため、過度な心配は不要(後述)
6
基本機能
標準モジュール
基本機能
標準モジュール
移行された
Collection 群
移行
※ 標準モジュール: https://docs.ansible.com/ansible/latest/collections/ansible/builtin/index.html#module-plugins
※ Ansible Galaxy や Automation Hub では、2.9 での標準モジュールから移行された Collection だけなく、独自のものも管理されている
2.9 2.10
Ansible Galaxy や
Automation Hub
- 7. 基本機能と標準モジュールは ansible-base に
基本機能と標準モジュールは ansible-base と呼ばれる
「pip install ansible」は ansible-base と 、移行された Collection のセット
7
2.9 ansible-base 2.10
基本機能
標準モジュール
基本機能
標準モジュール
ansible 2.10
移行された
Collection 群
Ansible Galaxy や
Automation Hub
※ ansible-base リリース履歴: 2020/08/14 2.10.0、2020/09/15 2.10.1、2020/10/06 2.10.2
※ ansible リリース履歴: 2020/09/23 2.10.0
- 8. Collection
Collection
そもそも Collection とは
モジュールやプラグインなどをまとめて配布する単位
Ansible Galaxy や Automation Hub 経由で配布
リポジトリは https://github.com/ansible-collections 配下や独自管理
hoge.fuga のような形式の名前空間が設けられる
含まれるモジュール類は hoge.fuga.piyo という形式(FQCN)
8
例: ansible.netcommon Collection
モジュール例 プラグイン例
ansible.necommon.cli_command
ansible.necommon.cli_config
ansible.necommon.network
ansible.necommon.ipaddr
※ FQCN: Fully Qualified Collection Name
※ Collection を取り巻く概要: https://github.com/ansible-collections/overview
モジュール
プラグイン
:
- 9. Ansible Galaxy による Collection の配布
例: ansible.netcommon Collection
https://galaxy.ansible.com/ansible/netcommon
9
- 10. なんで ansible-base と collection に分かれたの?(私見含む)
ansible 本体と Collection のリリースサイクルを分類
本体の安定性と新機能の取捨選択を両立
「新しいモジュールを使いたいけど ansible のバージョンアップは
影響が大きそうでできない・・」といった悩みがなくなりそう
10
ansible-base 2.10
基本機能
標準モジュール
必要な Collection の
インストール・アップデート
Collection 群
※ FQCN: Fully Qualified Collection Name
※ Collection を取り巻く概要: https://github.com/ansible-collections/overview
※ 参考 Collections signal major shift in Ansible ecosystem https://www.jeffgeerling.com/blog/2020/collections-signal-major-shift-ansible-ecosystem
- 12. インストール
ansible-base
ansible 基本機能と標準モジュール
ansible
ansible-base と 移行された Collection のセット
12
pip install ansible-base
pip install ansible
基本機能
標準モジュール
移行した
Collection 群
今まで通り
※ pip install ansible でインストールした場合、セットの Collection は <python環境>/lib/python3.X/site-packages/ansible_collections/ 配下にインストールされる
- 13. アップグレード
ansible 2.9 から ansible 2.10 へのアップグレード
一度アインインストールしてから再インストール
直接アップグレードできないのは ansible 2.10.0 時点の Known Issue
13
pip uninstall ansible
pip install ansible
venv などで別環境に
したほうがよさそう
- 16. Collection のインストール
Ansible Galaxy や Automation Hub で対象の Collection を見つけてインストール
デフォルトでは ~/.ansible/collections/ansible_collections/ コレクション名/ にインストール
-p オプションや、ansible.cfg の [defaults] 内 collections_path の指定で変更可能
16
ansible-galaxy collection install コレクション名
※ Installing collections: https://docs.ansible.com/ansible/latest/user_guide/collections_using.html#installing-collections
インストールコマンド
が載っている
- 17. Collection のアップグレード
現状、アップグレード専用コマンドがないため、
-f オプション で強制的に最新をインストール
17
ansible-galaxy collection install コレクション名 -f
※ Installing collections: https://docs.ansible.com/ansible/latest/user_guide/collections_using.html#installing-collections
ポイント
- 18. Collection の確認
インストールした Collection をコマンドで確認可能
デフォルトでは ~/.ansible/collections/ansible_collections/ 配下を検索
つまり pip install ansible でセットでインストールされた Collection がある
<python環境>/lib/python3.X/site-packages/ansible_collections/ は検索しない。
ansible 2.10.0 時点の Known Issue。明示的に検索先を指定する場合は -p オプションなどを利用する
18
ansible-galaxy collection list
※ Listing collections: https://docs.ansible.com/ansible/latest/user_guide/collections_using.html#listing-collections
※ Known Issues: https://docs.ansible.com/ansible/latest/porting_guides/porting_guide_2.10.html#known-issues
(a210) [sakana@centos7 ~]$ ansible-galaxy collection list
# /home/sakana/.ansible/collections/ansible_collections
Collection Version
-------------------- -------
ansible.netcommon 1.1.2
cisco.nxos 1.1.0
netbox.netbox 1.0.0
実行例
- 19. 補足1: Ansible Tower で利用する Collection を自動インストールする
プロジェクトの collections/requirements.yml に
必要な Collection 名を指定しておくと自動でインストールされる
19
※ Collections Support: https://docs.ansible.com/ansible-tower/latest/html/userguide/projects.html#collections-support
※ requirements.yml の書き方例 https://docs.ansible.com/ansible/latest/user_guide/collections_using.html#install-multiple-collections-with-a-requirements-file
---
collections:
- ansible.netcommon
例
- 20. 補足2: Collection と Red Hat サポートなど
Ansible Supported Collections, Versioning, and Release Strategy
https://access.redhat.com/articles/4993781
Ansible Automation Platform Certified Content
https://access.redhat.com/articles/3642632
20
- 22. サンプル Playbook(準備)
環境
python 3.6.8、新規 venv に paramiko インストール
pip install ansible でインストール
Playbook
Ansible 2.9 向けに書いた右記のもの
22
---
- hosts: ios
gather_facts: false
tasks:
- name: show ip route
ios_command:
commands:
- show ip route
register: resgister_show_ip_route
- name: debug route
debug:
msg: "{{ resgister_show_ip_route.stdout_lines }}"
標準ではなくなったモジュール
デモ
- 23. サンプル Playbook(実行)
Playbook の修正なしに動いた
23
(a2100) [root@centos7 stumble]# ansible-playbook -i inventory.ini ios_show.yml
PLAY [ios] ***********************************************************************************
TASK [show ip route] *************************************************************************
ok: [rt01]
TASK [debug route] ***************************************************************************
ok: [rt01] => {
"msg": [
[
"Codes: L - local, C - connected, S - static, R - RIP, M - mobile, B - BGP",
...(略)...
"C 10.255.255.1/32 is directly connected, Loopback0",
"O 10.255.255.2/32 [110/2] via 10.0.0.2, 04:59:05, GigabitEthernet0/3",
" 192.168.1.0/24 is variably subnetted, 2 subnets, 2 masks",
"C 192.168.1.0/24 is directly connected, GigabitEthernet0/0",
"L 192.168.1.11/32 is directly connected, GigabitEthernet0/0"
]
]
}
PLAY RECAP ***********************************************************************************
rt01 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
デモ
- 24. 動く理由1: Collection がセットだから
「pip install ansible」で、移行された Collection もセットでインストールされる
そのため 2.9 までの標準モジュールが 2.10 でも利用できる
ただし、移行後も開発が進むうえで仕様変更や廃止もあり得る
24
ansible-base 2.10
基本機能
標準モジュール
ansible 2.10
移行された
collection 群
※ セットの Collection の定義 (ansible 2.10.0 の例): https://github.com/ansible-community/ansible-build-data/blob/main/2.10/ansible-2.10.0.deps
- 25. 動く理由2: リダイレクト機能があるから
コレクション名を含まないモジュール名の指定(例 ios_command)でも
cisco.ios.ios_command のような FQCN に解決するリダイレクト機能がある
ansible-base としての機能
もともと標準でなかったモジュールの定義は含まれない
25※ リダイレクトの定義 (ansible-base 2.10.1 の例): https://github.com/ansible/ansible/blob/v2.10.1/lib/ansible/config/ansible_builtin_runtime.yml
plugin_routing:
# ...(略)...
modules:
# ...(略)...
ios_command:
redirect: cisco.ios.ios_command
ios_static_route:
redirect: cisco.ios.ios_static_route
# ...(略)...
単に ios_command と指定すれば
cisco.ios.ios_command と
解釈してくれる
- 26. とはいえ Porting Guide はいつも通り要チェック
バージョンごとに移行時の注意点などが記載されている
Ansible-base 2.10 Porting Guide
https://docs.ansible.com/ansible/latest/porting_guides/porting_guide_base_2.10.html
Ansible 2.10 Porting Guide
https://docs.ansible.com/ansible/latest/porting_guides/porting_guide_2.10.html
26
- 27. タスクごとの FQCN 指定が推奨
27
引用元: What's new with Ansible Content Collections
https://www.ansible.com/resources/webinars-training/whats-new-with-ansible-content-collections-webinar
FQCN
- 29. 地味に便利
タスクのタイムアウトが設定可能に
接続時間+処理時間のタイムアウトを指定、デフォルトは0(タイムアウトなし)
https://docs.ansible.com/ansible/latest/reference_appendices/config.html#task-timeout
ディレクトリやパスの結合に便利な path_join フィルター追加
python の os.path.join 相当、ディレクトリ名の後の / ありなしを気にしなくていい
https://tekunabe.hatenablog.jp/entry/2020/09/17/ansible_path_join
regexp_replace フィルターがマルチラインサポート
https://github.com/ansible/ansible/issues/61985
map や selectattr などのあとの | list が不要に
https://github.com/ansible/ansible/pull/68014
29※ ansible-base としての changelog: https://github.com/ansible/ansible/blob/stable-2.10/changelogs/CHANGELOG-v2.10.rst
- 30. ちょっと注意
CONDITIONAL_BARE_VARS のデフォルトが False に変更
assert モジュールや when で 変数名のみを指定してる場合は注意
2.9で [DEPRECATION WARNING]: evaluating 'hogevar' as a bare variable のような警告があったら特に
True/False が反転することも
https://twitter.com/akira6592/status/1294869956891275265
https://docs.ansible.com/ansible/latest/reference_appendices/config.html#conditional-bare-vars
hash_behaviour が Deprecated 扱い
代わりに combine フィルターを利用
https://docs.ansible.com/ansible/devel/reference_appendices/config.html#default-hash-
behaviour
30※ ansible-base としての changelog: https://github.com/ansible/ansible/blob/stable-2.10/changelogs/CHANGELOG-v2.10.rst
- 32. まとめ
ansible 2.10 から Collection の本格運用が開始
ansible-base = 基本機能 + 標準モジュール
ansible = ansible-base + 移行された Collection
Collection 移行に伴う影響は、過度に心配する必要はなさそう
Collection のインストールセットやリダイレクトによりポータビリティは確保
ゆくゆくは FQCN 表記にするのがよさそう
その他の機能追加も多々
32
- 33. 参考
公式
Ansible Collections Overview
https://github.com/ansible-collections/overview
FAQ: Where did all the modules go?
https://docs.ansible.com/ansible/latest/reference_appendices/faq.html#where-did-all-the-modules-go
changelog
https://github.com/ansible/ansible/blob/stable-2.10/changelogs/CHANGELOG-v2.10.rst
Ansible-base 2.10 Porting Guide
https://docs.ansible.com/ansible/latest/porting_guides/porting_guide_base_2.10.html
Ansible 2.10 Porting Guide
https://docs.ansible.com/ansible/latest/porting_guides/porting_guide_2.10.html
個人ブログ
[Ansible] モジュールの指定などを FQCN 表記に移行する手順
https://tekunabe.hatenablog.jp/entry/2020/09/19/ansible_fqcn
[Ansible] role 単位で collections を指定する
https://tekunabe.hatenablog.jp/entry/2020/09/20/ansible_role_collections
33