ansible入门01

1.批量操作

1.操作系统选型与安装:
1.安装在实体机上:
批量安装:
PXE(预引导执行环境):需要网卡上有DHCP客户端去加载bootloadder
cobbler:
kickstack:
2.安装在虚拟机上:克隆、虚拟机镜像模板、支持批量部署操作
3.安装在云主机上:
 
2.系统配置:
常用的批量配置工具:
puppet(ruby)
saltstack(python)
ansible(python)
chef:
。。。

批量命令执行工具:

fabric:python语言
func:
 
3.程序发布:
手动发布:
脚本执行:不够灵活
公司内部专用运维程序框架:集成运维工具,需要运维研发
 
要求:
1.不能影响用户体验
2.系统不能停机
3.不能导致系统故障或造成系统完全不可用
 
要基于灰度进行发布:
1.基于主机:发布时,先对部分主机进行发布
2.基于用户:在进行发布的时候,先对部分用户进行测试
发布路径:
在调度器上下线一批主机(标记为维护模式)--》关闭服务--》部署新版本--》启动服务--》在调度器上启动这一批主机
 
按需启动或停止:弹性计算
 
运维的三个阶段:稳定、批量、自动化
 
4.ansible:可以提供系统配置和命令批量执行(在主机数量相对较小时)
 
运维工具的分类:
1.无agent:大部分基于ssh协议,例ansible、fabric
2.有agent:例func、puppet

需要认证、需要确保连接的安全性

2.ansible

anible的特性:
1.模块化:调用特定的模块、完成特定的任务
2.基于python语言实现,由paramiko、PyYAML和Jinja2三个关键模块
3.部署简单、无agent
4.支持自定义模块
5.支持palybook
6.支持幂等性:即一个命令执行一次和执行n次的效果是相同的
 
ansible需要在不同的系统上分别执行不同的命令,需要提前进行判断。
 
安装:直接使用yum或编译安装
配置文件:/etc/ansible/ansible.cfg
主机清单:/etc/ansible/hosts
主程序:
ansible:
ansible-doc -l:列出所有可支持的模块
 

编辑主机组

2.1 预先进行ssh认证

ssh-keygen
ssh-copy-id

2.2 使用ansible

简单使用:ansible HOST_PATTERN -m NOD_NAME -a  MOD_ARGS
例:
# ansible all -m ping
192.168.61.144 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
192.168.61.142 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
192.168.61.143 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

2.3 ansible常用模块

列出所有支持的模块:ansible-doc -l
列出模块的用法:ansible-doc -s ping #显示ping模块的用法

2.3.1 command模块:

在远程主机运行命令;
ansible all -m command -a "date 051320422016" #直接修改hosts列表中主机的时间
ansible webservers -m command -a date   #显示组中的主机的系统时间
ansible all -m command -a "ls /var"
ansible all -a "ls /var" #命令可省
ansible webservers -a 'useradd user1'
 
command模块不支持管道,如下:
ansible webserver -m command -a "echo test1234|passwd  --stdin test"
192.168.61.142 | SUCCESS | rc=0 >>
test1234|passwd --stdin test

192.168.61.143 | SUCCESS | rc=0 >>
test1234|passwd --stdin test
 
上面的命令并没有执行成功。

2.3.2 shell模块:

在远程主机在shell进程下运行命令,支持shell特性,如管道等;
ansible all -m shell -a 'cat /tmp/test' #显示出列表中主机/tmp/test文件中的内容
ansible all -m shell -a "date"   #显示组中的主机的系统时间
ansible all -m shell -a "systemctl status httpd"
ansible all -m shell -a "echo mageedu|passwd --stdin yser1" #支持管道

2.3.3 copy模块:

把当前主机上的文件批量复制到远程主机上。
 Copies files to remote locations.
用法:
(1) src=  dest=:直接使用现有的文件
(2) content=  dest=:创建文本内容复制过去
 owner, group, mode
ansible all -m copy -a "src=./abc/test dest=/tmp owner=root group=root mode=600" 
#将当前主机的文件复制到远程主机
ansible all -m copy -a "src=/etc/fstab dest=/tmp/fstab owner=zhangpf"
ansible all -m copy -a "content='this is just a test' dest=/tmp/test.txt" #直接生成内容
 
复制的时候,如果远程主机已经有同名文件,那么直接覆盖。
例:
# ansible dbserver -m copy -a "src=/etc/hosts dest=/tmp owner=root group=root mode=600"
192.168.61.144 | SUCCESS => {
    "changed": true, 
    "checksum": "2142bf6a810d1fbca7759e922f2c806301923b2c", 
    "dest": "/tmp/hosts", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "a92245f0d6e4a4a2f8d7a2741d367dff", 
    "mode": "0600", 
    "owner": "root", 
    "size": 266, 
    "src": "/root/.ansible/tmp/ansible-tmp-1503209054.65-1091359116985/source", 
    "state": "file", 
    "uid": 0
}
192.168.61.143 | SUCCESS => {
    "changed": true, 
    "checksum": "2142bf6a810d1fbca7759e922f2c806301923b2c", 
    "dest": "/tmp/hosts", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "a92245f0d6e4a4a2f8d7a2741d367dff", 
    "mode": "0600", 
    "owner": "root", 
    "size": 266, 
    "src": "/root/.ansible/tmp/ansible-tmp-1503209056.66-183234739097730/source", 
    "state": "file", 
    "uid": 0
}
 

2.3.4 cron 模块:

编辑远程主机上的定时任务。
minute=
day=
month=
weekday=
hour=
job=
name=任务名
state=:任务状态
present:创建
absent:删除
ansible all -m cron -a "minute=*/5 job='/sbin/ntpdate 172.18.0.1 &>/dev/null' name=Synctime" #每五分钟通过主机172.18.0.1同步一次时间
ansible all -m cron -a "state=absent name=Synctime" #删除Synctime这个任务

2.3.5 fetch模块:

Fetches a file from remote nodes,从远程主机拉取文件到本地,通常用来做备份。
来源必须是文件,保存路径必须是一个文件夹。 
例:
# ansible webserver -m fetch -a "src=/etc/passwd dest=/home/test"
192.168.61.143 | SUCCESS => {
    "changed": true, 
    "checksum": "8044fec23fddf2c8cb9371261d12f85652dc52f3", 
    "dest": "/home/test/192.168.61.143/etc/passwd", 
    "md5sum": "bd75ad746a725360b719fc544f98ce3c", 
    "remote_checksum": "8044fec23fddf2c8cb9371261d12f85652dc52f3", 
    "remote_md5sum": null
}
192.168.61.142 | SUCCESS => {
    "changed": true, 
    "checksum": "8044fec23fddf2c8cb9371261d12f85652dc52f3", 
    "dest": "/home/test/192.168.61.142/etc/passwd", 
    "md5sum": "bd75ad746a725360b719fc544f98ce3c", 
    "remote_checksum": "8044fec23fddf2c8cb9371261d12f85652dc52f3", 
    "remote_md5sum": null
}
[root
@node003 ansible]# ll /home/test 总用量 8 drwxr-xr-x 3 root root 4096 8月 20 14:19 192.168.61.142 drwxr-xr-x 3 root root 4096 8月 20 14:19 192.168.61.143

2.3.6 file模块:

Sets attributes of files,设定文件属性
用法:
(1) 创建链接文件:*path=  src=  state=link
(2) 修改属性:path=  owner= mode= group=
(3) 创建目录:path=  state=directory 
ansible webservers -m file -a "dest=/src/foo/a.txt mode=600"
ansible webservers -m file -a "dest=/src/foo/b.txt mode=600 owner=test group=test" #类似于chmod
ansible webservers -m file -a "dest=/tmp/test/test.sh mode=755 owner=zhangpf group=root state=directory" 
#报错,/tmp下没有/test目录,不能递归创建文件
ansible webservers -m file -a "dest=/tmp/test/b.txt state=absent" #类似于删除文件或目录
ansible all -m file -a "src=/tmp/fstab path=/tmp/fstab.link state=link" 
#创建链接文件,链接到的是服务器主机的/etc/fsatb文件而非远程主机的/etc/fsatb文件
ansible all -m file -a "path=/tmp/testc state=link"

2.3.7 hostname模块:

Manage hostname,变更主机名,在这里要使用变量才能实现批量修改
ansible webserver -m hostname -a "name=zhangpf" #直接修改主机名

2.3.8 pip模块:

Manages Python library dependencies.管理python库的依赖性,自动安装相关的python模块。

2.3.9 yum模块:

Manages packages with the `yum' package manager,管理yum程序包。
name=:程序包名称,可以带版本号;
state=
present(安装)
latest(升级)
absent(卸载)
ansible all -m yum -a "name=httpd state=present"
 

2.3.10 service模块:管理服务

name= #服务名
state= #设置服务的状态
running
started
stopped
restarted
reloaded

enabled=:是否设置此服务开机启动

runlevel=:在哪个运行级别下开机运行此服务(init 0-6)
ansible all -m service -a "name=httpd state=started enabled=1"

2.3.11 user模块:管理用户账号

name=
system=:设置系统账号
uid=
shell=:设置默认shell类型
group=:设置基本组
groups=:设置附加组
comment=:设置注释信息
home=:设置家目录
password:设置密码,可以使用openssl加密密码
ansible all -m user -a "name=test uid=1200 groups=zhangpf comment='this is a test' shell=/bin/sh" #创建用户
ansible all -m user -a "name=test state=absent"  #删除
ansible all -m user -a "name=test passwd=123456" #添加密码

2.3.12 setup模块:获取变量

ansible 172.18.47.48 -m setup
ansible all -m setup
ansible all -m setup --tree /tmp/facts #搜集系统信息并以主机名为文件名分别保存在/tmp/facts 目录
ansible all -m setup -a 'filter=ansible_*_mb' #只显示内存相关信息
ansible all -m setup -a 'filter=ansible_enp0s3' #只显示网卡相关信息
steup获取到的主机的信息中的变量都可以使用

3.YAML语法

yaml的语法和其他高级语言类似,并且可以简单表达清单、散列表、标量等数据结构,其结构通过空格来展示,序列里的项用“-”来表示,Map里的键值对用“:”来分割。可以使用列表、字典、键值对等

3.1 palybook的核心元素

多个相关联的操作,通过读取yaml格式的配置文件,多个操作一次执行完毕
Hosts:定义远程主机
Tasks:任务列表
Variables:定义变量
Templates:包含了模板语法的文本文件;不能使用copy复制,需要在使用前用变量替换为目标主机的值。
Handlers:处理器,与task类似,但是只能由特定条件触发的任务;定义 task 执行完成以后需要调用的任务
  当达到某一条件后进行触发,执行某些操作
Roles:角色
 
playbook就是一个或者多个play所组成的列表

3.2 playbook的基础组件:

Hosts:运行指定任务的目标主机;可以是一个主机组,也可以是一个主机
remoute_user: 在远程主机上执行任务的用户;可以全局指定,也可以每台主机分别指定
        sudo_user:定义切换至哪个用户执行(可选)
tasks:任务列表
定义模块,指明模块参数;
指明任务的格式有两种:
(1) action: module arguments
(2) module: arguments:这种方法更通用一些
注意:shell和command模块后面直接跟命令,而非key=value类的参数列表;
(1) 某任务的状态在运行后为changed时,可通过“notify”通知给相应的handlers;
(2) 任务可以通过"tags“打标签,而后可在ansible-playbook命令上使用-t指定进行调用;使用","(逗号)隔开可以使用多个标签
例:
- hosts: all       #定义使用的主机组,我们可以定义多个hosts
  remote_user: root #远程执行的用户
  tasks:
  - name: create a user test1     #第一个任务
    user: name=test1 system=true uid=666   #调用user模块执行任务
  - name: create a user test2
    user: name=test2 system=true uid=667
测试运行,使用ansible-playbook --check
# ansible-playbook --check first.yaml 

PLAY [all] *********************************************************************

TASK [Gathering Facts] *********************************************************
ok: [192.168.61.142]
ok: [192.168.61.144]
ok: [192.168.61.143]

TASK [create a user test1] *****************************************************
changed: [192.168.61.144]
changed: [192.168.61.143]
changed: [192.168.61.142]

TASK [create a user test2] *****************************************************
changed: [192.168.61.142]
changed: [192.168.61.143]
changed: [192.168.61.144]

PLAY RECAP **********************************************************************
192.168.61.142             : ok=3    changed=2    unreachable=0    failed=0   
192.168.61.143             : ok=3    changed=2    unreachable=0    failed=0   
192.168.61.144             : ok=3    changed=2    unreachable=0    failed=0   

3.3 使用ansible-playbook:

测试运行:ansible-playbook --check first.yaml
列出运行任务的主机:ansible-playbook --list-hosts first.yaml
# ansible-playbook --list-hosts first.yaml
#显示如下

playbook: first.yaml

  play #1 (all): all    TAGS: []
    pattern: [u'all']
    hosts (3):
      192.168.61.143
      192.168.61.142
      192.168.61.144
正式运行:ansible-playbook first.yaml
例:
#在/etc/ansible下创建文件web.yaml
- hosts: webservers
  remote_user: root
  tasks:
  - name: install httpd
    yum: name=httpd state=present
  - name: install httpd conf
    copy: src=./web/httpd.conf.v1 dest=/etc/httpd/conf/httpd.conf
  - name: start httpd
    service: name=httpd state=reloaded
  - name: execute ss command
    shell: ss -tnl | grep :80
功能:在webserver组中的主机上(1)用yum安装httpd程序,(2)把本地的配置文件复制到远程主机,(3)配置远程主机上的http服务,(4)检查80端口是否被监听。
注意:在yaml中其他模块多为键值对形式,但是shell和command模块直接写即可。

3.4 处理器handlers

只有在特定条件下才会触发,接收到其它任务的通知时被触发;
某任务的状态在运行后为changed时,可通过“notify”通知给相应的handlers;
例:
- hosts: webservers
  remote_user: root
  tasks:
  - name: install httpd
    yum: name=httpd state=present
  - name: install httpd conf
    copy: src=./web/httpd.conf.v1 dest=/etc/httpd/conf/httpd.conf
    notify: restart httpd
  - name: start httpd
    service: name=httpd state=started
  handlers:
  - name: restart httpd
    service: name=httpd state=restarted

3.5 tags:任务打标签

任务可以通过"tags“打标签,而后可在ansible-playbook命令上使用-t指定进行调用,再次执行的时候只执行一部分操作;
使用","(逗号)隔开可以使用多个标签。如下:
- hosts: webservers
  remote_user: root
  tasks:
  - name: install httpd
    yum: name=httpd state=present
    tags: install
  - name: install httpd conf
    copy: src=./web/httpd.conf.v1 dest=/etc/httpd/conf/httpd.conf
    tags: install
    notify: restart httpd
  - name: start httpd
    service: name=httpd state=started
    tags: starthttpd
  handlers:
  - name: restart httpd
    service: name=httpd state=restarted
执行如下:
ansible-playbook -t install web.yaml #一次执行install标签的两个任务
ansible-playbook --check -t install,start web.yaml #一次执行两个标签install、start任务

3.6 variables:变量

ansibleplaybook中的多数变量是由steup模块显示的内容给出的
ansible 172.18.47.48 -m setup
ansible all -m setup
ansible all -m setup --tree /tmp/facts 
#搜集系统信息并以主机名为文件名分别保存在/tmp/facts 目录

ansible all -m setup -a 'filter=ansible_*_mb' #只显示内存相关信息
ansible all -m setup -a 'filter=ansible_enp0s3' #只显示网卡相关信息
(1) facts:显示的系统变量可以直接调用;
(2) ansible-playbook命令的命令行中的自定义变量:-e VARS, --extra-vars=VARS
(3) 通过roles传递变量;
(4) Host Inventory
(a) 向不同的主机传递不同的变量;
IP/HOSTNAME  varaiable=value var2=value2
(b) 向组中的主机传递相同的变量;
[groupname:vars]
variable=value
例:
#修改hosts文件
[webserver:vars]
http_port=8080
调用变量:使用双花括号:{{}}
例:安装tree程序
- hosts: all
  remote_user: root
  tasks:
  - name: install {{ pkname }}
    yum: name={{ pkname }} state=present
ansible-playbook --check -e pkname=tree testavr.yaml
ansible-playbook -e pkname=tree testavr.yaml
使用hosts文件中配置的变量:
修改hosts文件配置:
[webserver]
192.168.61.142  hname=www1
192.168.61.143  hname=www2
yaml文件如下:
- hosts: webserver
  remote_user: root
  tasks:
  - name: set hostname
    hostname: name={{ hname }}

3.7 不使用密钥连接

invertory参数:
在连接的时候不使用基于密钥的认证:编辑hosts文件
这种参数用于定义ansible远程连接目标主机时使用的参数,而非传递给playbook的变量;
还是有其他常用变量如下:
ansible_ssh_host
ansible_ssh_port
ansible_ssh_user
ansible_ssh_pass
ansbile_sudo_pass
...





posted @ 2017-08-22 09:36  flyme123  阅读(232)  评论(0编辑  收藏  举报