Ansible 配置文件 :

[root@center /data/ansiblework]# cat ansible.cfg 
[defaults]
remote_user = root
remote_port = 22
inventory = /data/ansiblework/hosts
log_path = /var/log/ansible.log
host_key_checking = False
retry_files_enabled = False

Ansible 主机配置 :

[root@center /data/ansiblework]# cat hosts 
[new_hosts]
192.168.1.1
192.168.1.2
192.168.1.3

[new_hosts:vars]
ansible_ssh_port = 22        # 远程连接端口
ansible_ssh_user = root      # 远程连接用户
ansible_ssh_pass = 123456    # 远程连接密码

Ansible Playbook :

[root@center /data/ansiblework]# cat onekey_init.yml 
---
- hosts: new_hosts
  gather_facts: True
  tasks:
    - name: 下发公钥到新主机
      authorized_key: user=root key="{{ lookup('file', '/root/.ssh/id_rsa.pub') }}"

    - name: 检查系统版本
      fail: msg="系统版本错误,请检查"
      when: ansible_facts['distribution'] != "CentOS" or ansible_facts['distribution_major_version'] != "7"

    - name: 检查是否挂载/data目录
      fail: msg="/data目录未挂载,请检查"
      when: not ansible_mounts[1].mount == "/data"

    - name: 检查/data目录是否挂载异常
      fail: msg="/data目录小于250G,请检查"
      when: ansible_mounts[1].size_total | int < 250000000000

    - name: 拷贝初始化脚本
      template: src={{ item }} dest=/tmp/{{ item }} owner=root group=root
      with_items:
        - base_lnmp.sh
        - lnmp_install.sh

    - name: 检查是否安装过LNMP
      shell: if [ -f /tmp/install.log ];then grep 'Install Complete' /tmp/install.log;fi || echo None
      register: result

    - name: 开始安装LNMP
      shell: /bin/sh /tmp/lnmp_install.sh > /tmp/install.log
      when: "'Install Complete' not in result.stdout"

    - name: 检查是否安装按成
      shell: if [ -f /tmp/install.log ];then grep 'Install Complete' /tmp/install.log;fi || echo None
      register: result
    - fail: msg="安装失败,请登录到安装机器查看/tmp/install.log查看原因"
      when: "'Install Complete' not in result.stdout"

    - name: 检查LNMP安装状态
      shell: ps aux | egrep "(php|nginx|mysql|zabbix|salt)"
      register: result
    - fail: msg="{{ item }}安装异常,请检查"
      when: "item not in result.stdout"
      with_items:
        - php
        - mysql
        - nginx
        - zabbix
        - salt-minion