ansible中的template和copy模块区别
概述
template和copy模块都是用于文件的分发,两者的参数都是一样的,区别在于:
- copy模块仅仅用于静态普通文件的分发
- 而template基于 Jinja2 模板引擎,渲染变量、循环、判断、过滤器后再写入目标机器,本质是 copy + jinja2 渲染
案例
下文有一个mod.j2文件,可以使用copy和template模块分别进行分发,可以看下都有什么效果
"主机名:{{ ansible_hostname }}"
"ip:{{ ansible_all_ipv4_addresses }}"
"系统名称:{{ ansible_distribution }}"
"系统总内存:{{ ansible_memtotal_mb }}"
"系统可用内存:{{ ansible_memfree_mb }}"
"系统内存情况:{{ ansible_memory_mb }}"
"cpu总数:{{ ansible_processor_vcpus }}"
使用copy模块进行分发:
- 编写playbook文件
root@master:/data00/ansible# cat template.yaml
- name: template使用
hosts: es
tasks:
- name: 分发j2文件
copy:
src: /data00/ansible/mod.j2
dest: /tmp/mod
mode: 744
tags: copyj2
- 执行
root@master:/data00/ansible# ansible-playbook template.yaml
PLAY [template使用] **********************************************************************************************************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************************************************************************************************
ok: [10.37.120.9]
TASK [分发j2文件] ************************************************************************************************************************************************************************************************
changed: [10.37.120.9]
PLAY RECAP *******************************************************************************************************************************************************************************************************
10.37.120.9 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- 查看远端机器上的文件
发现文件没有任何改动
root@master:/data00/ansible# ansible es -m shell -a 'cat /tmp/mod'
10.37.120.9 | CHANGED | rc=0 >>
"主机名:{{ ansible_hostname }}"
"ip:{{ ansible_all_ipv4_addresses }}"
"系统名称:{{ ansible_distribution }}"
"系统总内存:{{ ansible_memtotal_mb }}"
"系统可用内存:{{ ansible_memfree_mb }}"
"系统内存情况:{{ ansible_memory_mb }}"
"cpu总数:{{ ansible_processor_vcpus }}"
使用template模块进行分发
- 编写playbook文件
root@master:/data00/ansible# cat template.yaml
- name: template使用
hosts: es
tasks:
- name: 分发j2文件
template:
src: /data00/ansible/mod.j2
dest: /tmp/mod
mode: 744
tags: copyj2
- 执行
TASK [Gathering Facts] *******************************************************************************************************************************************************************************************
ok: [10.37.120.9]
# 忽略这个告警
TASK [分发j2文件] ************************************************************************************************************************************************************************************************
[WARNING]: Deprecation warnings can be disabled by setting `deprecation_warnings=False` in ansible.cfg.
[DEPRECATION WARNING]: INJECT_FACTS_AS_VARS default to `True` is deprecated, top-level facts will not be auto injected after the change. This feature will be removed from ansible-core version 2.24.
Origin: /data00/ansible/mod.j2
Use `ansible_facts["fact_name"]` (no `ansible_` prefix) instead.
changed: [10.37.120.9]
PLAY RECAP *******************************************************************************************************************************************************************************************************
10.37.120.9 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- 查看远端文件
发现文件中的变量已经被替换了
root@master:/data00/ansible# ansible es -m shell -a 'cat /tmp/mod'
10.37.120.9 | CHANGED | rc=0 >>
"主机名:node02"
"ip:['10.96.0.10', '10.96.3.191', '10.96.3.164', '10.96.1.170', '10.96.0.1', '172.17.0.1', '100.95.185.192', '10.37.120.9']"
"系统名称:Debian"
"系统总内存:253453"
"系统可用内存:192775"
"系统内存情况:{'real': {'total': 253453, 'used': 60678, 'free': 192775}, 'nocache': {'free': 216558, 'used': 36895}, 'swap': {'total': 0, 'free': 0, 'used': 0, 'cached': 0}}"
"cpu总数:64"
本文来自博客园,作者:huangSir-devops,转载请注明原文链接:https://www.cnblogs.com/huangSir-devops/p/21449440,微信Vac6666666,欢迎交流

浙公网安备 33010602011771号