Ansible の Playbook を実行すると、最初にリモートホストの情報 (Ansible facts) の収集が行われます。 ここには、OS の情報や IP アドレスの情報などが含まれており、タスクの中でこれらの情報を参照することができます。 実際に Ansible facts にどのような情報が含まれているかは、次のような Playbook で確認することができます。
- hosts: all
gather_facts: true
tasks:
- name: Show facts
ansible.builtin.debug:
var: ansible_facts
TASK [Show facts] *********************************
ok: [example.com] => {
"ansible_facts": {
"all_ipv4_addresses": [
"172.xxx.xx.xx",
"160.xxx.xx.xx"
],
"all_ipv6_addresses": [
"xxxx:xxxx:xxxx:xxx:xxx:xxx:xx:xx",
"xxxx::x:xxxx:xxxx:xxxx"
],
...
ansible_facts
変数の値がからっぽになってしまう場合は、gather_facts: false
と指定していないか確認してください。
ansible_facts
オブジェクトの個々のプロパティを参照するには次のようにします。
- name: Show architecture
ansible.builtin.debug:
msg: "Architecture: {{ ansible_architecture }}"
関連記事
- Ansible タスク例: ユーザーを特定のグループに参加させる (ansible.builtin.user)
- Ansible タスク例: Docker と Docker Compose をインストールする
- Ansible タスク例: 任意のコマンドを実行する (ansible.builtin.command/shell)
- Ansible タスク例: 空のディレクトリを作成する (ansible.builtin.file)
- Ansible タスク例: 空のファイルを作成する (ansible.builtin.file)
- Ansible で VPS を設定するための準備
- Ansible で SSH サーバーをセキュアにする (ansible.builtin.lineinfile, ansible.builtin.service)