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

本文介绍了如何在Ansible playbook中使用when进行条件判断和with_items实现循环。通过实例展示了条件判断playbook的执行过程及结果输出,以及循环执行在hello.log文件中打印不同item信息的操作。
摘要由CSDN通过智能技术生成

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

条件判断playbook实例

[root@host31 ~]# cat hello.playbook
- hosts: host31
  gather_facts: true
  tasks:
    - name:  say redhat hello task
      shell: echo "RedHat"  `date` by `hostname` >> /tmp/hello.log
      when:  ansible_os_family ==  "RedHat"
    - name:  say other linux hello task
      shell: echo "Not RedHat"  `date` by `hostname` >> /tmp/hello.log
      when:  ansible_os_family !=  "RedHat"
[root@host31 ~]#

事前确认

[root@host31 ~]# ll /tmp/hello.log
ls: cannot access /tmp/hello.log: No such file or directo
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值