使用ansible 2.8或更高版本:
我需要将模板呈现到我的桌面,以输出角色变量和有关我的操作系统的信息
我需要该变量在角色中具有默认值并被剧本覆盖
运行剧本应该看起来像这样
localhost:ok = 2已更改= 2无法访问= 0失败= 0跳过= 0获救= 0忽略= 0
$ cat〜/ Desktop / my-template.txt
我的自定义变量是test1234
我在Darwin localhost 18.7.0中的操作系统Darwin内核版本18.7.0:PDT 2019年8月20日星期二16:57:14;根目录:xnu-4903.271.2〜2 / RELEASE_X86_64 x86_64
我仍在学习中,我们将不胜感激,谢谢。
这是我到目前为止所拥有的
我的jinja2文件是:my-template.j2
<center>
<h1> My custom variable is {{ test_file }}</h1>
<h3> My operating system in {{ uname_a }}</h3>
</center>
</html>
I need to render a template to my Desktop that outputs a role variable and information about my operating system
I need the variable to have a default value in the role and be overridden by the playbook
My playbook look like this:
主机:127.0.0.1 成为:是的 vars: test_file:“ test1234” uname_a:“达尔文本地主机19.6.0达尔文“,”,“
任务:
答案 0 :(得分:1)
要动态获取模板中内核版本的值,应使用uname -a
命令的输出,而不是将其设置为变量。
示例模板my-template.txt.j2
:
My custom variable is {{ test_file }}
My operating system is {{ uname_a }}
尽管您提到了一个角色,但您看到的剧本中并未使用该角色...
示例剧本:
- hosts: localhost
connection: local
vars:
test_file: 'test1234'
tasks:
- name: get kernel version
command: 'uname -a'
register: uname_result
- name: save to variable
set_fact:
uname_a: '{{ uname_result.stdout }}'
- name: write os details to file
template:
src: 'my-template.txt.j2'
dest: '/tmp/my-template.txt'
在/tmp/my-template.txt
中渲染以下内容(我使用的是Linux机器):
My custom variable is test1234
My operating system is Linux linux-2hyj 3.4.6-2.10-desktop #1
也就是说,您应该尽可能使用Ansible提供的automatic variables。您可以使用的可能事实是:
ansible_os_family
ansible_distribution
ansible_kernel