【一篇非常好的ansible参考博文】
初识Ansible http://liumissyou.blog.51cto.com/4828343/1616462
-------------------------------------------------------------------
练习ansible的playbook配置,血泪斑斑啊,yaml的语法真挑战老程序员的常规思维,整个儿各种角度破坏性颠覆。
列出一路艰辛踩过和遇到的坑洼:
- 1)此货严格按左对齐的层次区分上下级;
举个例子--此例子目标用playbook配置的key变量值替换掉 hosts中指定的变量。
hosts主机配置,注意key
-------------------
[local]
192.168.52.102 key=ansible-host
192.168.52.101 key=ansible-node
variable.yaml文件,错格式
- hosts: local
gather_facts: False
vars:
key: Ansible
tasks:
- name: display Host Variable from hostfile
debug: msg=" The {{ inventory_hostname }} Value is {{ key }}"
执行后,*一片红叉叉的颜色。。。
以上是报错截图,直接报出来 hosts有错(大骗子!!!),在对hosts各种角度摧残调整无效后,各种资料查找和翻书才知道,被一个空格给坑了。
正确的代码
- hosts: local
gather_facts: False
vars:
key: Ansible
tasks:
- name: display Host Variable from hostfile
debug: msg=" The {{ inventory_hostname }} Value is {{ key }}"
[root@localhost ansible_playbook]# ansible-playbook variable.yaml PLAY [local] ******************************************************************* TASK [display Host Variable from hostfile] *************************************
ok: [192.168.52.102] => {
"msg": " The 192.168.52.102 Value is Ansible"
}
ok: [192.168.52.101] => {
"msg": " The 192.168.52.101 Value is Ansible"
} PLAY RECAP *********************************************************************
192.168.52.101 : ok= changed= unreachable= failed=
192.168.52.102 : ok= changed= unreachable= failed=