实例学习ansible系列(16)playbook中自定义环境变量的使用,ansibleplaybook
实例学习ansible系列(16)playbook中自定义环境变量的使用,ansibleplaybook
在前面的文章中我们使用过环境变量, 但是用户自定义或者随时export出来的环境变量却无法直接引用,而这种方式却是实际工作中用到的最多的情况,使用ansible提供的lookup便可解决这个问题.
定义一个环境变量并export:# export MESSAGE="hello world information"
准备一个简单的playbook用于使用此环境变量
[root@host31 ~]# cat hello.playbook
- hosts: host31
gather_facts: false
tasks:
- name: say hello task
shell: echo ${HOSTNAME} {{ lookup('env','MESSAGE') }} `date` by `hostname` >/tmp/hello.log
[root@host31 ~]#
执行
[root@host31 ~]# ansible-playbook hello.playbook
PLAY [host31] ******************************************************************
TASK [say hello task] **********************************************************
changed: [host31]
PLAY RECAP *********************************************************************
host31 : ok=1 changed=1 unreachable=0 failed=0
[root@host31 ~]#
结果确认:可以看到环境变量MESSAGE的内容已被替换
[root@host31 ~]# cat /tmp/hello.log
host31 hello world information Sun Aug 28 19:52:06 EDT 2016 by host31
[root@host31 ~]#
评论暂时关闭