## Ansible使用笔记 (1)安装及配置 * 软件安装:只需要在管理端安装:yum install -y ansible * 秘钥配置:私钥放在管理端服务器,公钥放在被管理端服务器,若有多台被管理服务器则都需放置,使ssh能直接连接 * 配置:把被管理端的IP放入到hosts (2)连接和测试 * ansible 47.106.123.191 -m command -a 'df -h',运行正确则说明ansible配置完成 * ping全部被管理机:ansible all -m ping * ping测试[or]组 ``` [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" ``` * 控制被管理机yum安装软件(示例中为安装npm) ``` ansible 118.31.62.194 -m yum -a "name=npm state=latest" ``` * 配置crontab ``` 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 ``` >[info] Playbook的使用 ``` --- - hosts: aliyun remote_user: root tasks: - name: install git yum: name=git state=latest - name: clone lnmp shell: "cd /home;git clone https://linzening:default123@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" ... ``` + 运行:`ansible-book xxx.yml` * 远程主机系统服务管理 >[success] 参考:http://www.cnblogs.com/wangxiaoqiangs/p/5685239.html > Date:2018-09-20