dannyman
dannyman

Reputation: 691

Ansible: with_items over hosts in current playbook role

I am building a role that I might want to include as a dependency for multiple playbooks. I want to support:

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

Vs:

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

While in either clusterA or clusterB meta/main.yml, I might have:

dependencies:
  - { role: commondependency }

Okay, while setting up commondependency I want to ssh-keyscan the other hosts in the cluster. The cluster could be clusterA, or it could be clusterB, or it could be clusterY. I can find tons of examples like this:

- 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']

But what I really want is:

- 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

Upvotes: 2

Views: 2085

Answers (1)

dannyman
dannyman

Reputation: 691

Here we go, mentioned way at the bottom of 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}}"

Upvotes: 2

Related Questions