Ansible:with_items over hosts in current playbook role

Ansible: with_items over hosts in current playbook role

我正在构建一个角色,我可能希望将其包含为多个剧本的依赖项。我要支持:

clusterA.yml
- hosts:
  - clusterA
  roles:
  - clusterA

对比:

clusterB.yml
- hosts:
  - clusterB
  roles:
  - clusterB

在 clusterA 或 clusterB meta/main.yml 中时,我可能有:

dependencies:
  - { role: commondependency }

好的,在设置 commondependency 时我想对集群中的其他主机进行 ssh-keyscan。集群可以是 clusterA,也可以是 clusterB,也可以是 clusterY。我可以找到很多这样的例子:

- name: Key Scan Cluster
  shell: ( ssh-keyscan {{item}} && cat /opt/commondependency/.ssh/known_hosts | sort | uniq ) > /opt/commondependency/.ssh/known_hosts
  with_items: hosts['clusterA']

但我真正想要的是:

- name: Key Scan Cluster
  shell: ( ssh-keyscan {{item}} && cat /opt/commondependency/.ssh/known_hosts | sort | uniq ) > /opt/commondependency/.ssh/known_hosts
  with_items: the hosts I am running a playbook on right now

开始吧,http://docs.ansible.com/playbooks_variables.html底部提到的方式:

- name: Key Scan Cluster
  shell: ( ssh-keyscan {{item}} && cat /opt/commondependency /.ssh/known_hosts | sort | uniq ) > /opt/commondependency/.ssh/known_hosts
  with_items: "{{ansible_play_hosts}}"