Ansible入门

Ansible

一、自动化运维平台

puppet perl

saltstack

ansible

二、Ansible介绍

Ansible是一个简单的自动化运维管理工具,基于python语言实现,由Paramiko和PyYAML两个关键模块构建,可用于自动化部署应用、配置、编排task(持续交付、无宕机更新)

1.特点
  • 去中心化,同时执行多台

  • 上手简单,学习曲线平滑

  • 安全,无需安装客户端

  • 配置简单,功能强大,扩展性强

  • 支持API及自定义模块,可以通过python轻松扩展

  • 通过Playbooks来定制强大的配置,状态管理

  • 幂等性,一次操作重复多次结果相同

2.ansible基础架构

在这里插入图片描述

Ansible

ansible 核心程序

Host Inventory

记录了每一个由ansible管理的主机信息,包括主机地址,ssh端口,账号密码等等。可以通过file加载,也可以通过CMDB加载

Playbooks

YAML格式文件,多个任务定义在一个文件中,使用时可以统一调用,剧本用来定义哪些主机需要调用哪些模块来完成的功能

Core Modules

ansible执行任何管理任务都不是由ansible自己完成的,而是由核心模块完成;ansible管理主机前,先调用core inventory中的主机,就可以完成管理主机

Connect Plugins

连接插件,Ansible和Host通信使用

三、环境部署

1.实验环境

Ansible管理端:

hostnamectl set-hostname ansible_center

Ansible

hostnamectl set-hostname nodel

centos6只用hostname就可以改名了,

2.安装

pip安装

yum install openssl-devel zlib-devel

下载python源码包

tar xf Pythonxxx cd

配置

./configure --prefix=/usr/local/python27 && make && make install
ln -s xx xxx

安装pip

crul https://bootstrap.pypa.io/get-pip.py-o get-pip.py
python27 get-pip-py
ls -s xxx xxx
pip27 install ansible

yum安装

yum install -y ansible
3.密钥部署

密钥互信

这是客户端给服务端的。。。。。。
在这里插入图片描述

管理端生成密钥

ssh-keygen

将密钥分发到2个节点中

ssh-copy-id root@10.211.55.13
4.配置文件(/etc/ansible)详解

ansible.cfg 主配置文件

hosts 主机&主机组定义文件

roles 角色目录

5.常用命令

ansible-doc 模块名 查看指定模块文档

ansible-doc -s 模块名 只显示模块关键参数

ansible-doc -l 列出所有模块

ansible-doc --version 查看ansible版本及配置信息

例子:

查看ping模块帮助 ansible-doc ping

使用ping模块测试主机存活 ansible plservers -m ping

使用shell模块执行shell命令 ansible plservers -m shell -a “uptime”

[root@ansinble_center ~]#ansible 192.168.217.131 -m ping
192.168.217.131 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
[root@ansinble_center ~]#ansible 192.168.217.131 -m pong
192.168.217.131 | FAILED! => {
    "msg": "The module pong was not found in configured module paths"
}
[root@ansinble_center ~]#ansible 192.168.217.131 -m shell -a "uptime"
192.168.217.131 | CHANGED | rc=0 >>
 21:38:49 up 12 min,  3 users,  load average: 0.02, 0.06, 0.04

ansible 执行单项任务

ansible-playbook 执行剧本

四、常用模块

1.远程命令模块

模块包括command,scirpt,shell,都可以实现远程shell命令运行

command为ansible默认模块,可以远程执行命令

script功能是在远程主机执行主控制存放的shell脚本

shell功能是执行远程主机的可行性文件

ansible webservers -m command -a “free -m”

[root@ansinble_center ~]#ansible 192.168.217.131 -m command -a "free -m"
192.168.217.131 | CHANGED | rc=0 >>
             total       used       free     shared    buffers     cached
Mem:           980        333        647          0         17        164
-/+ buffers/cache:        151        829
Swap:         4095          0       4095

ansible webservers -m script -a “/home/test.sh 12 34”

ansible webservers -m shell -a “/home/test.sh”

[root@ansinble_center ~]#ansible 192.168.217.131 -m shell -a "/home/test.sh"
192.168.217.131 | CHANGED | rc=0 >>
/home/test.sh 的参数为
[root@ansinble_center ~]#ansible 192.168.217.131 -m shell -a "/home/test.sh 12 34"
192.168.217.131 | CHANGED | rc=0 >>
/home/test.sh 的参数为12 34
2.yum,apt模块
  • 功能

linux 平台软件包管理操作

state

present 表示安装

latest安装最新版

absent卸载

  • 例子

ansible webservers -m apt -a “pkg=zsh state=latest”

ansible webservers -m yum -a “name=zsh state=latest”

[root@ansinble_center ~]#ansible 192.168.217.131 -m yum -a "name=zsh state=latest"
192.168.217.131 | CHANGED => {

在这里插入图片描述

ansible all -m yum -a “name= state=latest”

[root@ansinble_center ~]#ansible all -m yum -a "name= state=latest"
192.168.217.131 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "changes": {
        "installed": [], 
        "updated": []
    }, 
    "msg": "", 
    "rc": 0, 
    "results": [
        ""
    ]
}

ansible webservers -m yum -a “name=xxx.rpm state=present”

[root@ansinble_center ~]#ansible all -m yum -a "name=ftp state=present"
192.168.217.131 | CHANGED => {

在这里插入图片描述

ansible webservers -m yum -a “name=’@Development Tools’ state=present”

[root@ansinble_center ~]#ansible all -m yum -a "name='@Development Tools' state=present"
192.168.217.131 | CHANGED => {
3.service模块
  • 功能

远程主机系统服务管理

state

started 启动服务

stopped 停止服务

restarted 重启服务

  • 例子

ansible webservers -m services -a “name=httpd state=stopped”

ansible webservers -m services -a “name=httpd state=restarted”

ansible webservers -m services -a “name=httpd state=reloaded”

4.iptables防火墙模块

管理linux系统防火墙

ansible webservers -m iptables -a “action=append chain=INPUT protocol=tcp destination_port=80 jump=ACCEPT state=present”

5.lineinfile文件编辑模块

替换文件内容,可以基于正则

ansible all -m lineinfile -a “dest=/etc/selinux/config regexp=’^SELINUX’ line=‘SELINUX=disabled’”

6.setup系统参数

功能

显示操作系统相关信息

7.user模块
  • 功能

远程主机系统用户管理

  • 例子

添加用户

echo 123 | openssl passwd -1 -stdin

1 1 1mZKDgdHW$pW44oofvfz7gu78NN8B7J/

ansible webservers -m user -a “name=alice password= 1 1 1mZKDgdHW$pW44oofvfz7gu78NN8B7J/”

删除用户

ansible webservers -m user -a “name=bob state=absent remove=yes”

8.file模块
  • 功能

文件管理

  • 例子

创建目录

ansible webservers -m file -a “path=/tmp/test state=directory”

[root@ansinble_center ~]#ansible 192.168.217.131 -m file -a "path=/tmp/test state=directory"
192.168.217.131 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "gid": 0, 
    "group": "root", 
    "mode": "0755", 
    "owner": "root", 
    "path": "/tmp/test", 
    "size": 4096, 
    "state": "directory", 
    "uid": 0
}

创建文件

ansible webservers -m file -a “path=/tmp/test state=touch”

[root@ansinble_center ~]#ansible 192.168.217.131 -m file -a "path=/tmp/test state=touch"
192.168.217.131 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "dest": "/tmp/test", 
    "gid": 0, 
    "group": "root", 
    "mode": "0755", 
    "owner": "root", 
    "size": 4096, 
    "state": "file",
    "uid": 0
}

创建链接文件

ansible webservers -m file -a “path=/tmp/test src=/etc/fstab state=link”

创建链接文件

ansible webservers -m file -a “path=/tmp/test state=absent”

[root@ansinble_center ~]#ansible 192.168.217.131 -m file -a "path=/tmp/test state=absent"
192.168.217.131 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "path": "/tmp/test", 
    "state": "absent"
}
9.copy模块
  • 功能

实现主控端向目标主机拷贝文件,类似scp的功能

  • 例子

ansible webservers -m copy -a “src=/home/test.sh dest=/tmp/ owner=root group=root mode=0755”

遇到一些问题

要先在客户端安装

[root@localhost tmp]# yum install -y libselinux-python

然后执行

[root@ansinble_center ~]#ansible 192.168.217.131 -m copy -a "src=/home/alice dest=/tmp/ owner=root group=root mode=0755"
192.168.217.131 | CHANGED => {
    "changed": true, 
    "dest": "/tmp/", 
    "src": "/home/alice"
}
10.远程增量同步synchronzie
  • 功能

增量备份

  • 例子

ansible webservers -m syschronize -a “src= dest=”

ansible webservers -m syschronize -a “compress=yes src= dest=”

11.stat模块
  • 功能

获得远程文件状态信息,包括atime,ctime,mtime,md5,uid,gid等信息

  • 例子

ansible webservers -m stat -a “path=/etc/sysctl.conf”

12.get_url模块
  • 功能

实现远程主机下载执行URL到本地,支持sha256sum文件校验

  • 例子

ansible webservers -m get_url -a “url=http://images.17173.com/2016/acg/2016/01/07/gq0107tt10.jpg dest=/root/ mode=0755 force=yes”

13.cron模块
  • 功能

远程主机crontab配置

  • 例子

ansible webservers -m cron -a “name=‘do something’ hour=5,2 job=‘ls -alh >/dev/null’”

14.mount模块
  • 功能

远程主机分区挂载

  • 例子

ansible webservers -m mount -a “name=/mnt/data src=dev/sd0 fstype=ect4 opts=ro state=present”

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值