• 欢迎访问蜷缩的蜗牛博客 蜷缩的蜗牛
  • 微信搜索: 蜷缩的蜗牛 | 联系站长 kbsonlong@qq.com
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

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

Ansible 蜷缩的蜗牛 8个月前 (01-23) 12次浏览 已收录

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

<code class=" hljs rust">定义一个环境变量并<span class="hljs-keyword">export</span>:# <span class="hljs-keyword">export</span> MESSAGE=<span class="hljs-string">"hello world information"</span></code>

准备一个简单的 playbook 用于使用此环境变量

<code class=" hljs perl">[root<span class="hljs-variable">@host31</span> ~]<span class="hljs-comment"># cat hello.playbook</span>
- hosts: host31
  gather_facts: false
  tasks:
    - name:  <span class="hljs-keyword">say</span> hello task
      shell: echo <span class="hljs-variable">${HOSTNAME}</span> {{ lookup(<span class="hljs-string">'env'</span>,<span class="hljs-string">'MESSAGE'</span>) }}  <span class="hljs-string">`date`</span> by <span class="hljs-string">`hostname`</span> ><span class="hljs-regexp">/tmp/hello</span>.<span class="hljs-keyword">log</span>
[root<span class="hljs-variable">@host31</span> ~]<span class="hljs-comment">#</span></code>

执行

<code class=" hljs markdown">[root@host31 ~]# ansible-playbook hello.playbook

PLAY [host31] <span class="hljs-strong">*****</span><span class="hljs-strong">*****</span><span class="hljs-strong">*****</span><span class="hljs-strong">*****</span><span class="hljs-strong">*****</span><span class="hljs-strong">*****</span><span class="hljs-strong">*****</span><span class="hljs-strong">*****</span><span class="hljs-strong">*****</span><span class="hljs-strong">*****</span><span class="hljs-strong">*****</span><span class="hljs-strong">*****</span><span class="hljs-strong">*****</span>*

TASK [say hello task] <span class="hljs-strong">*****</span><span class="hljs-strong">*****</span><span class="hljs-strong">*****</span><span class="hljs-strong">*****</span><span class="hljs-strong">*****</span><span class="hljs-strong">*****</span><span class="hljs-strong">*****</span><span class="hljs-strong">*****</span><span class="hljs-strong">*****</span><span class="hljs-strong">*****</span><span class="hljs-strong">*****</span><span class="hljs-emphasis">***</span>
changed: [host31]

PLAY RECAP <span class="hljs-strong">*****</span><span class="hljs-strong">*****</span><span class="hljs-strong">*****</span><span class="hljs-strong">*****</span><span class="hljs-strong">*****</span><span class="hljs-strong">*****</span><span class="hljs-strong">*****</span><span class="hljs-strong">*****</span><span class="hljs-strong">*****</span><span class="hljs-strong">*****</span><span class="hljs-strong">*****</span><span class="hljs-strong">*****</span><span class="hljs-strong">*****</span><span class="hljs-emphasis">***</span>*
host31                     : ok=1    changed=1    unreachable=0    failed=0

[root@host31 ~]#</code>

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

<code class=" hljs coffeescript">[root<span class="hljs-property">@host31</span> ~]<span class="hljs-comment"># cat /tmp/hello.log</span>
host31 hello world information Sun Aug <span class="hljs-number">28</span> <span class="hljs-number">19</span>:<span class="hljs-number">52</span>:<span class="hljs-number">06</span> EDT <span class="hljs-number">2016</span> <span class="hljs-keyword">by</span> host31
[root<span class="hljs-property">@host31</span> ~]<span class="hljs-comment">#</span></code>

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


蜷缩的蜗牛 , 版权所有丨如未注明 , 均为原创丨 转载请注明实例学习 ansible 系列(16)playbook 中自定义环境变量的使用
喜欢 (0)
[]
分享 (0)