Ansible运维手册

条件与循环

如果系统为redhat则重启

tasks:
  - name: reboot redhat host
    command: /usr/sbin/reboot
    when: ansible_os_family == "RedHat"

如果result为成功状态则重启

tasks:
  - command: /bin/false
    register: result
    ignore_errors: True

  - command: /usr/sbin/reboot
    when: result|success

循环语句基础

tasks:
  - name: install LNMP
    yum: name={{ item }} state=present
    with_items:
       - nginx
       - mysql-server
       - php-fpm

循环还支持列表,使用with_flattened语句。 变量文件

---
packages_LNMP:
  - [ 'nginx', 'mysql-server', 'php-fpm' ]

引用

- name: Install LNMP
  yum: name={{ item }} state=present
  with_flattened:
     - packages_LNMP