Score:-1

Override hosts variable of Ansible playbook without command line

gn flag

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"
U880D avatar
ca flag
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 avatar
ca flag
You might be interested in background information about [Add host during runtime execution w/o starting new play](https://stackoverflow.com/a/71983263/6771046).
Score:0
in flag

--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
tuytuy20 avatar
gn flag
I need do it without command line
in flag
Then you need to either define it in the playbook or in the inventory. These are the only options.
Score:0
cn flag

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

tuytuy20 avatar
gn flag
Thank you. it is not possible.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.