ansible 目标主机 [-f N][-m module_name] [-a args]
案例:在每个主机上执行free -m命令
[root@centos6-1 ~]# ansible myserver -m command -a "free -m"
注意1:因为默认为command模块,可以省略
注意2:指定目标主机可以用all表示,all表示主机清单里的全部主机和主机组
[root@centos6-1 ~]# ansible myserver -a "free -m"
[root@centos6-1 ~]# ansible all -a "free -m"
案例:在所有被管理节点上创建用户tom,然后修改tom密码为123
[root@centos6-1 ~]# ansible all -m shell -a "echo 123 | passwd --stdin tom"
案例:在所有被管理节点创建用户jerry,密码设为123
[root@centos6-1 ~]# ansible all -m user -a "name=jerry password=123" [root@centos6-1 ~]# ansible all -m shell -a "tail -n1 /etc/passwd"
案例:将/tmp/a.txt分发到所有被管理节点的/home下,复制后,将权限改为700
[root@centos6-1 ~]# ansible all -m copy -a "src=/tmp/a.txt dest=/ mode=700"
案例:在每个被管理主机的tmp下创建空文件abc.txt
方法1 [root@centos6-1 ~]# ansible all -m file -a "name=/tmp/abc.txt state=touch" 方法2 [root@centos6-1 ~]# ansible all -m file -a "path=/tmp/abc.txt state=touch"
案例:检查被管理节点是否在线
[root@centos6-1 ~]# ansible all -m ping
案例:重启所有被管理节点
方法1: [root@centos6-1 ~]# ansible all -m reboot 方法2: [root@centos6-1 ~]# ansible all -m shell -a "reboot"
案例:在所有被管理节点安装zsh
[root@centos6-1 ~]# ansible all -m yum -a "name=zsh state=present [root@centos6-1 ~]# ansible all -m shell -a "rpm -q zsh"
案例:在所有被管理主机上安装apache
[root@centos6-1 ~]# ansible all -m yum -a "name=httpd state=present"
案例:在被管理节点的/home、/tmp、/root分别创建一个文件,文件名 时间.txt
oot@centos6-1 ~]# cat my.sh #!/bin/bash for dir in /home /tmp /root do touch $dir/`date +%F-%T`.txt done [root@centos6-1 ~]# chmod +x my.sh [root@centos6-1 ~]# ansible all -m script -a "/root/my.sh"
案例:将nginx的源码包解压到被管理节点的home下
[root@centos6-1 ~]# ansible all -m unarchive -a "src=/root/nginx-1.16.1.tar.gz dest=/home/"
案例:每隔2分钟在/tmp/a.txt中写入一行数据
[root@centos6-1 .ssh]# ansible all -m cron -a "name=mycron state=present job=‘echo 123>>/tmp/a.txt‘ minute=*/2"
原文:https://www.cnblogs.com/wendyluo/p/13178846.html