目录python
一、安装epel源linux
yum install -y wget # 安装wget wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo # 下载epel源文件
二、安装ansibleweb
yum install ansible -y
Usage: ansible <host-pattern> [options] -a MODULE_ARGS, --args=MODULE_ARGS # 模块的参数 -C, --check # 会去执行,可是不作任何的改变,干跑,白跑 -f FORKS, --forks=FORKS # 指定进程数,作并发 --list-hosts #列出主机 -m MODULE_NAME # 模块名称 --syntax-check #检查语法 -k, --ask-pass ask for connection password #指定密码
rpm -ql ansible /etc/ansible /etc/ansible/ansible.cfg /etc/ansible/hosts /etc/ansible/roles 10.0.0.132 10.0.0.133 10.0.0.134
密码redis
秘钥shell
ssh-keygen # 生成秘钥 ssh-copy-id root@10.0.0.132 # 将秘钥文件复制到远程主机
ansible 10.0.0.132 -m ping ansible 10.0.0.133 -m ping ansible 10.0.0.134 -m ping ansible all -m ping # 全部机器, hosts文件里面 ansible 10.0.0.133,10.0.0.132 -m ping # 部分机器 ## 分组信息 [web] 10.0.0.132 10.0.0.133 [db] 10.0.0.133 10.0.0.134 [cache] 10.0.0.134 ## www[001:006].example.com 从www001到www006 ansible web --list-hosts # 用来获取符合条件的主机 ansible web -m ping # 探测组内的机器 ansible web,db -m ping # 获取db和web的并集 ansible 'web:&db' -m ping # 获取db和web的交集 ansible 'web:!db' -m ping # 获取db和web的差集,在web中可是不在db中的 ansible 'web:db' -m ping # 获取db和web的并集
密码要符合的规则django
单个的主机json
ansible 127.0.0.1 -m .. 逗号分隔
单个组flask
ansible 组名 -m ...
差集windows
全部的机器 allcentos
多个主机
Usage: ansible-doc [-l|-F|-s] [options] [-t <plugin type> ] [plugin] -j, --json #以json的方式返回全部模块的信息 -l # 列出全部的模块 -s, --snippet # 以片断式显示模块的帮助信息 # 显示所有信息 ansible-doc -l |wc -l #统计ansible的模块
执行远程主机上的命令, 不特殊字符支持
chdir #切换目录,通常在编译安装 creates # 判断是否存在,若是存在,就不执行,若是不存在,就执行 removes # 若是不存在,就不执行,若是存在,就执行 ansible web -m command -a "pwd" ansible web -m command -a "ls /tmp" ansible web -m command -a "chdir=/tmp pwd" # 切换目录,通常作编译安装 ansible web -m command -a "creates=/tmp pwd" # 不被执行,由于/tmp已经存在, ansible web -m command -a "creates=/tmp2 pwd" # 被执行,由于/tmp2目录不存在 ansible web -m command -a "creates=/tmp2 mkdir /data" # 会被执行,由于/tmp2目录不存在 ansible web -m command -a "removes=/tmp2 pwd" # 不被执行,由于/tmp2目录不存在 ansible web -m command -a "removes=/tmp pwd" # 会被执行,由于/tmp已经存在,
补充
#查看用户建立成功与否 1.ll /home 2.tail -1 /etc/passwd 3.tail /etc/shadow 4.id alex echo "alex3714" |passwd --stdin alex # 给用户设置密码,不须要二次确认 [root@localhost ~]# name=alex [root@localhost ~]# echo "$name" alex [root@localhost ~]# echo '$name' $name shabang
执行远程主机的命令或脚本
ansible web -m shell -a "echo 'alex'|passwd --stdin alex" # 给用户设置密码 ansible 10.0.0.132 -m shell -a "bash a.sh" # 执行shell脚本 ansible 10.0.0.132 -m shell -a "./a.sh" ansible 10.0.0.132 -m shell -a "/root/a.sh" ansible 10.0.0.132 -m shell -a "/root/a.py" # 执行python脚本 ansible 10.0.0.132 -m shell -a "python a.py" # shell 脚本 #!/bin/bash mkdir /alex2sb11 # python脚本 #!/bin/env python #coding:utf-8 print "停车坐爱枫林晚,霜叶红于二月花"
执行本机的脚本
ansible db -m script -a "/root/a.sh" # 执行的是本地的脚本,管控机上的脚本 ansible db -m script -a "creates=/root/a.sh /root/a.sh" # 判断是远程主机是否存在,若是存在,就不执行,若是不存在,就执行 ansible db -m script -a "removes=/root/a.sh /root/a.sh" # 判断的主机是否存在,若是存在,就执行,若是不存在,就不执行
将本地的文件复制到远程主机上
backup # 建立备份文件,以时间戳结尾, content # 直接写内容 dest # 目标地址 group #文件的属组 mode # 文件的权限W 2 R 4 X 1 owner #文件的属主 src # 原文件 ansible db -m copy -a "src=/root/a.sh dest=/root/a.sh" # 复制文件 ansible db -m copy -a "src=/root/a.sh dest=/root/a.sh mode=755" # 复制文件,并修改文件的权限 ansible db -m copy -a "src=/root/a.sh dest=/root/a.sh mode=755 owner=alex" #复制文件,修改文件的权限,属主,根据md5值来判断 ansible db -m copy -a "src=/etc/init.d dest=/tmp/" # 复制文件夹 ansible db -m copy -a "src=/etc/init.d/ dest=/tmp/" # 复制文件夹下面的全部文件 ansible db -m copy -a "src=/etc/init.d dest=/tmp/ owner=alex " # 复制文件夹,并改变文件夹的属性,文件夹的文件的属性也会跟着改变 ansible db -m copy -a "content='大弦嘈嘈如急雨,小弦切切如私语' dest=/tmp/a.sh" # 直接写文字,覆盖写入,要慎用 ansible db -m copy -a "src=/root/a.sh dest=/root/a.sh mode=755 owner=alex backup=yes" #备份文件,若是远程机器上没有要备份的文件,即便指定了backup=yes 也不会去备份文件
在远程主机上建立文件夹, 文件, 软链接, 硬连接
access_time # 访问事件 group # 属组 mode #权限 owner #属主 path #路径 src # 原文件,link和hard的时候使用 state: directory 文件夹 file touch 空文件 link 软链接 hard 硬连接 absent 删除 ansible db -m file -a "path=/tmp/baoyuan state=directory" # 建立一个目录 *-
88
ansible db -m file -a "path=/tmp/baoyuan.txt state=touch owner=alex mode=644" # 建立一个文件,并制定属主,权限
ansible db -m file -a "path=/tmp/f src=/etc/fstab state=link" # 建立一个软链接
ansible db -m file -a "path=/tmp/f state=absent" # 删除
补充
软链接 windows的快捷方式 ln —s 原文件 目标文件 源文件改变,目标文件也改变 能够跨越分区 原文件删除,连接失效 硬连接 指向同一个硬盘的地址 ln 原文件 目标文件 原文件改变,目标文件也改变 不能够跨域分区 原文件删除,不会受影响
将远程机器上的文件拉取到本地, 以ip或者主机名生成目录,并保结构留原来的目录
dest #目标地址 src #源地址 ansible web -m fetch -a "dest=/tmp src=/var/log/cron"
1.yum 和rpm的区别
rpm redhat package manage yum会解决依赖关系
2.yum源的配置
[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
3.包组
linux lib windows dll 、 rpc 远程过程调用 yum grouplist #查看包组 yum groupinstall # 安装包组rpm
4.查看包安装状态
yum list|grep redis @表明安装成功 rpm -q redis rpm -qa 查看全部的包 rpm -ql 查看包安装生成的文件
.
disable_gpg_check # 是否要检,查key disablerepo # 禁用repo enab name # 包名 state # 状态 installed removed ansible web -m yum -a "name=python2-pip" # 安装一个包 ansible web -m yum -a "name='@Development Tools'" # 安装包组 ansible web -m yum -a "name=redis,python2-pip" # 同时安装多个包 ansible web -m yum -a "name=redis state=absent" # 卸载
pip list #查看全部的python第三方包 pip freeze > a.txt # 导出pi pip install -r a.txt # 安装
.
requirements #导出的文件 name # 包名 virtualenv # 虚拟环境 ansible web -m pip -a "name=django==1.11.18" # 安装 ansible web -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
.
enabled # 设置开机自启动 name # 名称" state startedansi stopped 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" # 启动并设置开机自启动
* * * * * 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 # 编辑计划任务
.
day # 天 hour # 小时 job #任务 minute #分钟 month # 月 name #名字,描述信息,不能够重复 state # 状态 user # 执行计划任务的用户 weekday # 周 disabled # 禁止 ansible web -m cron -a "minute=21 job='touch /tmp/cron.txt' name=touchfile" # 设置计划任务 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" # 删除计划任务
用户的分类 超级管理员 root 0 其余用户 系统用户 启动服务来专门设置的用户 1-999 centos7 1-499 centos6 登录用户 普通的登录用户 1000-65535centos7 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/wusir wusir #建立用户,指定用户的登录shell,id,家目录 useradd -s /sbin/nologin -G root,wusir -d /opt/wusir2 wusir2 #指定附加组,最大的后面+1 useradd -r baoyuan # 建立系统用户,从999倒序 userdel wusir2 # 删除用户 userdel -r baoyuan4 # 删除用户并删除用户的家目录
.
group # 组 groups #附加组 home #家目录 name #用户名 password #密码 shell #登录shell remove # 删除用户并删除用户的家目录 state # 状态 system #系统用户 uid # 用户id ansible db -m user -a "name=alex2 shell=/sbin/nologin home=/opt/alex2 uid=2000 group=root" # 建立用户,并指定用户的家目录,登录shell,uid,组 ansible db -m user -a "name=alex3 system=yes" #建立系统用户 ansible db -m user -a "name=alex3 state=absent" # 删除用户 ansible db -m user -a "name=alex2 state=absent remove=yes" # 删除用户并删除用户的家目录
用户组的分类 超级组 root 0 其余组 系统组 1-999 centos7 1-499 centos6 普通组 1000-65535centos7 500-65535 centos6 groupadd -g 指定组的id -r 指定系统组 groupdel
.
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" # 删除组