Ansible playbook run for the servers in the list

2019-08-30 19:45发布

问题:

quick question for Ansible Guru's. I want to run an ansible playbook for a specific set of boxes that I copied to a list.txt disregarding the inventory and the target block in ansible playbook:

---
- name: Ansible Runbook v.1.0
  hosts: test1
  gather_facts: yes
#  serial: "10%"

When I am running the following command I am getting no hosts matched:

ansible-playbook playbook.yaml --tags "simplejson" -vvv -i /x/home/list.txt

PLAY [Ansible Runbook v.1.0] **************************************************
skipping: no hosts matched

$cat list.txt
hostname2b

Any ideas for a workaround ?

回答1:

The reason of no host matching is that host test1, which is hardcoded in playbook, is not present in the inventory file that you specified from command line. The problem is ansible-playbook command does not accept any hosts parameter. So there is no direct way of getting around the hardcoded hosts test1.

However, there is a workaround for this as explained here. You can use a variable for hosts and specify all from command line for that variable. Something like this:

---
- name: Ansible Runbook v.1.0
  hosts: "{{ host_param }}"
  gather_facts: yes

Then pass that variable with extra-vars:

ansible-playbook playbook.yaml -i /x/home/list.txt --extra-vars="host_param=all" --tags "simplejson" -vvv