ansible
yum -y install epel-release.noarch
yum install ansible
实现无密码登录
ansi服务器上执行:
ssh-keygen -t rsa
ssh-copy-id root@192.168.9.61
ssh-copy-id root@192.168.9.64
ssh-copy-id root@192.168.9.52
ssh-copy-id root@192.168.9.53
ssh-copy-id root@192.168.9.65
/etc/ansible/ansibles.cfg
host_key_checking = False
主机列表:/etc/ansible/hosts
[adminserver]
192.168.9.111
[dbserver]
192.168.9.114
192.168.9.144
192.168.9.174
[appserver]
192.168.9.113
[webservers]
为ansible默认模块,不指定-m参数时,使用的就是command模块; comand模块比较简单,常见的命令都可以使用,但其命令的执行不是通过shell执行的,所以,像这些 "<", ">", "|", and "&"操作都不可以,当然,也就不支持管道; 缺点:不支持管道,没法批量执行命令;
ping:探测主机是否存活,无参数
ansible all -m ping
使用shell模块,在远程命令通过/bin/sh来执行;所以,我们在终端输入的各种命令方式,都可以使用。
例1:运行free -m 命令
[root@saltMaster ~]# ansible webservers -m shell -a "free -m"
sed -i '$a proxy=http://webimation:elinkD18@192.168.9.111:3128/ ' /etc/yum.conf
对shell模块的使用可以分成两块:
如果待执行的语句少,可以直接写在一句话中,如上例。
如果在远程待执行的语句比较多,可写成一个脚本,通过copy模块传到远端,然后再执行;但这样就又涉及到两次ansible调用;对于这种需求,ansible已经为我们考虑到了,script模块就是干这事的;
使用scripts模块可以在本地写一个脚本,在远程服务器上执行:
[root@saltMaster ~]# vim /etc/ansible/net.sh
sed -i '$a proxy=http://webimation:elinkD18@192.168.9.111:3128/ ' /etc/yum.conf
[root@saltMaster ~]# ansible web -m script -a "addProxy.sh"
通过ansible设置squid代理:
cat addProxy.sh
#!/bin/bash
## delete old yum proxy config
sed -i '/^proxy=/d' /etc/yum.conf
sed -i '$a proxy=http://webimation:elinkD18@192.168.9.4:3128/ ' /etc/yum.conf
# delete old http proxy config
sed -i '/^export http/d' /etc/profile
sed -i '$a export http_proxy=http://192.168.9.4:3128/ ' /etc/profile
sed -i '$a export https_proxy=http://192.168.9.4:3128/ ' /etc/profile
source /etc/profile
# ntp config
#---------------------------------------------------
sed -i '/^server 172.30/d' /etc/ntp.conf
sed -i '$a server 192.168.9.4' /etc/ntp.conf
ansible temp -m scripts -a "addProxy-120.sh"
copy模块:实现主控端向目标主机拷贝文件,类似scp功能
ansible web -m copy -a 'src=/root/software/nginx-1.16.1-1.el7.ngx.x86_64.rpm dest=/tmp/ backup=yes mode=0777 owner=root group=root'
ansible webservers -m copy -a "src=/etc/hosts dest=/tmp/ owner=root group=root mode=0755"
ansible web -m fetch -a 'src=/etc/fstab dest=/tmp/'
ansible db -m file -a 'src=/tmp/fstab dest=/tmp/fstab.link state=link'
#批量创建目录:state是directory
ansible web -m file -a 'path=/tmp/tomtest/ state=directory mode=0755'
#批量创建文件: state=touch
[root@root localhost]#ansible web -m file -a 'path=/tmp/ansible mode=0644 state=touch'
yum模块可以提供的status状态: latest ,present,installed #这3个代表安装;removed, absent #后面2个是卸载
ansible dbserver -m yum -a 'name=net-tools state=present'
实现远程主机下载指定url到本地,支持sha256sum文件校验。
ansible web -m get_url -a "url=https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm dest=/tmp/ mode=0440 force=yes"
远程主机crontab配置。
例如:增加每30分钟执行ls /tmp
ansible -i /etc/ansible/hosts web-servers -m cron -a "name='list dir' minute='*/30' job='ls /tmp'"
ansible web -m cron -a 'name=daycron minute="0" hour="1" weekday="5" job="backupmysql.sh >>/tmp/a.log"'
远程主机系统服务管理。
service模块常用参数:
(1)、name参数:此参数用于指定需要操作的服务名称,比如 nginx,httpd。
(2)、state参数:此参数用于指定服务的状态,比如,我们想要启动远程主机中的httpd,则可以将 state 的值设置为 started;如果想要停止远程主机中的服务,则可以将 state 的值设置为 stopped。此参数的可用值有 started、stopped、restarted(重启)、reloaded。
enabled参数:此参数用于指定是否将服务设置为开机 启动项,设置为 yes 表示将对应服务设置为开机启动,设置为 no 表示不会开机启动。
注:想使用service模块启动服务,被启动的服务,必须可以使用service 命令启动或关闭
ansible web -m service -a "name=nginx state=started"
ansible web -m service -a "name=nginx state=stopped"
#检查是否启动
ansible web -m shell -a 'ps aux|grep nginx'
远程主机sysctl配置
例:开启路由转发功能
ansible web -m sysctl -a "name=net.ipv4.ip_forward value=1 reload=yes"
ansible web -m group -a 'name=ansi gid=1200 state=present'
ansible web -m group -a 'name=ansi state=absent'
ansible web -m hostname -a "name={{host_name}}"
#批量添加新用户
ansible web -m user -a 'name=wang comment=wang uid=1100 group=wang'
[root@~ localhost]#id wang
uid=1100(wang) gid=1000(wang) groups=1000(wang)
控制台式的批量交互执行
#在所有服务器上以交互方式执行命令
ansible-consile
#各种模块的使用帮助,如下是command的模块
ansible-doc command
查看系统软硬件信息;
#只查看本发布机的CPU核数:
ansible web -m setup |grep vcpus
group模块:
#添加是present,删除是absent
- state
Whether the group should be present or not on the remote hos
(Choices: present, absent)[Default: present]
[root@root localhost]#ansible web -m group -a 'name=ansi gid=1200 state=present'
ansible web -m hostname -a 'name={{ hostname }
rpm 安装nginx
ansible proxy -m yum -a "name=/usr/local/src/nginx-release-centos-7-0.el6.ngx.noarch.rpm state=present"
远程安装
ansible proxy -m yum -a "name=http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm state=present"
情景:在App组中启动192.168.37.15的NTP服务。
ansible app -m command -a "service ntpd status" --limit "192.168.37.158"
用“:”作分隔符,指定多台机器做变更。 情景:启动192.168.37.158和192.168.37.161的NTP服务。 执行命令:
ansible "192.168.37.158:192.168.37.161" -m command -a "service ntpd status"
维护脚本:addProxy-120.sh
#!/bin/bash
#squid config
#---------------------------------------------------
## delete old yum proxy config
sed -i '/^export proxy=/d' /etc/yum.conf
sed -i '$a proxy=http://webimation:elinkD18@192.168.9.4:3128/ ' /etc/yum.conf
# delete old http proxy config
sed -i '/^export http/d' /etc/profile
sed -i '$a export http_proxy=http://192.168.9.4:3128/ ' /etc/profile
sed -i '$a export https_proxy=http://192.168.9.4:3128/ ' /etc/profile
source /etc/profile
#---------------------------------------------------
# ntp config
#---------------------------------------------------
sed -i '/^server 172.30/d' /etc/ntp.conf
sed -i '$a 'server 192.168.9.4' /etc/ntp.conf
ansible 需要配合脚本使用,如sed,awk等.
if [ ! -f "/opt/jumpserver/config.yml" ]; then
cp /opt/jumpserver/config_example.yml /opt/jumpserver/config.yml;
sed -i "s/SECRET_KEY:/SECRET_KEY: $SECRET_KEY/g" /opt/jumpserver/config.yml;
sed -i "s/BOOTSTRAP_TOKEN:/BOOTSTRAP_TOKEN: $BOOTSTRAP_TOKEN/g" /opt/jumpserver/config.yml;
sed -i "s/# DEBUG: true/DEBUG: false/g" /opt/jumpserver/config.yml;
sed -i "s/# LOG_LEVEL: DEBUG/LOG_LEVEL: ERROR/g" /opt/jumpserver/config.yml;
sed -i "s/# SESSION_EXPIRE_AT_BROWSER_CLOSE: false/SESSION_EXPIRE_AT_BROWSER_CLOSE: true/g" /opt/jumpserver/config.yml;
sed -i "s/DB_PASSWORD: /DB_PASSWORD: $DB_PASSWORD/g" /opt/jumpserver/config.yml;
fi
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
sed动作说明:
除了整行的处理模式之外, sed 还可以用行为单位进行部分数据的搜寻并取代。基本上 sed 的搜寻与替代的与 vi 相当的类似!他有点像这样:
sed 's/要被取代的字串/新的字串/g'
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。