ホストがグループに属していない場合にのみタスクを実行する

StackOverflow https://stackoverflow.com//questions/21008083

  •  21-12-2019
  •  | 
  •  

質問

現在のプレイブックのホストが特定のグループにのホストが属していない場合にのみ、Ansibleタスクを実行できます。半疑似コードで:

- name: my command
  command: echo stuff
  when: "if {{ ansible_hostname }} not in {{ ansible_current_groups }}"
.

どうすればいいですか?

役に立ちましたか?

解決

これを行うもう1つの方法は次のとおりです。

- name: my command
  command: echo stuff
  when: "'groupname' not in group_names"
.

group_namesは、ここに記載されているようなマジック変数です。 http://docs.ansible.com/play#magic-variables.html#magic-variables-and-how-to-access-information-about-Olth-hosts

group_namesは、現在のホストがin in inにあるすべてのグループのリスト(配列)です。

他のヒント

group_vars/にあるVARSファイルで、またはこのようなhostsファイルに直接設定できます。

[vagrant:vars]
test_var=true

[location-1]
192.168.33.10 hostname=apollo

[location-2]
192.168.33.20 hostname=zeus

[vagrant:children]
location-1
location-2
.

とこのようなタスクの実行:

- name: "test"
  command: "echo {{test_var}}"
  when: test_var is defined and test_var
.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top