重用剧本并通过主剧本传递主机

时间:2020-08-29 14:43:38

标签: ansible

我有select.yml,其中包含IP和machine_id.yml的提示,这些提示应包含在select.yml中,并应成为select.yml的主机。我尝试了许多配置,但均未成功。

如何重用machine_id.yml并将主机注入其中?

select.yml

- hosts: localhost
  gather_facts: no
  vars_prompt:
  - name: target_host
    prompt: "[M] please enter the target host IP"
    private: no
  tasks:
    - add_host:
        name: "{{ target_host }}"
        groups: dynamic_hosts
    - import_playbook: machine_id.yml

machine_id.yml

- hosts: localhost
  gather_facts: no
  vars_prompt:
  - name: target_host
    prompt: please enter the target host IP
    private: no
  tasks:
    - add_host:
        name: "{{ target_host }}"
        groups: dynamic_hosts

- hosts: "{{ dynamic_hosts }}"
  vars:  
    ansible_python_interpreter: /usr/bin/python3
  become: yes

  tasks:
    - name: Reset machine-id
      shell: rm /etc/machine-id && rm /var/lib/dbus/machine-id && dbus-uuidgen --ensure=/etc/machine-id && dbus-uuidgen --ensure
      args:
        warn: no

1 个答案:

答案 0 :(得分:1)

您的方法正确。 - import_playbook: machine_id.yml必须处于- hosts:

级别

以下是对我有用的示例:

select.yaml:

- hosts: localhost
  gather_facts: no
  vars_prompt:
  - name: target_host
    prompt: "[M] please enter the target host IP"
    private: no
  tasks:
    - add_host:
        name: "{{ target_host }}"
        groups: dynamic_hosts
    - debug: var=groups
- import_playbook: machine_id.yaml

machine_id.yaml:

- hosts: dynamic_hosts
  gather_facts: no
  tasks:
    - ping:

可以尝试一下吗?如果它不起作用,请告诉我们您的错误。