カスタムファクト(ローカルファクト)は、ansibleマネージドホストで宣言される変数です。カスタムファクトは、管理対象ホストの/etc/ansible/facts.dディレクトリのiniファイルまたはjsonファイルで宣言されます。カスタムファクトのファイル名には、.fact拡張子が必要です。
この記事では、カスタムファクトを作成して使用してSambaファイルサーバーをインストールし、そのサービスをansibleマネージドホストで開始する方法について説明します。ここでは、インベントリのファイルサーバーグループの一部としてhost1とhost2を使用しています。
カスタムファクトを示すために、以下は私のラボのセットアップです
- control.example.com — 10.20.0.57
- host1.example.com — 10.20.0.10//Ansibleマネージドホスト
- host3.example.com — 10.20.0.30//Ansibleマネージドホスト
注: devopsユーザーは、sudo権限を持つansibleコントロールおよびマネージドホストで構成されます。 Inventoryおよびansible.cfgファイルは、/ home / developments/installディレクトリーの下に定義されています。私の在庫の内容は以下のとおりです:
[[email protected] install]$ cat inventory [fileservers] host1.example.com host3.example.com [dbservers] host2.example.com host1.example.com [[email protected] install]$
カスタムローカルファクトを宣言して使用するための論理的な手順は次のとおりです
- 拡張子が.factのansibleコントロールホストにファクトファイルを作成します
- プレイブックに1つのプレイを作成して、フォルダ「/etc/ansible/facts.d」を作成し、このフォルダの管理対象ホストにファクトファイルをコピーします。
- プレイブックに2番目のプレイを作成し、ansible_local。
。 。 を使用してこれらのカスタムファクトを使用してSambaサーバーをインストールし、そのサービスを開始します。
カスタムファクトまたはローカルファクトの実際の実装について詳しく見ていきましょう。
ステップ1)コントロールノードにカスタムファクトファイルを作成する
次の内容でcustomfacts.factファイルを作成しましょう
[[email protected] install]$ cat customfacts.fact [localfacts] pkgname = samba srvc = smb [[email protected] install]$
ここで、localfactsはfactnameであり、pkgname&srvcは変数です。
ステップ2)2つの異なるプレイでプレイブックを作成する
以下の内容でcustomfacts-install.yamlプレイブックを作成します
[[email protected] install]$ vi customfacts-install.yaml --- - name: Install custom facts hosts: fileservers vars: remote_dir: /etc/ansible/facts.d facts_file: customfacts.fact tasks: - name: Create Facts Dir on Managed Hosts file: path: "{{ remote_dir }}" state: directory recurse: yes - name: Copy Contents to Facts file copy: src: "{{ facts_file }}" dest: "{{ remote_dir }}" - name: Install Samba Server with Custom Facts hosts: fileservers tasks: - name: Install SMB package: name: "{{ ansible_local.customfacts.localfacts.pkgname }}" state: present - name: Start SMB Service service: name: "{{ ansible_local.customfacts.localfacts.srvc }}" state: started enabled: yes
ファイルを保存して終了します。
ステップ3)ファイルサーバーでプレイブックを実行する
プレイブックをファイルサーバーで実行し、実行する前に、コントロールノードからこれらのノードへの接続を確認しましょう。
[[email protected] install]$ ansible fileservers -m ping
上記はピンポンが正常に機能していることを確認しているので、underコマンドを使用してansibleプレイブックを実行してみましょう
[[email protected] install]$ ansible-playbook customfacts-install.yaml
上記の出力は、プレイブックが正常に実行されたことを示しています。カスタムファクトとSambaサービスのインストールを確認しましょう。
ステップ5)カスタムローカルファクトとSambaサービスを確認する
以下のansibleアドホックコマンドを実行して、カスタムファクトのインストールを確認します
[[email protected] install]$ ansible fileservers -m setup -a "filter=ansible_local"
以下を実行して、Sambaサーバーのサービスステータスを確認します。
[[email protected] install]$ ansible fileservers -m command -a "systemctl status smb"
完璧な上記の出力は、Sambaが正常にインストールされ、そのサービスが稼働していることを確認します。
この記事からは以上です。カスタムファクトのインストールとその使用法に関する基本的な考え方を理解していただければ幸いです。