实例学习ansible系列(16)playbook中自定义环境变量的使用

在前面的文章中我们使用过环境变量, 但是用户自定义或者随时export出来的环境变量却无法直接引用,而这种方式却是实际工作中用到的最多的情况,使用ansible提供的lookup便可解决这个问题.

定义一个环境变量并export:# export MESSAGE="hello world information"
  • 1

准备一个简单的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 ~]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

执行

[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 ~]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

结果确认:可以看到环境变量MESSAGE的内容已被替换

[root@host31 ~]# cat /tmp/hello.log
host31 hello world information Sun Aug 28 19:52:06 EDT 2016 by host31
[root@host31 ~]#
  • 1
  • 2
  • 3

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://www.cnblogs.com/captainbed

猜你喜欢

转载自www.cnblogs.com/firsttry/p/10166981.html