Ansible:当前playbook角色

时间:2015-05-06 20:38:41

标签: ansible ansible-playbook

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

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

1 个答案:

答案 0 :(得分: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}}"