ansible-playbook的简单传参方式

// -v  是看运行细节。要更细节的信息,把-v换成 -vvv
// myhost 是我们自己写的host文件。也就是说,我们不一定要用/etc/ansible/hosts那个文件。
// -e 是传参到yml文件里面
ansible的渲染是依赖于jinja2的。
所以yml文件里面都是用花括号,表示待渲染的变量:{{  }}
 
vim  test.yml
复制代码
复制代码
- hosts: web
  gather_facts: no
 
 
  tasks:
  - ping:
  - name: get S/N
    shell: show version | grep S/N | cut -f2 -d":" $2'|sed  's/^[ \t]*//
    register: the_SN
    when: ("CPE") != (node.type)
    run_once: true
  - name: check S/N
    command: /bin/false
    register: result
    when: the_SN["stdout"] is defined and the_SN["stdout"] != (node.serialNum)
    ignore_errors: yes
    run_once: true
  - name: record the wrong S/N
    copy:
      content: '{"failed": true,"changed": false,"msg": "the sn given is not equal to system sn"}'
      dest: __result.json
    when: result is failed
    connection: local
    run_once: true
  - name: is end
    fail: msg='the sn given:{{node.serialNum}} is not equal to system sn:{{the_SN["stdout"]}}'
    when: result is failed
  - name: should goon
    debug: msg="go on..............."
复制代码
复制代码

 

vim  myhost
1
2
3
4
5
6
7
[host1]
192.168.1.100
   
[host1:vars]
ansible_ssh_user="user"
ansible_ssh_port=22
ansible_ssh_pass="Password"

 

有了以上的数据,我们就可以对host1里面的机子用yml文件做配置了。但是参数-e,后面如果需要很多的参数,那就不方便了。比如这样:
ansible-playbook -i myhost test.yml -e '{"name":"xiaoming","age":18......}'
这样写起来就笼长。所以,其实还有一下写法:
先写一个json文件:test.json
 
vim test.json
1
{"name":"xiaoming","age":18}
 
然后这么去使用:
ansible-playbook -i myhost test.yml -e '@test.json'
是的,没错。就像微信聊天,扣扣聊天一样,艾特一下它,就可以了。这样,test.json里面的参数,就可以传到yml文件里面了。
posted @   linuxws  阅读(1079)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示