首页 > 解决方案 > 重用剧本并从主剧本传递主机

问题描述

我有select.ymlIP 提示,machine_id.yml它应该包含在select.yml并且应该从select.yml. 我尝试了很多配置都没有成功。

如何重用machine_id.yml和注入主机?

选择.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

标签: ansible

解决方案


你走对了。- import_playbook: machine_id.yml必须在水平- hosts:

这是一个对我有用的例子:

选择.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:

你能试一试吗?如果它不起作用,请告诉我们您的错误。


推荐阅读