ansible自动化批量部署工具

ansible架构图

ansible架构图
Ansible:Ansible的核心程序
Host Lnventory:记录了每一个由Ansible管理的主机信息,信息包括ssh端口,root帐号密码,ip地址等等。可以通过file来加载,可以通过CMDB加载
Playbooks:YAML格式文件,多个任务定义在一个文件中,使用时可以统一调用,“剧本”用来定义那些主机需要调用那些模块来完成的功能.
Core Modules:Ansible执行任何管理任务都不是由Ansible自己完成,而是由核心模块完成;Ansible管理主机之前,先调用core Modules中的模块,然后指明管理Host Lnventory中的主机,就可以完成管理主机。
Custom Modules:自定义模块,完成Ansible核心模块无法完成的功能,此模块支持任何语言编写。
Connection Plugins:连接插件,Ansible和Host通信使用

ansible优点

Stupied Simple ,上手简单,学习曲线平滑
SSH by default ,安全,无需安装客户端
配置简单、功能强大、扩展性强
支持API及自定义模块,可通过Python轻松扩展
通过Playbooks来定制强大的配置、状态管理
提供一个功能强大、操作性强的Web管理界面和REST API接口——AWX平台
幂等性:一种操作重复多次结果相同

安装下载

yum install -y epel-release
yum install -y ansible

配置客户端(无密码登录)

第一种方法:

server: ssh-keygen
scp id_rsa.pub root@192.168.254.25:/root/.ssh/authorized_keys

第二种方法:

vim /etc/ansible/hosts
[scum]
192.168.77.151 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass=root
可以写多个
书写格式

ansible-doc -l #查看支持的模块
ansible-doc -s MODEL_NAME #查看模块用法
ansible命令应用基础
ansible [options]
      -f forks:启动并发线程数
      -m model_name:要使用的模块
      -a args:特有的参数

常用命令

ansible all -m ping #查看client端是否正常ping通
ansible webserver -m setup #查看客户端信息
ansible webserver -m copy -a ‘src=/root/git_test/code.txt dest=/root/test’ #copy文件到cient端
ansible webserver -m user -a “name=test state=present” #创建test用户
ansible webserver -m user -a “name=test state=absent” #删除test用户
ansible webserver -m yum -a ‘name=epel-relese state=latest‘ #yum安装
ansible webserver -m service -a ‘name=httpd state=stopped enabled=no‘ #停止httpd服务
ansible webserver -m script -a ‘/tmp/test.sh‘ #运行脚本
ansible webserver -m command ‘date’ #查看时间

部分错误演示
  • 在执行ansible是发生如下错误
[root@scum ~]# ansible all -m ping
192.168.77.151 | FAILED! => {
    "msg": "Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this.  Please add this host's fingerprint to your known_hosts file to manage this host."
}
vim /etc/ansible/ansible.cfg
host_key_checking = False    #注释解开
#重新执行一次命令
[root@scum ~]# ansible all -m ping         
192.168.77.151 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
#成功ping通
  • 如果用command执行命令出现这个错误
[root@scum ~]# ansible scum -m command -a 'service stop httpd'   
 [WARNING]: Consider using the service module rather than running 'service'.
If you need to use command because service 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.77.151 | FAILED | rc=0 >>
Redirecting to /bin/systemctl stop httpd.service

意思为有执行这个命令的模块

vim /etc/ansible/ansible.cfg
command_warning = False  #注释打开

playbooks

一个任务代表一个play
多个play组合起来叫做playbooks
一定要注意书写格式

  • tasks
  • variables
  • templates
  • handlers
  • roles
  • yaml介绍

yaml是一个可读性高的用来表达资料序列的格式,yaml参考了其他多种语言,包括:xml,c语言,python,perl以及电子邮件格式RFC2822等,ClarkEvans在2001年在首次发表了这种语言。

1.yaml的可读性好
2.yaml和脚本语言的交互性好
3.yaml使用实现语言的数据类型
4.yaml有一个一致的信息模型
5.yaml易于实现
6.yaml可以基于流程来处理
7.yaml表达能力强,扩展性好

tasks任务
[root@scum ~]# vim test.yml
- hosts: scum          #指定客户端(hosts里的组)
  remote_user: root   #指定执行用户
  tasks:   #任务
  - name: yum install tree  #任务名称
    yum: name=tree state=present   #调用yum模块,指定名称,状态
  - name: changed service status   #任务名
    service: name=httpd state=started  #指定服务 指定状态
~

执行过程与结果

[root@scum ~]# ansible-playbook test.yml 

PLAY [scum] ********************************************************************

TASK [Gathering Facts] *********************************************************
ok: [192.168.77.151]

TASK [yum install tree] ********************************************************
changed: [192.168.77.151]

TASK [changed service status] **************************************************
changed: [192.168.77.151]

PLAY RECAP *********************************************************************
192.168.77.151             : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

variables变量
[root@scum ~]# vim test.yml              
  
- hosts: scum
  remote_user: root
  vars:
  - first: tree   #first=tree
  tasks:
  - name: yum install tree
    yum: name={{ first }} state=present  #下载的名称为first变量
  - name: changed service status
    systemd: name=httpd state=started
~                                       
迭代
[root@scum ~]# vim test.yml 
- hosts: scum
  remote_user: root
  vars:
  - first: tree
  tasks:
  - name: yum install tree
    yum: name={{ first }} state=present
  - name: changed service status
    systemd: name=httpd state=started
  - name: useradd
    user: name={{ item }}#内置变量,死格式
    with_items: #迭代,声明变量
    - user1  #迭代内容
    - user2
    - user3
~ 
templates模板和handlers触发器

可以实现不同主机的配置
在yml文件里调用templates模板,把要拷贝的文件里的参数修改成变量,在/etc/ansible/hosts里添加变量

[root@scum ~]# vim test.yml 
- hosts: scum
  remote_user: root
  vars:
  - first: tree
  tasks:
  - name: yum install tree
    yum: name={{ first }} state=present
  - name: changed service status
    systemd: name=httpd state=started
  - name: useradd
    user: name={{ item }}
    with_items:
    - user1
    - user2
    - user3
  - name: copy httpd.conf
    template: src=/etc/httpd/conf/httpd.conf dest=/etc/httpd/conf/httpd.conf  #源地址与目标地址
    notifi:  #警告
    - systemd restart httpd
  handlers: #触发器
  - name: systemd restart httpd #触发器名称要和警告名称一样
    systemd: name=httpd state=restarted
~

在httpd配置文件把端口号修改成变量名
格式:{{ 变量名 }}

[root@scum ~]# vim /etc/httpd/conf/httpd.conf 
#Listen 12.34.56.78:80
Listen {{ first }}

在ansible的hosts文件里设置变量名

[root@scum ~]# vim /etc/ansible/hosts 
[scum]
192.168.77.151 ansible_ssh_user=root ansible_ssh_port=22 ansible_s
sh_pass=root first=777

roles

roles把每一个play封装角色里,调用时只需要添加角色就好。

[root@scum ~]# mkdir -pv playbooks/roles/{scum,snow}/{files,handlers,tasks,templates,vars}
[root@scum playbooks]# tree /root/playbooks/
/root/playbooks/
├── roles
│   ├── scum  #角色名称
│   │   ├── files
│   │   ├── handlers  #目录下的文件必须是main.yml
│   │   ├── tasks  #目录下的文件必须是main.yml
│   │   ├── templates
│   │   └── vars  #目录下的文件必须是main.yml
│   └── snow
│       ├── files
│       ├── handlers
│       ├── tasks
│       ├── templates
│       └── vars
└── site.yml
13 directories, 1 file

在roles同级目录建立site.yml(固定的)

[root@scum ~]# vim playbooks/site.yml 
- hosts: scum
  remote_user: root
  roles:
  - snow   #调用该角色
~          
handlers文件格式
[root@scum ~]# vim playbooks/roles/snow/handlers/main.yml
- name: restart httpd
  systemd: name=httpd state=restart
tasks文件格式
[root@scum ~]# vim playbooks/roles/snow/tasks/main.yml
- name: yum install tree
yum: name=tree state=absent
- name: cp -a httpd
copy: src=httpd.conf dest=/etc/httpd/conf/httpd.conf
vars文件格式
[root@scum ~]# vim playbooks/roles/snow/vars/main.yml 
first: 777
files目录

存放需要调用的文件

templates目录

调用templates时所需的文件,全部存放在这个目录下

新手上路,请大家多关照

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值