ansible web -a 'ls /'#第一个命令 ansible web -a 'pwd' ansible web -a 'chdir=/tmp pwd'# 切换目录执行命令,使用场景是编译安装时使用 ansible web -a 'creates=/tmp pwd' # 用来判断/tmp目录是否存在,存在就不执行操作 ansible web -a 'creates=/data pwd' # 因为data不存在,所有才会执行pwd命令 ansible web -a 'removes=/tmp pwd' #用来判断tmp目录是否存在,存在就执行操作 ansible web -a 'removes=/data pwd' #因为data不存在,所有才不会执行
查看用户是否创建成功
1 2 3 4
tail -1 /etc/passwd tail -1 /etc/shadow id tom echo'123' | passwd --stdin tom #设置密码
shell
1 2 3 4 5
ansible web -m shell -a 'echo "123" | passwd --stdin tom' # 批量创建密码 ansible 192.168.107.131 -m shell -a 'bash a.sh' # 执行远程文件方式一 ansible 192.168.107.131 -m shell -a '/root/a.sh' #执行远程文件方式二,文件必须有执行权限 ansible 192.168.107.131 -m shell -a '/root/a.py' # 执行远端的Python脚本
script
1 2 3 4
ansible web -m script -a '/root/m.sh' # 执行本地的文件,执行管控机上的文件 ansible web -m script -a 'removes=/root/m.sh /root/m.sh' # 用来判断被管控机上是不是存在文件,如果存在,存在就执行,不存在就不执行 ansible web -m script -a 'creates=/root/a.sh /root/m.sh' #用来判断被管控机上是不是存在文件,如果存在,就不执行
copy
1 2 3 4 5 6 7 8 9 10 11 12 13 14
backup 备份,以时间戳结尾 dest 目的地址 group 文件的属组 mode 文件的权限 r 4 w 2 x 1 owner 文件的属主 src 源文件 # 通过md5码来判断是否需要复制 ansible db -m copy -a 'src=/root/m.sh dest=/tmp/a.sh' #复制本地文件的到远程主机 ansible db -m copy -a 'src=/root/m.sh dest=/tmp/a.sh mode=755' #修改文件的权限 ansible web -m copy -a 'src=/root/m.sh dest=/tmp/a.sh mode=755 owner=tom' 修改文件的属主 ansible web -m copy -a 'src=/etc/init.d dest=/tmp/ mode=755 owner=tom' # 复制本地目录到远程主机,如果改变文件的属性,则文件夹内的文件也会被改变 ansible web -m copy -a 'src=/etc/init.d/ dest=/tmp/ mode=755 owner=tom' # 复制本地目录内的所有文件到远程主机 ansible web -m copy -a "content='大弦嘈嘈如急雨,小弦切切如私语\n' dest=/tmp/b.txt" # 直接将文本内容注入到远程主机的文件中
ansible db -m file -a 'path=/lzmly2 state=directory' #在远程机器上创建文件夹 ansible db -m file -a 'path=/root/q.txt state=touch' #用来在远程机器上创建文件 ansible db -m file -a 'path=/tmp/f src=/etc/fstab state=link' #创建软连接src是源地址,path是目标地址 ansible db -m file -a 'path=/tmp/f state=absent' #用来删除文件或者文件夹
fetch
1 2 3
dest 目的地址 src 源地址 ansible web -m fetch -a 'src=/var/log/cron dest=/tmp' # 下载被控节点的文件,每台机器创建一个文件夹,并保留原来的目录结构
yum
rpm 和yum 的区别
rpm:redhat package manager yum可以解决依赖关系
yum 源配置
[epel]
name=Extra Packages for Enterprise Linux 7 - $basearch #名字
baseurl=http://mirrors.aliyun.com/epel/7/$basearch #rpm源的地址,可以写http,https,ftp,Samba,file:
failovermethod=priority
enabled=1 # 是否开启,1代表开启,0表示关闭
gpgcheck=0 #是否校验签名,1代表校验,0表示校验
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
disablerepo #禁用源 enablerepo #启用源 name #包名 state install (`present' or `installed', `latest'), or remove (`absent' or `removed')
1 2 3 4
ansible web -m yum -a 'name=wget' # 安装wget ansible web -m yum -a 'name=python2-pip' # 安装python2-pip ansible web -m yum -a 'name=wget state=absent' # 卸载软件包 ansible web -m yum -a 'name="@Development Tools"' # 安装包组
group 组 groups 附加组 home 家目录 name 用户名 password 密码 remove ? shell 用户登录后使用的shell system 创建一个系统用户 uid 用来指定用户的id state 状态 ansible db -m user -a 'name=canglaoshi uid=4000 home=/opt/canglaoshi groups=root shell=/sbin/nologin' #创建一个用户,并指定用户的id,用户的家目录,用户的附加组,用户的shell ansible db -m user -a 'name=canglaoshi state=absent' #删除用户但是不删除用户的家目录 ansible db -m user -a 'name=canglaoshi3 state=absent remove=yes' # 删除用户并删除用户的家目录
group
1 2 3 4 5 6
gid 组的id name 组名 system 系统组 state ansible db -m group -a 'name=canglaoshi system=yes' #创建系统组 ansible db -m group -a 'name=canglaoshi state=absent' # 删除组
web
创建一个用户组tom10
1
ansible web -m group -a 'name=tom10'
创建一个用户kitty10
1
ansible web -m user -a 'name=kitty10'
把/etc/fstab文件复制到远程主机上/tmp/f
1
ansible web -m copy -a 'src=/etc/fstab dest=/tmp/f'
安装nginx,并启动,设置开机自启动
1 2 3
ansible web -m yum -a 'name=nginx'
ansible web -m service -a 'name=nginx enabled=yes'
常见问题
从机拒绝访问
报错如下:
1
/usr/bin/ssh-copy-id: ERROR: ssh: connect to host 172.17.0.2 port 22: Connection refused
此时因为从机没有开启ssh导致的,从机可以执行以下操作
1 2 3
/usr/sbin/sshd-keygen -A # 手动执行
/usr/sbin/sshd # 再执行
从机测试ssh服务
1
ssh localhost
问题解决。如果提示输入密码就先设置一个密码。
1
ssh-keygen # 用来生成ssh的密钥对
从机无法安装软件
报错如下:
1
Failed to get D-Bus connection: Operation not permitted