マッピング値に基づいてAnsibleロールのホストを指定できますか?

マッピング値に基づいてAnsibleロールのホストを指定できますか?

単一ノードアーキテクチャまたは分散アーキテクチャにアプリケーションを設定できるホストがあります。

だから在庫があります。

[STG]
node1

[LIVE]
app_node
db_node
gateway_node

したがって、デフォルト値に設定されている変数をsingleCLIからに変更できますdistributed

役割定義があります。

- hosts:
  gather_facts: no
  roles:
      - {role: setup, tags: ['setup', 'orchestra']}

そのため、ホスト行が地図の値に応じて動的に表示されるようにします。

- hosts: 'if single then host == STG else LIVE'

ベストアンサー1

より多くのオプションがあります:

  1. 論理を式に置き換えるマスター:
shell> cat pb.yml
- hosts: "{{ (map_value == 'single')|ternary('STG', 'LIVE') }}"
  tasks:
    - debug:
        var: ansible_play_hosts
      run_once: true

あなたが望むものを与える

shell> ansible-playbook pb.yml -e map_value=single

PLAY [single] ********************************************************************************

TASK [debug] *********************************************************************************
ok: [node1] => 
  ansible_play_hosts:
  - node1

PLAY RECAP ***********************************************************************************
node1: ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
shell> ansible-playbook pb.yml -e map_value=distributed

PLAY [distributed] ***************************************************************************

TASK [debug] *********************************************************************************
ok: [app_node] => 
  ansible_play_hosts:
  - app_node
  - db_node
  - gateway_node

PLAY RECAP ***********************************************************************************
app_node: ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0


  1. 作る子供たち(グループエイリアス)
shell> cat hosts 
[STG]
node1

[single:children]
STG

[LIVE]
app_node
db_node
gateway_node

[distributed:children]
LIVE

スクリプトは同じ結果を提供します。

shell> cat pb.yml
- hosts: "{{ map_value }}"
  tasks:
    - debug:
        var: ansible_play_hosts
      run_once: true

在庫ファイルを変更できない場合は、別名を別のファイルに入れてください。例えば、

shell> tree inventory/
inventory/
├── 01-hosts
└── 02-aliases
shell> cat inventory/01-hosts 
[STG]
node1

[LIVE]
app_node
db_node
gateway_node
shell> cat inventory/02-aliases 
[single:children]
STG

[distributed:children]
LIVE

スクリプトは同じ結果を提供します。

shell> ansible-playbook -i inventory pb.yml -e map_value=single
...
shell> ansible-playbook -i inventory pb.yml -e map_value=distributed
...

  1. インベントリプラグインの使用設立する。バラより
shell> ansible-doc -t inventory constructed

たとえば、在庫

shell> tree inventory
inventory
├── 01-hosts
└── 02-constructed.yml
shell> cat inventory/01-hosts 
[STG]
node1

[STG:vars]
map_group_value=single

[LIVE]
app_node
db_node
gateway_node

[LIVE:vars]
map_group_value=distributed
shell> cat inventory/02-constructed.yml 
plugin: constructed
use_extra_vars: true
compose:
  map_group: map_value
groups:
  map_group: map_group == map_group_value

その後、スクリプト

- hosts: map_group
  tasks:
    - debug:
        var: ansible_play_hosts
      run_once: true

同じ結果を提供します

shell> ansible-playbook -i inventory pb.yml -e map_value=single
...
shell> ansible-playbook -i inventory pb.yml -e map_value=distributed
...

役割テストケースの使用に固執する場合は、役割を作成してください。

shell> cat roles/setup/tasks/main.yml 
- debug:
    var: ansible_play_hosts
  run_once: true

プレイブックで好きなタグと一緒に使用してください。

shell> cat pb.yml
- hosts: map_group
  roles:
    - role: setup

おすすめ記事