假设如下:
主剧本.yml
- name: Play-1
hosts: localhost
connection: local
gather_facts: no
roles:
- role: my-role
vars:
newhost: 192.168.1.1
generated_playbook.yml
- name: Play-1
hosts: newhost
gather_facts: yes
tasks:
- name: Task1
- name: Task2
- name: Task3
角色的主要任务:
- name: "Role MAIN-1"
add_host:
name: newhost
ansible_host: "{{newhost}}"
- include: generated_playbook.yml
错误:
ERROR! conflicting action statements: hosts, tasks
The error appears to be in 'generated_playbook.yml': line 1, column 3, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
- name: Generated Playbook
^ here
我创建了新角色并将其包含在主剧本中。
在此角色中,我将新主机添加到内存清单中,然后使用 j2 模板示例输出 generated_playbook.yml 生成新的剧本
问题:
有没有办法仅在新添加的主机上运行这个新生成的剧本,而不向主剧本添加任何其他内容?
我试图使用 import-playbook 或包含在角色中,但这失败了
答案1
这是不可能的。引用自ansible.builtin.import_playbook
带有播放列表的文件只能包含在顶层。
这例子明确地表明了这一点
- name: This DOES NOT WORK
hosts: all
tasks:
- debug:
msg: task1
- name: This fails because I'm inside a play already
import_playbook: stuff.yaml
笔记
- 没有include_playbook。看什么包括_*和进口_*模块可用。
shell> ansible-doc -t module -l | grep include_
include_role Load and ...
include_tasks Dynamically inclu...
include_vars Load variables from files, dynamically...
shell> ansible-doc -t module -l | grep import_
import_playbook Imp...
import_role Import a ro...
import_tasks Impo...
- 引用注释来自导入模块:
这是 Ansible 的核心功能,而不是模块,并且不能像模块一样被覆盖。