5

ansible自定义变量在when中判断

 1 year ago
source link: https://blog.51cto.com/u_13236892/5714631
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

ansible自定义变量在when中判断

精选 原创

哭泣的馒头 2022-09-27 09:33:18 博主文章分类:ansible随笔 ©著作权

文章标签 自定义 字段 文章分类 Linux 系统/运维 yyds干货盘点 阅读数178

1、hosts中定义

在hosts自定义模块中新添加了一个字段
vi hosts
[test]
10.10.10.10 iswar=true
[all:vars]
ansible_ssh_port=22
ansible_ssh_user=root
ansible_ssh_pass=123456

iswar这个变量想在yaml中进行when判断
方法一:
when: "{{ iswar }} == true"
这个样子可以使用,但是会报一个warning
"[WARNING] when statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: {{ iswar }} == true"

方法二:
when: iswar == "true"
但是下面两种方式就不行,这两种都不会进行判断
when: iswar == true
when: "iswar == true"

2、在vars_files中定义

cat test_module/test_module.yaml
iswar: true

vars_files: test_module/test_module.yaml
iswar这个变量想在yaml中进行when判断
方法一:
when: "{{ iswar }} == true"
还是会报那个warning
方法二:
when: iswar == true
#注意此处true不能有"",如果有""则不判断
其他方式几乎都不判断
  • 收藏
  • 评论
  • 分享
  • 举报

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK