Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I specify hosts when importing a playbook

Tags:

ansible

ansible version: 2.9

Hi.

How can I specify the hosts when I import a playbook with import_playbook?

My code (/project/first_pb.yml)

- import_playbook: /test/pb0.yml
  hosts: atlanta
like image 521
CH06 Avatar asked Nov 07 '25 16:11

CH06


2 Answers

Q: "A method to pass specific family hosts to the imported playbook?"

A: There is no difference between a playbook imported or not. For example,

shell> cat pb-A.yml
- hosts: "{{ my_hosts|default('localhost') }}"
  tasks:
    - debug:
        var: inventory_hostname

shell> ansible-playbook pb-A.yml -e my_hosts=host1
...
  inventory_hostname: host1
shell> cat pb-B.yml
- import_playbook: pb-A.yml

shell> ansible-playbook pb-B.yml -e my_hosts=host1
...
  inventory_hostname: host1

There are many options on how to pass specific hosts and groups to a playbook. For example, see:

  • Patterns: targeting hosts and groups

  • add_host module – Add a host and group to the ansible-playbook in-memory inventory

  • Inventory plugins (e.g. constructed)

like image 166
Vladimir Botka Avatar answered Nov 10 '25 05:11

Vladimir Botka


I can filter play plabooks with "when: " condition, for example:

- import_playbook: /test/pb0.yml
  when: hostname != host1a*

- import_playbook: /test/pb0.yml
  when: '"north" not in hostname'

- import_playbook: /test/pb0.yml
  when: '"west" in hostname'
like image 22
CH06 Avatar answered Nov 10 '25 06:11

CH06



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!