ping 命令
#测试单个主机 [root@node1 opt]# ansible -m ping 10.0.0.22 #获取多个主机 [root@node1 opt]# ansible 10.0.0.22,10.0.0.23 -m ping #测试hosts文件中的所有主机 [root@node1 opt]# ansible all -m ping
主机分组,在/etc/ansible/hosts 文件中修改
[web] 10.0.0.22 10.0.0.23 [db] 10.0.0.[23:24] #从23到24
通过分组来查看主机信息
#用来获取符合条件的机器 [root@node1 opt]# ansible web --list-hosts hosts (2): 10.0.0.22 10.0.0.23 #探测组内的机器 [root@node1 opt]# ansible web -m ping #获取web和db的交集 [root@node1 opt]# ansible 'web:&db' -m ping #获取web和db的并集 [root@node1 opt]# ansible 'web:db' -m ping [root@node1 opt]# ansible web,db -m ping #获取web和db的差集,在web中存在但是在db中不存在 [root@node1 opt]# ansible 'web:!db' -m ping
模块
获取模块帮助信息
[root@node1 opt]# ansible-doc -h Usage: ansible-doc [-l|-F|-s] [options] [-t <plugin type> ] [plugin] -j, --json #以json的方式返回所有模块的信息 -l # 列出所有的模块 -s, --snippet # 以片段式显示模块的帮助信息,没有-s显示详细信息 ansible-doc -l |wc -l #统计ansible的模块
command模块
command用户执行远程命令,为默认值,不写 -m command也可执行
chdir #切换目录 creates # 如果存在,就不执行,如果不存在,就执行 removes # 如果不存在,就不执行,如果存在,就执行
获取远程机器的工作目录
[root@node1 opt]# ansible web -m command -a "pwd"
chdir :切换目录,一般在做编译安装时使用
[root@node1 opt]# ansible web -m command -a "chdir=/tmp pwd"
creates:当crestes右边条件成立时,不会被执行,不成立是会被执行
#不会执行,因为/tmp目录已经存在 [root@node1 opt]# ansible web -m command -a "creates=/tmp pwd" #会被执行,因为/tmp2目录不存在 [root@node1 opt]# ansible web -m command -a "creates=/tmp2 pwd"
removes:当removes等号右边成立时,会被执行,不成立是不会执行
#会被执行,因为/tmp目录已经存在 [root@node1 opt]# ansible web -m command -a "removes=/tmp pwd" #不会被执行,因为 /tmp2目录不存在 [root@localhost ~]# ansible web -m command -a "removes=/tmp2 pwd"
shell:执行远程机器的脚本
创建用户并为用户设置 密码
[root@node1 opt]# ansible db -a "useradd wl" [root@node1 opt]# ansible web -m shell -a "echo 'wanglan'|passwd --stdin wl"
执行shell脚本
[root@node1 opt]# ansible 10.0.0.22 -m shell -a "bash /opt/a.sh" [root@node1 opt]# ansible 10.0.0.22 -m shell -a "/opt/a.sh" #a.sh要有执行权限
执行python脚本
[root@node1 opt]#ansible 10.0.0.22 -m shell -a "/root/a.py" [root@node1 opt]#ansible 10.0.0.22 -m shell -a "python a.py" #a.py要有执行权限
script:执行管控机上的脚本
执行本地的脚本,管控机上的脚本
[root@node1 opt]# ansible web -m script -a "/opt/b.sh"
判断远程机器是否存在,如果不存在,就不执行,如果存在,就执行
[root@node1 opt]# ansible db -m script -a "creates=/root/a.sh /root/a.sh"
判断远程机器是否存在,如果不存在,就执行,如果存在,就不执行
[root@node1 opt]# ansible db -m script -a "removes=/root/a.sh /root/a.sh"
copy
参数说明
backup #创建备份文件,以时间戳结尾 content #直接写内容 dest #目标地址 group #文件的属组 mode #文件的权限 owner #文件的属主 src #原文件
复制文件
[root@node1 opt]# ansible web -m copy -a "src=/opt/a.sh dest=/opt/a.sh"
复制并修改权限
[root@node1 opt]# ansible web -m copy -a "src=/opt/a.sh dest=/opt/a.sh mode=755"
复制并修改权限和属主
[root@node1 opt]# ansible web -m copy -a "src=/opt/a.sh dest=/opt/a.sh mode=755 owner=wl"
复制文件夹
[root@node1 opt]# ansible web -m copy -a "src=/etc/init.d dest=/tmp/"
复制文件夹下的所有文件
[root@node1 opt]# ansible web -m copy -a "src=/etc/init.d/ dest=/tmp/"
复制文件夹,并改变文件夹的属性,文件夹的文件的属性也会跟着改变
[root@node1 opt]# ansible web -m copy -a "src=/etc/init.d dest=/tmp/ owner=wl"
直接写文字,覆盖写入,要慎用
[root@node1 opt]# ansible web -m copy -a "content='大弦嘈嘈如急雨,小弦切切如私语' dest=/tmp/a.sh"
备份文件,如果远程机器上有就会备份,没有要备份的文件,即使指定了backup=yes 也不会去备份文件
[root@node1 opt]# ansible wb -m copy -a "src=/root/a.sh dest=/root/a.sh mode=755 owner=alex backup=yes"
file
参数
ccess_time # 访问时间 group # 属组 mode #权限 owner #属主 path #路径 src # 原文件,link和hard的时候使用 state: directory 文件夹 file touch 空文件 link 软连接 hard 硬链接 absent 删除
创建一个目录
[root@node1 opt]# ansible web -m file -a "path=/tmp/wl state=directory"
创建一个目录,并制定属主,权限
[root@node1 opt]# ansible web -m file -a "path=/tmp/wl state=directory owner=wl mode=644"
创建一个文件,并制定属主,权限
[root@node1 opt]# ansible web -m file -a "path=/tmp/wl state=touch owner=wl mode=644"
创建一个软连接,当state=link时,必学要有src
[root@node1 opt]# ansible web -m file -a "path=/tmp/f src=/etc/fstab state=link"
删除
[root@node1 opt]# ansible web -m file -a "path=/tmp/f state=absent"
fetch
fetch将远程机器上的文件拉取到本地,以IP或者主机名生成目录,并保留原来的目录结构
参数
dest #目标地址 src #源地址 [root@node1 opt]# ansible db -m fetch -a "dest=/tmp src=/var/log/cron"
通过ansible以非root用户登录远程机器
[root@node1 opt]# ansible db -a 'pwd' -u baoyuan2 -k k #输入远程机器的密码
yum
系统的yum
yum源的配置
[root@node1 opt]# ll /etc/yum.repos.d/
配置文件详解
[epel] # 名称 name=Extra Packages for Enterprise Linux 7 - $basearch # 描述信息 baseurl=http://mirrors.aliyun.com/epel/7/$basearch # yum源的地址 failovermethod=priority enabled=1 # 指定yum源是否可用,1代表可用,0代表不可用 gpgcheck=0 # 是否检查gpgkey文件,0代表不检查,1代表的检查 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
包组
查看包组
[root@node1 opt]# yum grouplist
其中有一个名为 Development Tools 的开发包组,对其进行安装
[root@node1 opt]# yum groupinstall 'Development Tools' # groupinstall:安装包组
ansible的yum
参数
disable_gpg_check # 是否要检查key disablerepo # 禁用repo enablerepo #启用repo name # 包名 state # 状态 installed removed
使用
ansible db -m yum -a "name=python2-pip" # 安装一个包 ansible db -m yum -a "name='@Development Tools'" # 安装包组 ansible db -m yum -a "name=redis,python2-pip" # 同时安装多个包 ansible db -m yum -a "name=redis state=absent" # 卸载
查看包安装状态
yum list|grep redis #@epel代表安装成功 rpm -q redis #查看包是否安装 rpm -qa #查看所有的包 rpm -ql #查看包安装生成的文件
pip
系统的pip
pip list #查看所有的python第三方包 pip freeze > a.txt # 导出 pip install -r a.txt # 安装
ansible的pip
requirements #导出的文件 name # 包名 virtualenv # 虚拟环境 ansible db -m pip -a "name=django==1.11.18" # 安装 ansible db -m pip -a "name=flask
系统命令
ps -ef|grep redis # 查看进程 ss -tnlp #查看端口信息 # 启动服务 systemctl start redis #centos7 service redis start # centos6 # 开机自启动 systemctl enable redis #centos7 chkconfig redis on #centos6
ansible的service
enabled # 设置开机自启动 name # 名称 state started stoped restarted reloaded ansible web -m service -a "name=redis state=started" # 启动 ansible web -m service -a "name=redis state=stopped" # 关闭 ansible web -m service -a "name=redis state=started enabled=yes" # 启动并设置开机自启动
cron
系统的crontab
* * * * * job 分 时 日 月 周 任务 1 * * * * job # 代表每小时的第一个分钟 2/* * * * * job # 每隔2分钟执行job 1 10-19 * * * job # 代表10到19点的第一分钟 0-59 0-23 1-31 1-12 0-7 job * * * * * tar -zcf /opt/etc.tar.gz /etc 分钟不要用*,最好是指定时间 分钟不要用*,最好是指定时间 分钟不要用*,最好是指定时间 crontab -l # 查看计划任务 crontab -r # 删除所有的计划任务 crontab -e # 编辑计划任务
ansible的cron
day # 天 hour # 小时 job #任务 minute #分钟 month # 月 name #名字,描述信息,不可以重复 state # 状态 user # 执行计划任务的用户 weekday # 周 disabled # 禁止 ansible web -m cron -a "minute=21 job='touch /tmp/cron.txt' name=touchfile" # 设置计划任务 name为描述 ansible web -m cron -a "minute=23 job='touch /tmp/cron.txt' name=touchfile4 disabled=yes" # 禁用计划任务,表现为加注释 ansible web -m cron -a "name=touchfile4 state=absent" # 删除计划任务
user
系统中的user
用户的分类 超级管理员 root 0 其他用户 系统用户 启动服务来专门设置的用户 1-999 centos7 1-499 centos6 登陆用户 普通的登陆用户 1000-65535 centos7 500-65535 centos6 useradd -d # 指定家目录 -g # 组id -G, --groups GROUPS # 附加组 -r, --system # 创建系统用户 -s, --shell SHELL # 登陆shell -u, --uid UID #用户id useradd -s /sbin/nologin -u 2000 -d /opt/wl wl #创建用户,指定用户的登陆shell,id,家目录 useradd -s /sbin/nologin -G root,wusir -d /opt/wl1 wl1#指定附加组,最大的后面+1 useradd -r wl # 创建系统用户,从999倒序 userdel wl1 # 删除用户 userdel -r wl2 # 删除用户并删除用户的家目录
ansible中的user
group # 组 groups #附加组 home #家目录 name #用户名 password #密码 shell #登陆shell remove # 删除用户并删除用户的家目录 state # 状态 system #系统用户 uid # 用户id ansible db -m user -a "name=wl shell=/sbin/nologin home=/opt/alex2 uid=2000 group=root" # 创建用户,并指定用户的家目录,登陆shell,uid,组 ansible db -m user -a "name=wl1 system=yes" #创建系统用户 ansible db -m user -a "name=wl2 state=absent" # 删除用户 ansible db -m user -a "name=wl3 state=absent remove=yes" # 删除用户并删除用户的家目录
group
系统的group
用户组的分类 超级组 root 0 其他组 系统组 1-999 centos7 1-499 centos6 普通组 1000-65535centos7 500-65535 centos6 -g #指定组的id -r #指定系统组 groupadd #添加用户组 groupdel #删除用户组
ansible中的user
gid #组的id name # 组名 state #状态 system #系统组 ansible db -m group -a "name=canglaoshi" #创建普通组 ansible db -m group -a "name=wutenglan system=yes" # 创建系统组 ansible db -m group -a "name=wutenglan state=absent" # 删除组