Ansibleのsetupモジュール

# Ansibleの情報収集モジュール
Ansibleで使用できるモジュールでいうと[setup](https://docs.ansible.com/ansible/latest/modules/setup_module.html#setup-module)、[ohai](https://docs.ansible.com/ansible/latest/modules/ohai_module.html#ohai-module)、[facter](https://docs.ansible.com/ansible/latest/modules/facter_module.html#facter-module)がありますが、当初、3つを比較してそれぞれの使い分けについて考えようと思っていました。

が・・・そもそもohai、facterがインストールされたホストに対してsetupモジュールで情報取得に行くと、ohai、facterの情報もまとめて取ってきてくれるんですね。

改めてsetupモジュールのドキュメント読むと普通に書いてありました。
https://docs.ansible.com/ansible/latest/modules/setup_module.html

# ohai、facterモジュールの使用
Ansibleでよく使われるsetupモジュールは特別な操作をしなくともそのまま使用できますが、ohai、facterは管理ホストに対してパッケージをインストールする必要があります。（一般的なLinuxディストリの場合）

# 各モジュールで取得可能な情報
それぞれどのような情報が取得できるか、各モジュールで取得できる情報の第一階層（setupモジュールのみ第一階層だとfactsの情報しか出なかったので第二階層）を取得してみました。

今回、管理ホストにohai、facterをインストールした状態でsetupモジュールを実行しているため、ohai、facterの情報もsetupモジュールの実行結果に含まれていることが確認できます。

ohai、facterの情報は、それぞれ「ohai_」「facter_」の接頭語がついているので、どのモジュールの情報かわかりやすいですね。

<details><summary>setup</summary>
"ansible_all_ipv4_addresses": [
"ansible_all_ipv6_addresses": [
"ansible_apparmor": {
"ansible_architecture": "x86_64",
"ansible_bios_date": "12/01/2006",
"ansible_bios_version": "VirtualBox",
"ansible_cmdline": {
"ansible_date_time": {
"ansible_default_ipv4": {
"ansible_default_ipv6": {},
"ansible_device_links": {
"ansible_devices": {
"ansible_distribution": "Ubuntu",
"ansible_distribution_file_parsed": true,
"ansible_distribution_file_path": "/etc/os-release",
"ansible_distribution_file_variety": "Debian",
"ansible_distribution_major_version": "20",
"ansible_distribution_release": "focal",
"ansible_distribution_version": "20.04",
"ansible_dns": {
"ansible_docker0": {
"ansible_domain": "",
"ansible_effective_group_id": 1000,
"ansible_effective_user_id": 1000,
"ansible_enp0s3": {
"ansible_enp0s8": {
"ansible_env": {
"ansible_fibre_channel_wwn": [],
"ansible_fips": false,
"ansible_form_factor": "Other",
"ansible_fqdn": "vmubuntu01",
"ansible_hostname": "vmubuntu01",
"ansible_hostnqn": "",
"ansible_interfaces": [
"ansible_is_chroot": false,
"ansible_iscsi_iqn": "",
"ansible_kernel": "5.4.0-40-generic",
"ansible_kernel_version": "#44-Ubuntu SMP Tue Jun 23 00:01:04 UTC 2020",
"ansible_lo": {
"ansible_local": {},
"ansible_lsb": {
"ansible_machine": "x86_64",
"ansible_machine_id": "389b93f59183469fb13afb4aae1b55a2",
"ansible_memfree_mb": 755,
"ansible_memory_mb": {
"ansible_memtotal_mb": 1987,
"ansible_mounts": [
"ansible_nodename": "vmubuntu01",
"ansible_os_family": "Debian",
"ansible_pkg_mgr": "apt",
"ansible_proc_cmdline": {
"ansible_processor": [
"ansible_processor_cores": 2,
"ansible_processor_count": 1,
"ansible_processor_threads_per_core": 1,
"ansible_processor_vcpus": 2,
"ansible_product_name": "VirtualBox",
"ansible_product_serial": "NA",
"ansible_product_uuid": "NA",
"ansible_product_version": "1.2",
"ansible_python": {
"ansible_python_version": "3.8.2",
"ansible_real_group_id": 1000,
"ansible_real_user_id": 1000,
"ansible_selinux": {
"ansible_selinux_python_present": false,
"ansible_service_mgr": "systemd",
"ansible_ssh_host_key_ecdsa_public": "AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBMOUpASBVedbKFxuOMF1V71hZu8n1SC3IZRt+ZNOc1+YOUTa+SLcM2uJb9LGNsohNu24BoLFCiZB7QXhyAWNevE=",
"ansible_ssh_host_key_ed25519_public": "AAAAC3NzaC1lZDI1NTE5AAAAIOApcjPxH1NxypOBfEwBVgtCRoUW4bvBXfGxHDUiqQt5",
"ansible_ssh_host_key_rsa_public": "AAAAB3NzaC1yc2EAAAADAQABAAABgQC/K+pHJ3DZd+Qysd5QVJ4qoz90rlyGUzj2BiRAKIDZZ080l0xRJ1iEkdTJE7bCrq33hIDo4YRbSYIUCp25MgMwGq2yDuhdk7AcYjrLHuss0pP2UmO24wjGet5V3n+EhO75oelCja99Ru7uenwKlfVHO2dVY1yiC5KaYU4YDaRT/7v5qQ+vPDaxjhRktPvpvJWCo0rP7qKxSxCF4a6pebIWUQyV+B6XX/39OzYUrqQ/A5+jJO0QGCO+qb//WbxhGu4CCBKq4+Vc8NNWAfh2c73A1HDAFDZdHMYy1lTeyehMt3751mdp4x0vOLp/95kep354upuO2wzzFCpkA/luw+CcuUq25KgoP9FkVhFv483ktBEWl3K18syr3okeqh0WIYKaAPMvrWWxKRAzMq4KtyWPnrQvb3eYqFzv0Ba1npKicnN1yU6fFkVo44Dr9eNP9V/lUje8KQASHFxa7VyDA+mx/ZPd8p6Z/6n1SFFugtNL1+5PNkeY44mbz35qZccq1ls=",
"ansible_swapfree_mb": 923,
"ansible_swaptotal_mb": 923,
"ansible_system": "Linux",
"ansible_system_capabilities": [
"ansible_system_capabilities_enforced": "True",
"ansible_system_vendor": "innotek GmbH",
"ansible_uptime_seconds": 1694,
"ansible_user_dir": "/home/vmware",
"ansible_user_gecos": "vmware,,,",
"ansible_user_gid": 1000,
"ansible_user_id": "vmware",
"ansible_user_shell": "/bin/bash",
"ansible_user_uid": 1000,
"ansible_userspace_architecture": "x86_64",
"ansible_userspace_bits": "64",
"ansible_virtualization_role": "guest",
"ansible_virtualization_type": "virtualbox",
"facter_disks": {
"facter_dmi": {
"facter_facterversion": "3.11.0",
"facter_filesystems": "ext2,ext3,ext4,squashfs,vfat",
"facter_fips_enabled": false,
"facter_identity": {
"facter_is_virtual": true,
"facter_kernel": "Linux",
"facter_kernelmajversion": "5.4",
"facter_kernelrelease": "5.4.0-40-generic",
"facter_kernelversion": "5.4.0",
"facter_load_averages": {
"facter_memory": {
"facter_mountpoints": {
"facter_networking": {
"facter_os": {
"facter_partitions": {
"facter_path": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games",
"facter_processors": {
"facter_ruby": {
"facter_ssh": {
"facter_system_uptime": {
"facter_timezone": "JST",
"facter_virtual": "virtualbox",
"gather_subset": [
"module_setup": true,
"ohai_block_device": {
"ohai_chef_packages": {
"ohai_cloud": null,
"ohai_command": {
"ohai_counters": {
"ohai_cpu": {
"ohai_dmi": {
"ohai_docker": {
"ohai_domain": null,
"ohai_filesystem": {
"ohai_fips": {
"ohai_fqdn": "vmubuntu01",
"ohai_hostname": "vmubuntu01",
"ohai_hostnamectl": {
"ohai_idletime": "55 minutes 07 seconds",
"ohai_idletime_seconds": 3307,
"ohai_init_package": "systemd",
"ohai_ip6address": "fe80::3ace:bd6d:7658:e333",
"ohai_ipaddress": "10.0.3.15",
"ohai_kernel": {
"ohai_keys": {
"ohai_languages": {
"ohai_lsb": {
"ohai_macaddress": "08:00:27:13:70:39",
"ohai_machine_id": "389b93f59183469fb13afb4aae1b55a2",
"ohai_machinename": "vmubuntu01",
"ohai_memory": {
"ohai_network": {
"ohai_ohai_time": 1594852696.2341497,
"ohai_os": "linux",
"ohai_os_version": "5.4.0-40-generic",
"ohai_packages": {
"ohai_platform": "ubuntu",
"ohai_platform_family": "debian",
"ohai_platform_version": "20.04",
"ohai_root_group": "root",
"ohai_shells": [
"ohai_sysconf": {
"ohai_systemd_paths": {
"ohai_time": {
"ohai_uptime": "28 minutes 16 seconds",
"ohai_uptime_seconds": 1696,
"ohai_virtualization": {
</details>

<details><summary>ohai</summary>
"block_device": {
"changed": false,
"chef_packages": {
"cloud": null,
"command": {
"counters": {
"cpu": {
"dmi": {
"docker": {
"domain": null,
"filesystem": {
"fips": {
"fqdn": "vmubuntu01",
"hostname": "vmubuntu01",
"hostnamectl": {
"idletime": "53 minutes 14 seconds",
"idletime_seconds": 3194,
"init_package": "systemd",
"ip6address": "fe80::3ace:bd6d:7658:e333",
"ipaddress": "10.0.3.15",
"kernel": {
"keys": {
"languages": {
"lsb": {
"macaddress": "08:00:27:13:70:39",
"machine_id": "389b93f59183469fb13afb4aae1b55a2",
"machinename": "vmubuntu01",
"memory": {
"network": {
"ohai_time": 1594852637.2567618,
"os": "linux",
"os_version": "5.4.0-40-generic",
"packages": {
"platform": "ubuntu",
"platform_family": "debian",
"platform_version": "20.04",
"root_group": "root",
"shells": [
"sysconf": {
"systemd_paths": {
"time": {
"uptime": "27 minutes 17 seconds",
"uptime_seconds": 1637,
"virtualization": {
</details>

<details><summary>facter</summary>
"changed": false,
"disks": {
"dmi": {
"facterversion": "3.11.0",
"filesystems": "ext2,ext3,ext4,squashfs,vfat",
"fips_enabled": false,
"identity": {
"is_virtual": true,
"kernel": "Linux",
"kernelmajversion": "5.4",
"kernelrelease": "5.4.0-40-generic",
"kernelversion": "5.4.0",
"load_averages": {
"memory": {
"mountpoints": {
"networking": {
"os": {
"partitions": {
"path": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games",
"processors": {
"ruby": {
"ssh": {
"system_uptime": {
"timezone": "JST",
"virtual": "virtualbox"
</details>

# 情報取得方法
通常の変数を呼び出す時と同様`{{ [変数名] }}`で取得できます。
また、ネストされた情報は「.」で区切ることで取得できます。

例えば以下enp0s3のIPv4アドレス情報を取得したい場合、`{{ ansible_enp0s3.ipv4.address }}`と指定することでアドレス情報を変数として呼び出すことができます。

```json:setup実行結果（抜粋）
"ansible_enp0s3": {
(略)
    "ipv4": {
        "address": "192.168.56.130",
        "broadcast": "192.168.56.255",
        "netmask": "255.255.255.0",
        "network": "192.168.56.0"
    },
```

```text:"ansible_enp0s3.ipv4.address"取得結果
192.168.56.130
```