// -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/^[ ]*// 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
[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
{"name":"xiaoming","age":18}
然后这么去使用:
ansible-playbook -i myhost test.yml -e '@test.json'
是的,没错。就像微信聊天,扣扣聊天一样,艾特一下它,就可以了。这样,test.json里面的参数,就可以传到yml文件里面了。