追加の変数(target_hosts
)としてホスト名のリストを使用して、インベントリ全体に対して実行する必要のあるプレイブックがあります。
target_hosts
すべてのホストには、group_id
hostvarが定義されています。group_id
varに対応する一部の補助ホストは、1つのセクションで一致するようにグループごとの構成を必要とするため、インベントリ全体を使用します。
多くの場合group_id
、target_hosts
リスト内のホストに関連付けられた複数の値があります。補助ホストの正しいインベントリグループを選択し、プレイブックをインポート/実行して、メインプレイブックの途中で両方のサーバーセットを構成する必要があります。
これは私が現在行っていることです:
include_playbook: group-configure.yaml
vars:
src_hosts: "group-{{ group_id }}-ancillary-1"
dest_hosts: "{{ target_hosts }}"
私は現在、手動でを手動で分離してから、メインのプレイブックをそれぞれ1回実行target_hosts
するgroup_id
必要があります。これには大量の不要なオーバーヘッドがあります。
私が本当に実行したいのはこれです:
for each group of hosts from `target_hosts` with the same `group_id` hostvar:
import and run group-configure.yaml with:
src_hosts: "ancillary-{{ group_id }}"
target_hosts: restricted to those with that value of `group_id`'
これどうやってするの?これが構造化されている現在の方法が機能しない場合、最良の代替アプローチは何ですか?
とadd_host:
組み合わせるとgroupby
、探しているものになると確信しています。これにより、それらのホストを属性ごとにロールアップし、そのグループがすでに定義されているかのように、それらに対してプレイブックを実行できます。
- hosts: localhost
connection: local
gather_facts: no
become: no
vars:
list_of_name_groups: >-
{%- set results = [] -%}
{%- for g_id, items in (dict(hostvars) | dict2items | groupby("value.group_id")) -%}
{%- for hostname in (items | map(attribute="key") | list) -%}
{%- set _ = results.append({"group_id": g_id, "hostname": hostname}) -%}
{%- endfor -%}
{%- endfor -%}
{{ results }}
tasks:
- add_host:
name: '{{ item.hostname }}'
groups: ancillary-{{ item.group_id }}
with_items: '{{ list_of_name_groups }}'
- hosts: ancillary-my-awesome-groupid
# etc etc
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加