2 Star 1 Fork 0

zhrun8899 / learning-notes

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
ansible.md 9.43 KB
一键复制 编辑 原始数据 按行查看 历史

ansible

1.安装

yum -y install epel-release.noarch

yum install ansible

2.ssh安全密钥公钥传输

实现无密码登录

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

3.ansible配置

/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]

4.command模块

为ansible默认模块,不指定-m参数时,使用的就是command模块; comand模块比较简单,常见的命令都可以使用,但其命令的执行不是通过shell执行的,所以,像这些 "<", ">", "|", and "&"操作都不可以,当然,也就不支持管道; 缺点:不支持管道,没法批量执行命令;

ping:探测主机是否存活,无参数

ansible all -m ping  

5.shell模块:

使用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模块的使用可以分成两块:

  1. 如果待执行的语句少,可以直接写在一句话中,如上例。

  2. 如果在远程待执行的语句比较多,可写成一个脚本,通过copy模块传到远端,然后再执行;但这样就又涉及到两次ansible调用;对于这种需求,ansible已经为我们考虑到了,script模块就是干这事的;

6.scripts模块

使用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"

7.copy模块:

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" 

8. fetch模块:从受管主机拉取文件到管理机

ansible web -m fetch -a 'src=/etc/fstab dest=/tmp/'

9.file模块:

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'

10.yum模块

yum模块可以提供的status状态: latest ,present,installed #这3个代表安装;removed, absent #后面2个是卸载

ansible dbserver -m yum -a 'name=net-tools state=present'

11.get_url模块

实现远程主机下载指定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"

12.cron模块

远程主机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"'

13.service模块

远程主机系统服务管理。

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'  

14.sysctl模块

远程主机sysctl配置

例:开启路由转发功能

 ansible  web -m sysctl -a "name=net.ipv4.ip_forward value=1 reload=yes"

15.group模块:

ansible web -m group -a 'name=ansi gid=1200 state=present'
ansible web -m group -a 'name=ansi  state=absent'

16.hostname模块

ansible web -m hostname -a "name={{host_name}}"

17.user模块:

#批量添加新用户
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)

18.ansible-console:

控制台式的批量交互执行

#在所有服务器上以交互方式执行命令
ansible-consile

#各种模块的使用帮助,如下是command的模块
ansible-doc command

19.setup模块:

查看系统软硬件信息;

#只查看本发布机的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'

20.hostname模块:

ansible web -m hostname -a 'name={{ hostname }

21.proxy 模块

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动作说明

  • a :新增, a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)~
  • c :取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行!
  • d :删除,因为是删除啊,所以 d 后面通常不接任何咚咚;
  • i :插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行);
  • p :打印,亦即将某个选择的数据印出。通常 p 会与参数 sed -n 一起运行~
  • s :取代,可以直接进行取代的工作哩!通常这个 s 的动作可以搭配正规表示法!例如 1,20s/old/new/g 就是啦!

数据的搜寻并替换

除了整行的处理模式之外, sed 还可以用行为单位进行部分数据的搜寻并取代。基本上 sed 的搜寻与替代的与 vi 相当的类似!他有点像这样:

sed 's/要被取代的字串/新的字串/g'
1
https://gitee.com/zhrun8899/learning-notes.git
git@gitee.com:zhrun8899/learning-notes.git
zhrun8899
learning-notes
learning-notes
master

搜索帮助