Linux自动化运维工具Ansible 以及playbook安装配置使用

Ansible

ansible 安装

#ubantu使用
apt install ansible -y
#centos使用
yum install ansible -y

ansible 配置

#编辑配置文件在最后进行配置(见下面代码框)
vim /etc/ansible/hosts
#下面代码框,第一个[]代表的是分组名,这里分组名为testgroup,然后下面的是分组内的机器ip地址
#修改好保存退出
# Here's another example of host ranges, this time there are no
# leading 0s:
#db-[99:101]-node.example.com
[testgroup]
192.168.108.130

ansible配置主控对节点的免密连接

http://baijiahao.baidu.com/s?id=1648175338751747914&wfr=spider&for=pc

#在主控生成一个RSA密钥对
#然后在输入里键入你要保存rsa密钥的地方,例如 ~/.ssh/id_rsa
root@ubuntu:~# ssh-keygen -t rsa -b 4096
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
#然后让你输入密码,直接不输入按两次回车即可
#然后将公钥复制到节点  root是账号名 192.168.108.130是IP地址
ssh-copy-id [email protected]
#然后输入密码即可

测试ansible是否能连接到节点

#正常
root@ubuntu:~# ansible all -m ping
192.168.108.130 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

节点网络检查 ping模块

#测试192.168.108.130,显示网络正常
root@ubuntu:~# ansible 192.168.108.130 -m ping
192.168.108.130 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

复制文件功能 module copy

要覆盖参数里加 force=yes,其他参数根据自己需求使用即可

ansible 192.168.108.130 -m copy -a ‘src=/root/hello.txt dest=/root force=true’

  • 复制文件
#首先我们在主目录下创建一个hello.txt并写入一段话
root@ubuntu:~# echo 'hello,ansible' > hello.txt
root@ubuntu:~# cat hello.txt 
hello,ansible
#然后复制到节点上去,如果要复制到所有节点则吧192.168.108.130改为all
#解释下参数的意义, -m  module指定用哪个模块  -a args指定参数
root@ubuntu:~# ansible 192.168.108.130 -m copy -a 'src=/root/hello.txt dest=/root'
192.168.108.130 | SUCCESS => {
    "changed": true, 
    "checksum": "df800445bb74b4abb144b3f9bf03f90aa9618f4c", 
    "dest": "/root/hello.txt", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "f61d358bbdd6a9bd2e93322023a4e29d", 
    "mode": "0644", 
    "owner": "root", 
    "size": 14, 
    "src": "/root/.ansible/tmp/ansible-tmp-1606447443.42-209416966266640/source", 
    "state": "file", 
    "uid": 0
}
#然后我们取节点上取看是不是真的复制过来了,可以看到的确复制过来了
root@ubuntu:~# ls
hello.txt
root@ubuntu:~# cat hello.txt 
hello,ansible
  • 复制目录
#首先准备一些目录和文件,两层,每层放一个
root@ubuntu:~# ls
hello.txt
root@ubuntu:~# mkdir hello_dir
root@ubuntu:~# cd hello_dir/
root@ubuntu:~/hello_dir# ls
root@ubuntu:~/hello_dir# echo 'hello' > hello.txt
root@ubuntu:~/hello_dir# ls
hello.txt
root@ubuntu:~/hello_dir# mkdir hello_level2
root@ubuntu:~/hello_dir# ls
hello_level2  hello.txt
root@ubuntu:~/hello_dir# cd hello_level2/
root@ubuntu:~/hello_dir/hello_level2# echo 'hello2' > hello2.txt
root@ubuntu:~/hello_dir/hello_level2# ls
hello2.txt
root@ubuntu:~/hello_dir/hello_level2# cd 
root@ubuntu:~# ls
hello_dir  hello.txt
#复制hello_dir整个文件夹及子文件夹的所有文件到节点的/root目录下,如果只需要当前文件夹下的所有文件在文件夹后面加/即可   -a 'src=/root/hello_dir/  dest=/root'
root@ubuntu:~# ansible 192.168.108.130 -m copy -a 'src=/root/hello_dir  dest=/root'
192.168.108.130 | SUCCESS => {
    "changed": true, 
    "dest": "/root/", 
    "src": "/root/hello_dir"
}
#去节点看看,全部复制过来了
root@ubuntu:~# ls
hello_dir  hello.txt
root@ubuntu:~/hello_dir# ls
hello_level2  hello.txt

在节点执行shell功能

#在节点curl 一下百度
root@ubuntu:~# ansible 192.168.108.130 -m shell -a 'curl baidu.com'
 [WARNING]: Consider using the get_url or uri module rather than running curl.  If you need to use command because get_url or uri
is insufficient you can add warn=False to this command task or set command_warnings=False in ansible.cfg to get rid of this
message.

192.168.108.130 | SUCCESS | rc=0 >>
<html>
<meta http-equiv="refresh" content="0;url=http://www.baidu.com/">
</html>  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    81  100    81    0     0    810      0 --:--:-- --:--:-- --:--:--   810
#给节点创建文件夹
root@ubuntu:~# ansible 192.168.108.130 -m shell -a 'mkdir /root/mkdir_test'
 [WARNING]: Consider using the file module with state=directory rather than running mkdir.  If you need to use command because file
is insufficient you can add warn=False to this command task or set command_warnings=False in ansible.cfg to get rid of this
message.

192.168.108.130 | SUCCESS | rc=0 >>
#去节点查看是否真的创建成功了
root@ubuntu:~# ls
hello_dir  hello.txt  mkdir_test

其他组件自行百度看demo

Ansible Playbook

hosts是表明要操作哪些机器,这些机器必须配置过免密连接,且在/etc/ansible/hosts里有
可以支持多Ip,或者写分组名、或者写all代表全部
remote_user 远程登录使用的账号
vars变量表,你可以定义一些变量放这里,在后面可以通过{{var_name}}的方式引用,vars里的变量也可以引用前面定义的变量,需要使用""在最外层包起来
tasks是任务表,会一个个执行
每个task一般包括

  • name: taskname 任务名字
    shell: xxxxxxxxx 任务的组件用的shell,参数是 后面的,例如copy 组件的参数后面写src=xxx dest=xxx

书写playbook的时候建议每个单独测试通过再加进整个里,避免一堆BUG
以下是常用操作的示范代码,已通过测试。自行参考理解,对齐要求严格,一般是文件三个短杠开头第一行,然后关键字用短杠空格开始,其他的用两个空格对齐,层次要求严格,后缀名一般用yml

---
- hosts: 192.168.108.131
  remote_user: root
  vars:
    deploy_user: ubuntu
    home_dir: /home/ubuntu
    #被hash512处理过的密文,用来创建用户的时候给定密码,生成密码使用python
    #from passlib.hash import sha512_crypt
    #print(sha512_crypt.encrypt("123456"))
    #exit()
    hash512pwd: $6$rounds=656000$3w6h1Ao6VPQYTPgW$Eaeoz.x08n7E24CZil1tWKOStOeECg/KoKXyDB5XOXNXmjarjbu3LXIQQNGwHrDoLS3YSzPp4O2W3CFE8146K/
    #安装包放置的路径(不带/结尾)
    install_package_path: /usr/local/share/deploy
    #openresty 安装包名字(不带.tar.gz)
    openresty_tar_gz_name: openresty-1.19.3.1
    #node安装包名字
    node_tar_xz_name: node-v14.15.1-linux-x64
    #由于ansible的环境变量问题,所以在使用npm等命令时,先修改path,这样使用,shell: "{{path}} && npm install -g yarn -y"
    path: "PATH=$PATH:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin:{{home_dir}}/runtime/openresty/nginx/sbin/nginx:{{home_dir}}/runtime/{{node_tar_xz_name}}/bin"
  tasks:
    #************************基础环境配置************************
    #python 设置
    - name: set python link
      shell: sudo ln -sf /usr/bin/python3 /usr/bin/python

    #更新apt
    - name: update apt
      shell: sudo apt update -y && sudo apt upgrade -y 

    #时区设定
    - name: set timezone
      shell: sudo ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

    # 安装基础编译库 正则库 ssl库 zlib
    - name: install basic lib
      shell: sudo apt install build-essential libpcre3 libpcre3-dev openssl libssl-dev zlib1g-dev  -y 

    #创建账号
    - name: add user
      user: name={{deploy_user}} password={{hash512pwd}} shell=/bin/bash home={{home_dir}} state=present

    # 创建相关目录
    - name: make directory
      shell: mkdir {{home_dir}}/backup {{home_dir}}/runtime {{home_dir}}/service {{home_dir}}/vhosts {{home_dir}}/logs {{home_dir}}/static  {{home_dir}}/share


    
    #安装Openresty
    #复制安装包
    - name: copy install package
      copy: src={{install_package_path}}/{{openresty_tar_gz_name}}.tar.gz  dest={{home_dir}}/share
    #解压
    - name: unzip install package
      shell: tar -zxvf {{home_dir}}/share/{{openresty_tar_gz_name}}.tar.gz -C {{home_dir}}/share/
    #配置 编译  安装
    - name: configure && make && make install
      shell: cd {{home_dir}}/share/{{openresty_tar_gz_name}} && ./configure --prefix={{home_dir}}/runtime/openresty && make && make install
    #配置到环境变量
    - name: add to path
      shell: echo "export PATH=\$PATH:{{home_dir}}/runtime/openresty/nginx/sbin" >> /etc/profile

    #安装Node 
    #复制安装包
    - name: copy install package
      copy: src={{install_package_path}}/{{node_tar_xz_name}}.tar.xz dest={{home_dir}}/share
    #解压
    - name: unzip package
      shell:  tar -xvJf {{home_dir}}/share/{{node_tar_xz_name}}.tar.xz -C {{home_dir}}/runtime
    #添加到环境变量
    - name: add to path
      shell: echo "export PATH=\$PATH:{{home_dir}}/runtime/{{node_tar_xz_name}}/bin" >> /etc/profile
    #安装新版npm
    - name: install npm
      shell: "{{path}} && npm install -g npm -y"
    #安装Yarn 
    - name: install yarn
      shell: "{{path}} && npm install -g yarn -y"
    #安装pm2
    - name: install pm2
      shell: "{{path}} && npm install -g pm2 -y"

    #安装Open JDK 8
    - name: install openjdk 8
      shell: sudo apt install openjdk-8-jdk -y

ansible命令版示范

注意好$这种有特殊作用的词,需要时用\转义

  • 一键安装openresty
ansible all -m shell -a '
echo '开始安装Openresty' &&
sudo apt-get -y install --no-install-recommends wget gnupg ca-certificates &&
wget -O - https://openresty.org/package/pubkey.gpg | sudo apt-key add - &&
echo "deb http://openresty.org/package/ubuntu $(lsb_release -sc) main" \
    | sudo tee /etc/apt/sources.list.d/openresty.list  &&
sudo apt-get update &&
sudo apt-get -y install --no-install-recommends openresty &&
echo 'Openresty安装完毕' '
  • 一键安装node
echo 'echo "开始安装Node" && cd /usr/local/share && wget https://nodejs.org/dist/v14.15.1/node-v14.15.1-linux-x64.tar.xz && tar -xvJf /usr/local/share/node-v14.15.1-linux-x64.tar.xz -C /usr/local/share && echo "export PATH=\$PATH:/usr/local/share/node-v14.15.1-linux-x64/bin" >> /etc/profile && source /etc/profile && echo "Node安装完成" && node -v' > script.sh &&
ansible all -m copy -a 'src=./script.sh dest=/usr/local/share' &&
ansible all -m shell -a 'chmod +x /usr/local/share/script.sh && bash /usr/local/share/script.sh'
  • 一键安装java
ansible all -m shell -a 'apt install openjdk-8-jdk -y'
  • 一键部署jar项目

部署/usr/local/share/demo.jar到所有机器并后台启动

ansible all -m copy -a 'src=/usr/local/share/demo.jar dest=/usr/local/share' &&
ansible all -m shell -a 'nohup java -jar /usr/local/share/demo.jar > /usr/local/share/demo.log 2>&1 &'

你可能感兴趣的:(Linux,运维,linux,ansible,集群管理,自动化运维)