てくなべ (tekunabe)

ansible / network automation / 学習メモ

[Ansible] 今日のラッキーモジュールを表示する(Ansible占い)

■ はじめに

Ansible には 3,000 を超えるモジュールが標準で含まれています。

ここまで増えると「今日は何のモジュールを触ろうかな?今日のラッキーモジュールはなんだろう?」と気になりますよね。

この記事では、そんなときにぴったりで極めて実用的な Playbook をご紹介します。

※ ただし、結果に対して当方は何ら責任を負いません

  • 動作環境
    • Ansible 2.9.1


■ Playbook

ansible-doc コマンド-l でモジュールの一覧が、-j オプションで JSON 形式で表示されます(-j はAnsible 2.9 からの機能)。

この結果を受け取って、from_json フィルターで、Ansible のディクショナリにパースし、その結果をdict2items フィルターでリストに変換します。

そのリストを、with_random_choice にかけて、ランダムに1つのモジュールを選択します。

- hosts: localhost
  gather_facts: no
  connection: local

  tasks:
    - name: get module list
      command: "ansible-doc -l -j"
      changed_when: no
      register: result

    - name: do uranai
      debug:
        msg:
          - "今日のラッキーモジュールは・・・・"
          - ""
          - ""
          - "    {{ item.key }}    です!!"
          - ""
          - ""
          - "概要: {{ item.value }}"
          - "今すぐアクセス!! >> https://docs.ansible.com/ansible/latest/modules/{{ item.key }}_module.html"
      with_random_choice: "{{ result.stdout | from_json | dict2items }}"
      loop_control:
        label: "{{ item.key }}"


■ 実行例

実行例です。インベントリファイルは不要です。

$ ansible-playbook -i localhost, uranai.yml 

PLAY [localhost] ****************************************************************************************************

TASK [get module list] **********************************************************************************************
ok: [localhost]

TASK [do uranai] ****************************************************************************************************
ok: [localhost] => (item=fortios_system_replacemsg_spam) => {
    "msg": [
        "今日のラッキーモジュールは・・・・",
        "",
        "",
        "    fortios_system_replacemsg_spam    です!!",
        "",
        "",
        "概要: Replacement messages in Fortinet's FortiOS and FortiGate",
        "今すぐアクセス!! >> https://docs.ansible.com/ansible/latest/modules/fortios_system_replacemsg_spam_module.html"
    ]
}

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

ちなみに今日の私のラッキーモジュールは fortios_system_replacemsg_spamでした。

fortios_* モジュールは 400個以上もあるので、出現頻度は高いと思います。

もう一回くらいやってみましょう。

ok: [localhost] => (item=slxos_config) => {
    "msg": [
        "今日のラッキーモジュールは・・・・",
        "",
        "",
        "    slxos_config    です!!",
        "",
        "",
        "概要: Manage Extreme Networks SLX-OS configuration sections",
        "今すぐアクセス!! >> https://docs.ansible.com/ansible/latest/modules/slxos_config_module.html"
    ]
}

slxos_config モジュールでした。ネットワークモジュールが続いて、なんだか嬉しいです。

よく考えたら、同じ日に実行したら同じ結果になるべきですかね。おいておきましょう・・。


■ さいごに

表示の仕方はもっといろいろカスタマイズできますし、組み合わせ次第でチャットツールに送ることもできます。ぜひご活用ください。