ansible自动化运维安装部署实践-部署、搭建、模块、playbook

运维⾃动化平台介绍 
运维⾃动化平台是由管理机器和业务机器组成的。
管理机器:任务定制及发布;
业务机器:接收任务并执⾏任务。
运维⾃动化平台的优势:
1、⼀次性任务定制:任务⼀次性发布给所有机器 
2、节省任务执⾏时间:任务主机并发完成任务,节省部署时间 
3、错误率低:避免重复,保证⼀次任务定制准确即可

1、ansible特性

1. no agents:不需要在被管控主机上安装任何客户端; 
2. no server:⽆服务器端,使⽤时直接运⾏命令即可; 
3. modules in any languages:基于模块⼯作,可使⽤任意语⾔开发模块; 
4. yaml,not code:使⽤yaml语⾔定制剧本playbook; 
5. ssh by default:基于SSH⼯作

2、准备工作

#实验机器
192.168.192.128  managn01
192.168.192.129  node01
192.168.192.130  node02
192.168.192.131  node03
hostnamectl set-hostname node01-永久修改主机名(vm直接拷贝虚拟机情况)
#提前确认
关闭selinux、firewalld、ntp/chronyc同步、yum update、静态IP、主机名解析/etc/hosts
#ssh免密登陆
管理机将自己生成的公钥传给被管理机器,然后私钥匹配公钥连接
[root@manage01 ~]# ssh-keygen #创建密钥
[root@manage01 ~]# ssh-copy-id -i .ssh/id_rsa.pub [email protected] #将公钥传给所有被管理机
[root@manage01 ~]# for i in 129 130 131 #免密登陆证书验证
> do
> ssh [email protected].$i hostname
> done
node01
node02
node03 
#⼩窍⻔(批量免密设置) 
免交互创建公私钥 
[root@manage01 ~]# ssh-keygen -f /root/.ssh/id_rsa -N ""   
-f 指定私钥存放路径 -N ""  新密码设置问空 -P ""  ⽼密码是什么
如何可以⾮交互式传公钥呢 
[root@manage01 ~]# yum -y install sshpass 
[root@manage01 ~]# sshpass -p123456 ssh-copy-id -o StrictHostKeyChecking=no -i /root/.ssh/id_rsa.pub [email protected] 
StrictHostKeyChecking   严厉的主机监测=no  就不会问你yes|no了 sshpass ⾮交互式传密码

3、ansible部署

#yum安装
[root@manage01 ~]# yum -y install ansible 
[root@manage01 ~]# ansible --version
#源码安装(推荐)
[root@manage01 ~]# wget https://releases.ansible.com/ansible/ansible2.9.0rc3.tar.gz
[root@manage01 ~]# cd /opt/ansible
#确认有没有python和pip环境
[root@manage01 ansible]# yum -y install epel-release
[root@manage01 ansible]# yum install python-pip
[root@manage01 ansible]# pip install --upgrade pip
#因ansible为python开发,使用pip进行环境包和软件包安装
[root@manage01 ansible]# pip2 install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple/ 
[root@manage01 ansible]# pip2 install --user ansible -i https://pypi.tuna.tsinghua.edu.cn/simple/
#为了日后方便
[root@manage01 ~]# ln -s /opt/ansible/bin/* /usr/bin/ 
[root@manage01 ~]# mkdir /etc/ansible
[root@manage01 ~]# cp /opt/ansible/examples/ansible.cfg /etc/ansible/
#配置主机列表并测试
[root@manage01 ansible]# cp examples/hosts /etc/ansible/ #拷贝主机列表文件供ansible读取
[root@manage01 ansible]# cat  /etc/ansible/hosts #配置被管理机主机列表
[group1] 
192.168.192.[129:131]
[root@manage01 ansible]# ./bin/ansible -m ping group1 #测试
 192.168.192.130 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"


4、ansible常用模块


ansible是基于模块⼯作的,本身没有批量部署的能⼒。真正具有批量部署的是ansible所运⾏的模块,
虽然日常推荐使用playbook剧本,基本模块还是需要了解的。
模块用法:
ansible 机器 -m 模块名称 -a '模块参数'
基本格式为: ansible 操作的机器名或组名 -m 模块名 -a "参数1=值1 参数2=值2

#file模块-创建、删除、权限、软链接
[root@manage01 ~]# ansible -m file group1 -a "path=/tmp/tyschool state=touch"
[root@manage01 ~]# ansible -m file 192.168.192.129 -a "path=/tmp/tyschool state=absent"
[root@manage01 ~]# ansible -m file 192.168.192.129 -a "path=/tmp/tyschool owner=sko group=nobody mode=0600"
[root@manage01 ~]# ansible -m file 192.168.192.129 -a "src=/tmp/tyschool path=/tmp/tyschool_com state=link"
#copy模块-拷贝并校验完整性、
[root@manage01 ~]# sha1sum readme
f8182e9ccdbe6efd13eb36a056a7db203fe66e40 readme
[root@manage01 ~]# ansible -m copy group1 -a "src=/root/readme dest=/opt
checksum=f8182e9ccdbe6efd13eb36a056a7db203fe66e40 owner=sko group=sko mode=0400"
*/etc/yum.repos.d后⾯不带/符号,则表示把/etc/yum.repos.d整个⽬录拷⻉到/tmp/⽬录下
[root@manage01 ~]# ansible group1 -m copy -a 'src=/etc/yum.repos.d dest=/tmp/'
*/etc/yum.repos.d/后⾯带/符号,则表示把/etc/yum.repos.d/⽬录⾥的所有⽂件拷⻉到/tmp/⽬录下 
[root@manage01 ~]# ansible group1 -m copy -a 'src=/etc/yum.repos.d/ dest=/tmp/'
#cron模块-创建定时执行任务、删除任务
[root@manage01 ~]# ansible -m cron group1 -a 'name="cron test" user=root job="echo haha > /tmp/test" minute=23 hour=12
[root@manage01 ~]# ansible -m cron group1 -a 'name="cron test" state=absent'
#yum_repository模块-配置yum源
[root@manage01 ~]# ansible -m yum_repository group1 -a "name=dvd description=BaseOS baseurl=file:///mnt/BaseOS gpgcheck=0 enabled=yes"
#yum模块-yum装包、卸载
[root@manage01 ~]# ansible -m yum group1 -a "name=vsftpd"
[root@manage01 ~]# ansible -m yum 192.168.192.129 -a "state=absent name=vsftpd"
#service模块-启动服务、停止
[root@manage01 ~]# ansible -m service 192.168.192.129 -a "name=vsftpd state=started enabled=on"
[root@manage01 ~]# ansible -m service 192.168.192.129 -a "name=vsftpd state=stopped enabled=false"
#shell模块-执行命令行,不能用于vim等交互式命令
[root@manage01 ~]# ansible -m shell 192.168.192.129 -a "ls /root"


5、playbook剧本


playbook(剧本): 是ansible⽤于配置,部署,和管理被控节点的剧本。
参考:https://docs.ansible.com/ansible/latest/user_guide/playbooks_intro.html
使⽤的格式为yaml格式(saltstack,elk,docker等也都会⽤到yaml格式)
YAML格式规则
⽂件的第⼀⾏以 "---"开始,表明YMAL⽂件的开始.
以#号开头为注释
列表中的所有成员都开始于相同的缩进级别, 并且使⽤⼀个 "- " 作为开头(⼀个横杠和⼀个空格)
⼀个字典是由⼀个简单的 键: 值 的形式组成(这个冒号后⾯必须是⼀个空格)
注意: 写这种⽂件不要使⽤tab键,都使⽤空格
参考: https://docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html#yaml-syntax
#YAML格式规则
⽂件的第⼀⾏以 "---"开始,表明YMAL⽂件的开始.
以#号开头为注释
列表中的所有成员都开始于相同的缩进级别, 并且使⽤⼀个 "- " 作为开头(⼀个横杠和⼀个空格)
⼀个字典是由⼀个简单的 键: 值 的形式组成(这个冒号后⾯必须是⼀个空格)
注意: 写这种⽂件不要使⽤tab键,都使⽤空格

[root@manage01 ~]# mkdir -p /opt/ansible/playboot/web
[root@manage01 web]# cat  apache_init.yaml   
#apache安装及业务初始化
---
- hosts: 192.168.192.129
  remote_user: root
  tasks:
  - name: create user
    user: user=haha state=present
  - name: install httpd server
    yum: name={{item}} state=latest
    with_items:
      - httpd
      - httpd-devel
  - name: start httpd server
    service: name=httpd state=started enabled=yes
  - name: copy new httpd.conf to /etc/httpd/conf
    copy: src=/opt/ansible/playbook/web/httpdnew.conf dest=/etc/httpd/conf
    notify:
    - restart httpd service
  handlers:
  - name: restart httpd service
    service: name=httpd state=restarted

 FAQ:
#警告

 [DEPRECATION WARNING]: Invoking "yum" only once while using a loop via squash_actions is deprecated. Instead of using a loop to supply multiple
 items and specifying `name: "{{item}}"`, please use `name: ['httpd', 'httpd-devel']` and remove the loop. This feature will be removed in 
version 2.11. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
#解决
[root@manage01 web]# cat  /etc/ansible/ansible.cfg | grep False
deprecation_warnings=False


6、roles(⻆⾊): 

playbook+roles自动化部署lamp环境见下章节。

发布了64 篇原创文章 · 获赞 9 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_39855998/article/details/104372622