Ansible简单使用


Ansible使用笔记

(1)安装及配置

(2)连接和测试

[root@xinxi-server ansible]# ansible or -m ping
47.106.123.191 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
120.79.83.150 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
118.31.62.194 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

(3)远程命令模块

ansible Client -m command -a "free -m"
ansible Client -m script -a "/home/test.sh 12 34"
ansible Client -m copy -a "src=/home/test.sh dest=/tmp/ owner=root group=root mode=0755"
ansible 118.31.62.194 -m stat -a "path=/home/test.sh"
ansible 118.31.62.194 -m get_url -a "url=http://soft.vpser.net/lnmp/lnmp1.5.tar.gz dest=/home mode=0755 force=yes"
ansible 118.31.62.194 -m yum -a "name=npm state=latest"
ansible Client -m cron -a "name='check dirs' hour='5,2' job='ls / > /home/test.txt'"

在被管理机:

[root@iz tmp]# crontab -l
#Ansible: check dirs
* 5,2 * * * ls / > /home/test.txt

Playbook的使用

---
- hosts: aliyun
  remote_user: root
  tasks:
    - name: install git
      yum: name=git state=latest
    - name: clone lnmp
      shell: "cd /home;git clone https://user123:pass123@gitee.com/linzening/lnmp"
    - name: install lnmp
      shell: "cd /home/lnmp/;bash install.sh lnmp"
    - name: install redis
      shell: "cd /home/lnmp/;bash addons.sh 5"
    - name: install mysql
      shell: "cd /home/lnmp/tools;bash rpm_mysql8_0_12.sh"
    - name: install myweb
      shell: "cd /home/lnmp/tools;bash deploy_myweb.sh"
...

参考:http://www.cnblogs.com/wangxiaoqiangs/p/5685239.html

Date:2018-09-20于花果园