てくなべ (tekunabe)

ansible / network automation / 学習メモ

[Ansible] 「つまずき Ansible 【Part17】ansible 2.10 まわり」ふりかえり

はじめに

2020/09/26 に、YouTube Live で「つまずき Ansible 【Part17】Ansible 2.10 まわり」という配信をしました。

実際に作業しながらエラーと戦って進めるシリーズです。

tekunabe.connpass.com

今回は、2020/09/22 にリリースされたの Asnible 2.10.0(not ansible-base)をとりまく仕組み(collectionなど)について復習しました。

やったことや、わかったことをふりかえります。

  • 環境
    • ansible-base 2.10.1
    • ansible 2.10.0

動画

youtu.be


■ やったこと

インストール

比較のため、ansible-base 2.10.1 (標準モジュールのみ)と、ansible 2.10.0 を別 vevn にそれぞれインストール。

ansible-base インストール

# pip install ansible-base
...(略)...
# ansible --version
ansible 2.10.1
...(略)...
# pip freeze
ansible-base==2.10.1    # ansible-base
cffi==1.14.3
cryptography==3.1.1
Jinja2==2.11.2
MarkupSafe==1.1.1
packaging==20.4
pycparser==2.20
pyparsing==2.4.7
PyYAML==5.3.1
six==1.15.0

ansible インストール

$ pip install ansible-base
...(略)...
# ansible --version
ansible 2.10.1
...(略)...
# pip freeze
ansible==2.10.0         # ansible-base とは別パッケージ扱い
ansible-base==2.10.1    # ansible-base
bcrypt==3.2.0
cffi==1.14.3
cryptography==3.1.1
Jinja2==2.11.2
MarkupSafe==1.1.1
packaging==20.4
paramiko==2.7.2
pycparser==2.20
PyNaCl==1.4.0
pyparsing==2.4.7
PyYAML==5.3.1
six==1.15.0

アップデート

ansible 2.9 から 2.10 へのアップーデートは直接できない。

# pip install ansible -U
Collecting ansible
  Using cached https://files.pythonhosted.org/packages/4a/0b/44b586965bd51135d3915a02d1327fb392843630435cd41d6c89898c5f24/ansible-2.10.0.tar.gz
    Complete output from command python setup.py egg_info:
    
    
                ### ERROR ###
    
                Upgrading directly from ansible-2.9 or less to ansible-2.10 or greater with pip is
                known to cause problems.  Please uninstall the old version found at:
    
                /root/envs/a29/lib64/python3.6/site-packages/ansible/__init__.py
    
                and install the new version:
    
                    pip uninstall ansible
                    pip install ansible
    
                If you have a broken installation, perhaps because ansible-base was installed before
                ansible was upgraded, try this to resolve it:
    
                    pip install --force-reinstall ansible ansible-base
    
                If ansible is installed in a different location than you will be installing it now
                (for example, if the old version is installed by a system package manager to
                /usr/lib/python3.8/site-packages/ansible but you are installing the new version into
                ~/.local/lib/python3.8/site-packages/ansible with `pip install --user ansible`)
                or you want to install anyways and cleanup any breakage afterwards, then you may set
                the ANSIBLE_SKIP_CONFLICT_CHECK environment variable to ignore this check:
    
                    ANSIBLE_SKIP_CONFLICT_CHECK=1 pip install --user ansible
    
                ### END ERROR ###
    
    
    
    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-p3lwq3xc/ansible/

一度アンストールしてから、再インストールする。

# pip uninstall ansible
# pip install ansible

参考: Ansible-2.10.0 has been released!

collection の操作

インストール

# ansible-galaxy collection install cisco.ios

確認

# ansible-galaxy collection list
(ログとり忘れ・・)

デフォルトでは上記のログにもあるように、~/.ansible/collections/ansible_collections/ 配下にインストールされる。(-p オプションや、COLLECTIONS_PATHS で変更可能)

pip install ansible でインストールした場合は lib/python3.6/site-packages/ansible_collections/ のように、別の Python パッケージような扱いで配置される。(COLLECTIONS_SCAN_SYS_PATHTrue ならここも探す)

collection 関連のデフォルト設定

# ansible-config dump | grep -i collection
COLLECTIONS_ON_ANSIBLE_VERSION_MISMATCH(default) = warning
COLLECTIONS_PATHS(default) = ['/root/.ansible/collections', '/usr/share/ansible/collections']
COLLECTIONS_SCAN_SYS_PATH(default) = True

先に、COLLECTIONS_PATHS を探して、なければ COLLECTIONS_SCAN_SYS_PATH に従う模様。

2.9 で書いた Playbook が動くか?

以下の Playbook をおためし。

---
- hosts: ios
  gather_facts: false
  
  tasks:
    - name: show ip route
      ios_command:      # ayashii
        commands:
          - show ip route
      register: resgister_show_ip_route

    - name: debug route
      debug:            # daijoubu
        msg: "{{ resgister_show_ip_route.stdout_lines }}"

ansible-base (collectionなし)

個別に collection をインストールしていない状態で実行。collection に移行した、ios_command がないというエラーに。

# ansible-playbook -i inventory.ini ios_show.yml 
ERROR! couldn't resolve module/action 'cisco.ios.ios_command'. This often indicates a misspelling, missing collection, or incorrect module path.

The error appears to be in '/root/general/vagrant/nwlab/stumble/ios_show.yml': line 6, column 7, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

  tasks:
    - name: show ip route
      ^ here

ansible (collection)

pip install ansible して、自動でざまざまなな collection (cisco.ios含む)をインストールした環境でおためし。正常完了。

# ansible-playbook -i inventory.ini ios_show.yml 

PLAY [ios] ********************************************************************************

TASK [show ip route] **********************************************************************
ok: [rt01]
ok: [rt02]

TASK [debug route] ************************************************************************
ok: [rt01] => 
  msg:
  - - 'Codes: L - local, C - connected, S - static, R - RIP, M - mobile, B - BGP'
    - '       D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area '
    - '       N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2'
    - '       E1 - OSPF external type 1, E2 - OSPF external type 2'
    - '       i - IS-IS, su - IS-IS summary, L1 - IS-IS level-1, L2 - IS-IS level-2'
    - '       ia - IS-IS inter area, * - candidate default, U - per-user static route'
    - '       o - ODR, P - periodic downloaded static route, H - NHRP, l - LISP'
    - '       a - application route'
    - '       + - replicated route, % - next hop override, p - overrides from PfR'
    - ''
    - Gateway of last resort is not set
...(略)...

PLAY RECAP ********************************************************************************
rt01                       : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
rt02                       : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

なぜ動いたか?

前述のように、 pip install ansible でインストールすると、様々な collection (cisco.ios含む)がインストールされるため。

かつ、ios_command のような、装飾なしのモジュールの指定でも FQCN へ紐付けるリダイレクトデータがあるため。

ただ、これまでのバージョンアップ同様、ぎりぎり使えたオプションが使えなくなったり挙動が変わったり警告が表示されるようになったりする可能性はある。


ドキュメントはどうなった?

Module Index が Collection Index に

Collection Index — Ansible Documentation

標準のモジュール類は ansible.builtin を参照

カテゴリごとではなく、collection ごとに。標準のモジュール類は ansible.builtin

Ansible.Builtin — Ansible Documentation

モジュールの説明ページの旧リンクはリダイレクトされる

たとえば https://docs.ansible.com/ansible/latest/modules/ios_command_module.htmlhttps://docs.ansible.com/ansible/latest/collections/cisco/ios/ios_command_module.html へリダイレクトされる。

URL に collection 名が入るのが特徴。

事前にいたご質問

質問1

質問2

何が同梱されて、何が同梱されないのかについて (ざっくり) 知りたいです! (debugはさすがに同梱されると思いますが、ipaddrフィルターとかどうなんだろう)

debugモジュール は標準同梱、ipaddr フィルターは ansible.netcommon collection 配下です。

すでに、ipaddr フィルターの説明ページでも、ansible.netcommon.ipaddr といった表記になっています。

pip install ansible でインストールした環境の場合、ansible-doc -l で表示したときに、装飾なしのモジュール名で表示されるようです。

# ansible-doc -l 
...(略)...
copy                                                                           Copy files to remote locations               
cron                                                                           Manage cron.d and crontab entries            
cyberark.pas.cyberark_account                                                  Module for CyberArk Account object creation, ...
cyberark.pas.cyberark_authentication                                           CyberArk Authentication using PAS Web Service...
cyberark.pas.cyberark_credential                                               Credential retrieval using AAM Central Creden...
cyberark.pas.cyberark_user                                                     CyberArk User Management using PAS Web Servic...
debconf                                                                        Configure a .deb package                     
debug                                                                          Print statements during execution            
dellemc.os10.base_xml_to_dict                                                  Operations for show command output ...(略)...

この方法いいですね!


Part18 にむけて

以下のネタを検討中です。気が向いたものをやります。

  • Ansible 2.10 関連ほかにも
  • connection: local ななにか
  • Ansible Toewr / AWX をコマンドがら操作する
  • ansible.cfg
  • Jinja2、フィルター
  • Windows
  • ESXi で VM作成
  • parsee_cli モジュール(Part15 の続き)
  • when や assert