大型网站架构与自动化运维——ansible

                                                   自动化运维之Ansible

一、概述
1、分类
(1)需代理(基于专用Agent程序):Puppet、Func、Zabbix
(2)不需代理(可基于SSH完成):Ansible、Fabric等

2、Puppet
基于Ruby研发,支持Linux、Unix、Windows平台,可针对用户、系统服务、配置文件、软件包等进行管理,有很强扩展性,但远程执行命令相对较弱

3、SaltStack
基于Python研发,允许对多个操作系统创建统一的管理系统,比Puppet轻量级

4、Ansible
基于Puthon研发,视线里批量运行命令、部署程序、配置系统等功能

二、Anisble核心组件
1、Ansible core:核心引擎
2、Host inventory:主机清单,用来定义所管理的主机,默认在Ansible的hosts配置文件中定义
3、Connection plugins:连接插件,负责和被管理主机实现通信。除SSH连接外还支持其他连接方式
4、Playbooks:脚本,用来集中定义Ansible任务的配置文件,即将多个任务定义在一个剧本中执行
5、Core modules:核心模块,是Ansible自带模块,使用这些模块将资源分发到被管理主机,使其执行特定任务或匹配特定状态
6、Custom modules:自定义模块,用于完成模块功能的补充,可记住相关插件完成记录日志、发送邮件等功能

三、安装部署Ansible服务
1、安装Ansible
(1)配置yum的epel源(略)
(2)yum -y install ansible

2、配置主机清单
vim /etc/ansible/hosts
[groupname]
192.168.11.151
[groupname2]
192.168.11.152

3、设置SSH无密码登录
ssh-keygen -t rsa
ssh-copy-ip [email protected]
ssh-copy-ip [email protected]

四、Ansible基础命令
命令格式:ansible <host-pattern> [-m module_name] [-a args]
查看模块帮助信息:ansible-doc -s(指定模块) -l(所有模块)

1、command模块(默认模块):命令模块
(1)指定IP运行
ansible 192.168.11.151 -m command -a ‘date’
(2)指定主机分类运行
ansible nodes -a ‘date’
(3)所有主机运行
ansible all -a ‘date’

2、cron模块:计划任务模块,persent为添加,absent为删除
(1)添加计划任务
ansible nodes -m cron -a ’minute=”*/10” job=”/bin/echo hello” name=”test job”’
(2)删除计划任务
ansible nodes -m cron -a ‘minut=”*/10” job=”/bin/echo hello” name=”test job” state=absent’

3、user模块:创建、修改、删除用户,用name指定用户名
(1)创建用户
ansible nodes -m user -a ‘name=”user1”’
(2)删除用户
ansible nodes -m user -a ‘name=”user1” state=absent’

4、group模块:管理用户组
(1)创建组并添加用户
ansible nodes -m group -a ‘name=mysql gid=306 system=yes’
ansible nodes -m user -a ‘name=mysql uid=306 system=yes group=mysql’

5、copy模块:文件复制及下发,src指定源文件,dest定义被管理机文件路径,content指定信息内容生成目标文件
(1)复制文件并设置用户及权限
ansible nodes -m copy -a ‘src=/etc/fstab dest=/tem/fatab.ansible owner=root mode=640’
(2)将文本写入目标机文件
ansible nodes -m copy -a ‘content=”Hello” dest=/tem/fatab.ansible’
(3)如被管理机开启了Selinux,则需要在其上面安装libselinux-python软件包

6、file模块:用于设置文件属性,src指定源文件路径,path指定目标文件路径,name或dest来替换创建文件的符号链接
(1)设置文件属主、属组及权限
ansible nodes -m file -a ‘owner=mysql group=mysql mode=644 path=/tem/fstab.ansible’
(2)设置文件连接
ansible nodes -m file -a ‘path=/tem/fstab.link src=/tem/fstab.ansible state=link’

7、ping模块:检测连通性
    ansible all -m ping

8、service模块:管理控制服务运行状态,enabled表示是否开机启动(true、false),name定义服务名称,state指定服务状态(started、stoped、restarted)
(1)查看服务状态
ansible nodes -a ‘service httpd status‘
(2)启动服务并设置为开机启动
ansible nodes -m service -a ‘enabled=true name=httpd state=started’

9、shell模块:运行命令并支持管道符等功能
(1)无交互式设置用户密码
ansible nodes -m shell -a ‘echo 111111 | passwd --stdin user1’

10、script模块:将本地脚本复制到被管理机进行运行(使用相对路径指定脚本)
(1)ansible nodes -m script -a ‘test.sh’

11、yum模块:yum安装或卸载软件包,name指定软件包,state指定安装包状态(present、absent)
(1)安装软件
ansible nodes -m yum -a ‘name=zsh’
(2)卸载软件
ansible nodes -m yum -a ‘name=zsh state=absent’

12、setup模块:搜集、查看被管理机的facts
    ansible nodes -m setup

五、YAML介绍
yaml是一种用来表达资料序列的格式,由于参考了多种语言,所以具有很高的可读性
1、特性
(1)很好的可读性,易于实现
(2)表达能力强,扩展性好
(3)和脚本语言的交互性好
(4)有一个一致的信息模型
(5)可以基于流来处理

2、语法
    和其他语言类似,也可以表达散列表、标量等数据结构。其中结构通过空格来展示:序列(sequence)里的项用-来代表;Map里的键值对用:来分隔。

3、常用数据类型
(1)list(列表)
列表的所有元素均使用-开头,如:
-Apple
-Orange
(2)dictionary(字典)
字典通过key和value进行标识,如:
name:Example Dictionary
Job:Developer
Skill:Elite
也可以使用key:value的形式放置于{}中进行标识,如:
{name:Example Dictionary,Job:Developer,Skill:Elite}

六、Ansible基础元素介绍
1、Inventory(主机清单)
    默认主机清单为/etc/ansible/hosts文件。主机清单可设置为多个,也可以通过Dynamic Inventory动态生成。实例:
    [nodes]
    192.168.11.151
    192.168.11.152
    如主机名遵循类似命名规则,可使用列表方式标识主机,如
    [nodes]
    www[01:05].example.org
    db-[a:f].example.org
    Inventory中的重要概念:
(1)主机变量:可以在定义主机时设置,以便在后续Playbook中使用
[nodes]
192.168.11.151 http_port=80 maxRequestsChild=808
(2)组变量:给指定主机设置可以在Playbook中直接使用的变量
[servers-vars]
ntp_server=ntp.example.org
nfs_server=nfs.example.org
(3)组嵌套
组可以嵌套其他组,也可以向组中的主机指定变量。不过这些变量只能在ansible-playbook工具中使用
[node1]
192.168.11.151
[nodes:children]
node1
(4)Inventory参数
ansible_ssh_port:指定ssh端口
ansible_ssh_user:指定用户
ansible_ssh_pass:指定用户登录认证密码,明文密码不安全
ansible_sudo_pass:指明sudo时的密码
ansible_connection:SSH连接类型(local、ssh、paramiko)
ansible_ssh_private_key_file:公钥文件
ansible_shell_type:指定shell解释器,默认sh
ansible_python_interpreter:指定python解释器的路径
ansible \_\*\_interpreter:指定主机上其他语法解释器的路径

2、变量
(1)通过命令行传递变量
ansible-playbook test.yml -e “hosts-www user-mageedu”
(2)通过roles传递变量
roles:
  - common
  - {role: foo_app_instance,dir:’/web/htdocs/a.com’,port:8080}

3、条件测试
(1)when
在task之后添加when即可进行条件测试,when支持jinjia2表达语句
task:
  - name: shutdown
    command: shutdown -h 0
    when: ansible_os_family - “Debian”
(2)迭代
将需要迭代的内容定义为item变量并引用,然后通过with_items指明迭代的元素
user: name={{ item }}
with_items:
  - user1
  - user2

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章