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

实例学习ansible系列(15)playbook的条件和循环

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

Ansible 中有众多的模块,可以写 playbook,同时里面也可以写条件判断和循环,这样基本上脚本能做的事情 ansible 大体都可以作了。条件判断使用 when,循环使用 with_items,接下来看一下如何使用的简单实例。

条件判断 playbook 实例

<code class=" hljs perl">[root<span class="hljs-variable">@host31</span> ~]<span class="hljs-comment"># cat hello.playbook</span>
- hosts: host31
  gather_facts: true
  tasks:
    - name:  <span class="hljs-keyword">say</span> redhat hello task
      shell: echo <span class="hljs-string">"RedHat"</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>
      <span class="hljs-keyword">when</span>:  ansible_os_family ==  <span class="hljs-string">"RedHat"</span>
    - name:  <span class="hljs-keyword">say</span> other linux hello task
      shell: echo <span class="hljs-string">"Not RedHat"</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>
      <span class="hljs-keyword">when</span>:  ansible_os_family !=  <span class="hljs-string">"RedHat"</span>
[root<span class="hljs-variable">@host31</span> ~]<span class="hljs-comment">#</span></code>

事前确认

<code class=" hljs perl">[root<span class="hljs-variable">@host31</span> ~]<span class="hljs-comment"># ll /tmp/hello.log</span>
ls: cannot access /tmp/hello.<span class="hljs-keyword">log</span>: No such file <span class="hljs-keyword">or</span> directory
[root<span class="hljs-variable">@host31</span> ~]<span class="hljs-comment">#</span></code>

执行 playbook

<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 [setup] <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>**
ok: [host31]

TASK [say redhat 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>*
changed: [host31]  -〉此 task 的条件判断 when 条件达成,所以 changed

TASK [say other linux 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>*
skipping: [host31] -〉此 task 的条件判断 when 条件未达成,所以 skip 了

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=2    changed=1    unreachable=0    failed=0

[root@host31 ~]#</code>

结果输出文件确认
[root@host31 ~]# cat /tmp/hello.log
RedHat Sun Jul 31 09:37:08 EDT 2016 by host31
[root@host31 ~]#

循环实例

<code class=" hljs haml">[root@host31 ~]# cat hello.playbook
-<span class="ruby"> <span class="hljs-symbol">hosts:</span> host31
</span>  gather_facts: true
  tasks:
    -<span class="ruby"> <span class="hljs-symbol">name:</span>  say redhat hello task
</span>      shell: echo {{item}}  `date` by `hostname` >> /tmp/hello.log
      with_items:
        -<span class="ruby"> message item1
</span>        -<span class="ruby"> message item2
</span>        -<span class="ruby"> message item3
</span>        -<span class="ruby"> message item4
</span>        -<span class="ruby"> message item5
</span>[root@host31 ~]#</code>

事前确认

<code class=" hljs perl">[root<span class="hljs-variable">@host31</span> ~]<span class="hljs-comment"># ll /tmp/hello.log</span>
ls: cannot access /tmp/hello.<span class="hljs-keyword">log</span>: No such file <span class="hljs-keyword">or</span> directory
[root<span class="hljs-variable">@host31</span> ~]<span class="hljs-comment">#</span></code>

执行 playbook

<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 [setup] <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>**
ok: [host31]

TASK [say redhat 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>*
changed: [host31] => (item=message item1)
changed: [host31] => (item=message item2)
changed: [host31] => (item=message item3)
changed: [host31] => (item=message item4)
changed: [host31] => (item=message item5)

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=2    changed=1    unreachable=0    failed=0

[root@host31 ~]#</code>

结果输出文件确认
[root@host31 ~]# cat /tmp/hello.log
message item1 Sun Jul 31 09:50:10 EDT 2016 by host31
message item2 Sun Jul 31 09:50:10 EDT 2016 by host31
message item3 Sun Jul 31 09:50:10 EDT 2016 by host31
message item4 Sun Jul 31 09:50:11 EDT 2016 by host31
message item5 Sun Jul 31 09:50:11 EDT 2016 by host31
[root@host31 ~]#

本文转载自 实例学习 ansible 系列(15)playbook 的条件和循环


蜷缩的蜗牛 , 版权所有丨如未注明 , 均为原创丨 转载请注明实例学习 ansible 系列(15)playbook 的条件和循环
喜欢 (0)
[]
分享 (0)