要使用Ansible模板推送至多个主机,可以按照以下步骤进行操作:
template_playbook.yml
,并定义主机列表:---
- name: Push template to multiple hosts
hosts: my_hosts
gather_facts: false
tasks:
- name: Copy template file
template:
src: path/to/template_file.j2
dest: /path/on/remote/template_file
hosts.ini
,并在其中定义主机列表:[my_hosts]
host1 ansible_host=192.168.0.1
host2 ansible_host=192.168.0.2
host3 ansible_host=192.168.0.3
template_file.j2
,其中包含你的模板内容:This is a template file.
Variable1: {{ variable1 }}
Variable2: {{ variable2 }}
ansible-playbook -i hosts.ini -e "variable1=value1 variable2=value2" template_playbook.yml
在运行上述命令时,Ansible会将template_file.j2
中的变量替换为提供的值,并将生成的文件推送到所有主机上的/path/on/remote/template_file
位置。
请确保在运行前已经安装了Ansible,并且已经在本地配置好SSH连接。
上一篇:Ansible模板循环变量
下一篇:Ansible模板追加到文件