分享

Jenkins+Ansible+GitLab持续交付平台搭建-第3篇

 TestOps云层 2021-06-22

Ansible安装配置管理

Ansible优势和应用场景

Ansible:开源部署工具

开发语言:Python

特点:ssh协议通讯,全平台,无需编译,模块化部署管理

作用:推送playbook进行远程节点快速部署

优势:

轻量级无客户端(Agentless)

开源免费,学习成本低,快速上手

使用playbook作用核心配置架构,统一脚本格式,批量化部署

完善的模块化扩展,支持目前主流的开发场景

强大的稳定性和兼容性

Ansible安装配置

Ansible的两种安装模式(cenOS7)

1.yum包管理安装

1# yum –y install ansible

2.Git源码码安装(推荐)

Ansible2.5+Python3.6安装步骤:

1)预先安装py3.6

1# wget https://www./ftp/python/3.6.5/Python-3.6.5.tar.xz
2
3#指定目录编译
4# tar xvf Python-3.6.5.tar.xz
5#cd /Python-3.6.5
6# ./configure --prefix=/usr/local --with-ensurepip=install --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"

遇到错误:configure: error: no acceptable C compiler found in $PATH

解决:yum install -y gcc

# make && make altinstall

遇到错误:make: *** [altinstall] Error 1

解决:yum install zlib-devel bzip2-devel sqlite sqlite-devel openssl-devel

https:///questions/40355611/python-3-4-installation-error-1-oserror-errno-22-invalid-argument

2)安装virtualenv

1#查看pip路径
2# which pip3.6
3/usr/local/bin/pip3.6
4
5#给usr/local/bin/pip3.6做软连接
6# ln -s /usr/local/bin/pip3.6  /usr/local/bin/pip
7# pip install virtualenv

3)创建ansible账号并安装py3.6版本和virtualenv实例

1# useradd deploy
2# su - deploy
3# virtualenv -p /usr/local/bin/python3.6 .py3-a2.5-env

4)git源代码安装Ansible2.5

1#查看deploy用户是否安装git
2# su - deploy
3$ which git
4/usr/bin/git
5
6#使用root用户安装git依赖包
7# yum -y install git nss  curl
8
9#deploy git安装完成后(使用root)
10# cd  /home/deploy/.py3-a.2.5-env
11# git clone https://github.com/ansible/ansible.git

5)加载py3.6 virtualenv环境

1# source  /home/deploy/.py3-a2.5-env/bin/activate

6)安装Ansible依赖包

1#安装paramiko pyYAML jinja2依赖包
2# pip install paramiko pyYAML jinja2
3
4#进入ansible目录下将ansible源代码切换到2.5版本
5# cd .py3-a2.5-env/ansible
6# git checkout stable-2.5

7)在py3.6虚拟环境下加载ansible2.5

1# source  /home/deploy/.py3-a2.5-env/ansible/hacking/env-setup -q

8)验证ansible2.5

1# ansible --version

到此ansible2.5版本已经成功的在py3.6虚拟环境下完成安装;

CentOS6.7下Ansible部署:http://www.showerlee.com/archives/1649

Ansible中文权威指南:http://www.ansible.com.cn/index.html

https://blog.csdn.net/wating_jx/article/details/71084592

Ansible常用模块操作和编写规范

playbook框架与格式

Test playbook 文件结构清单:

inventory/ ---->server详细清单目录

  testenv/ ---->具体清单与变量声明文件

roles/ ---->roles任务列表

  testbox/

    tasks/

      main.yml ---->testbox主任务文件

deploy.yml ---->playbook任务入口文件

详细目录testenv

[testservers]  ---->server组列表

test.example.com  ---->目标部署服务器主机名

[testservers:vars]  ---->server组列表参数

server_name= test.example.com

user=root      ---->目标主机key/value参数

output=/root/test.txt

主任务文件main.yml

任务入口文件deploy.yml

远程ansible主机,编写playbook框架

1# ssh root@47.98.198.241
2# su - deploy
3
4#加载py3.6虚拟环境
5# source .py3-a2.5-env/bin/activate
6
7#同时加载ansible2.5版本
8# source  /home/deploy/.py3-a2.5-env/ansible/hacking/env-setup -q
9
10#验证加载效果
11# ansible-playbook --version
12
13#编写playbook框架
14# mkdir playbook
15# cd /playbook
16# mkdir inventory
17# mkdir roles
18# cd inventory
19
20# vi testenv
21   [testservers]
22    report.example.com
23
24  [testservers:vars]
25    server_name= report.example.com
26    user=root
27    output=/root/test.txt
28
29# cd roles
30# mkdir testbox
31# cd testbox
32# mkdir tasks
33# cd tasks
34
35# vi main.yml
36- name: print server name and user to remste testbox
37shell: "echo 'Currently {{ user }} is logining {{ server_name }}' > {{ output }}"
38
39# cd ../../..
40#pwd
41 /home/deploy/playbook
42
43# vi deploy.yml
44- hosts: "testservers"
45  gather_facts: true
46  remote_user: root
47  roles:
48    - testbox

#查看树形文件夹

1# tree .
2
3(.py3-a2.5-env) [deploy@iZbp1hovekdxocyqhdwshnZ xj_aml]$ tree .
4
5.
6
7├── deploy.yml
8
9├── inventory
10
11│   └── testenv
12
13└── roles
14
15    └── testbox
16
17        └── tasks
18
19            └── main.yml
20
21
22
234 directories, 3 files

#创建目标主机DNS主机记录

1# su - root
2# vi /etc/hosts
3172.16.87.102  report.example.com

#切换deploy用户,创建ssl秘钥认证

# ssh-keygen -t rsa

#指定deploy用户公钥

1# ssh-copy-id -i /home/deploy/.ssh/id_rsa root@report.example.com(test.example.com是ansible本机DNS)
2# ansible-playbook -i inventory/testenv ./deploy.yml (命令手动敲) 在/home/deploy/playboo目录下执行

遇到错误:bad permissions: ignore key: /home/deploy/.ssh/id_rsa

解决:

1chmod 755 ~/.ssh/
2chmod 600 ~/.ssh/id_rsa ~/.ssh/id_rsa.pub
3chmod 644 ~/.ssh/known_hosts

#测试目标主机

1# ssh root@report.example.com
2
3[root@localhost ~]# ls -l .ssh/
4
5总用量 4
6-rw-------. 1 root root 410 11月  7 15:51 authorized_key

authorized_key是目标主机.ssh的秘钥


此系列会在我们TestOps公众号定期更新,请随时关注哟~

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多