Ansible:当前playbook角色的主机上的with_items

问题描述 投票:2回答:1

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

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

VS:

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

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

dependencies:
  - { role: commondependency }

好的,在设置公共依赖关系时,我想ssh-keys扫描集群中的其他主机。群集可以是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
ansible ansible-playbook
1个回答
2
投票

在这里,我们去,在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}}"
© www.soinside.com 2019 - 2024. All rights reserved.