-1

Have 2 hosts my_host1, my_host2 This code work for my_host1, I also need run it for my_host2 Can be overridden hosts value on ansible main.yml task without changing string "hosts: my_host1" in init playbook.yml? Command line --extra-vars is not the solution for me.

Thank you

---

#init playbook.yml
- name: run role test
  hosts: my_host1
  roles:
   - role: test_1

---

#main.yml in role test_1

   - name: Run tasks from example.yml
     block:
       - include_tasks: example.yml


---
# example.yml

   - debug:
       msg: "This is example task"
  • Can you explain in more detail why command line options are no option for you? As well why it is not possible to write an inventory? – U880D May 28 '23 at 15:34
  • You might be interested in background information about [Add host during runtime execution w/o starting new play](https://stackoverflow.com/a/71983263/6771046). – U880D May 28 '23 at 15:44

2 Answers2

0

--extra-vars would not work anyway, since the hosts definition is not a variable.

Usually you add the hosts where you want to run a playbook, if not defined explicitly in the playbook, via the --limit argument.

# playbook.yml
- name: run role test
  hosts: all

Then you execute:

ansible-playbook playbook.yaml --limit my_host1,myhost2

Or you provide a group defined in the inventory

[my_hosts]
my_host1
my_host2

call:

ansible-playbook playbook.yaml --limit my_hosts
Gerald Schneider
  • 23,274
  • 8
  • 57
  • 89
0

You should simply write an inventory file with a [computer] block for instance, and then set your hosts: computer.

Your playbook will be applied on every hosts within your inventory. That's the base of Ansible purpose.

https://docs.ansible.com/ansible/latest/inventory_guide/intro_inventory.html

motorbass
  • 303
  • 2
  • 12