自动化运维工具----ansible

时间:2023-03-09 07:21:33
自动化运维工具----ansible

ansible是新出现的运维工具是基于Python研发的糅合了众多老牌运维工具的优点实现了批量操作系统配置、批量程序的部署、批量运行命令等功能。

主要模块以及功能:

1 command

2 user

3 group

4 cron

5 copy

6 file

7 ping

8 yum

9 service

10 shell

11 script

12 setup

13 playbooks

14 忽略错误

15 handler

ansible一般使用普通用户操作,如需使用root权限,可以设置sudo

主要有以下特点

ansible:   yum -y install ansible
模块化:调用特定的模块,完成特定的任务
基于python语言实现,由paramiko,PyYAMAL和Jinja2三个关键模块:
部署简单,agentless
支持主从模式
支持自定义模块
支持Playbook
幂等性:  同一个配置文件中执行的操作允许多少次结果都是一样

配置文件有

    配置文件:
/etc/ansible/ansible.cfg
/etc/ansible/hosts 主机清单

在使用前需要发布ssh密钥来实现自动部署

ssh-keygen -t rsa -P ''   -P密码为空
ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.31.5
ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.31.17
执行命令测试:
ssh 192.168.31.5 'ifconfig'
问题:
ansible 192.168.31.5 -m command -a 'ifconfig'
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit
localhost does not match 'all' [WARNING]: Could not match supplied host pattern, ignoring: 192.168.31.5
解决:需要将主机写入/etc/ansible/hosts文件里面

查看文档说明

ansible-doc -s  command|user..... 查看文档说明

模块说明:

1 command模块

ansible all -m command -a 'ifconfig'

ansible websrvs  -m command -a 'wget -O /tm/文件名 http://路径/文件'  给websrvs组内的主机下载文件

2 user模块   -a 'name=  state={present|absent}  system=.....'

ansible wesrvs -m user -a "name=hacluster state=present" 创建一个非系统用户
ansible wesrvs -m user -a "name=hacluster state=absent" 删除这个用户

3 group模块 -a 'name= gid= state=  system='

4 cron模块,用来生成计划任务 -a  'name= minute= hour= day= month= weekday= job= user=  state='

[root@centos7 ansible]# ansible all -m cron -a 'name="sync time form ntpserver" minute="*/10" job="/sbin/ntpdate us.pool.ntp.org &> /dev/null"'
192.168.31.5 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": [
"sync time form ntpserver"
]
}
192.168.31.17 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": [
"sync time form ntpserver"
]
}
[root@centos7 ansible]# crontab -l
#Ansible: sync time form ntpserver
*/ * * * * /sbin/ntpdate us.pool.ntp.org &> /dev/null 删除计划任务:
ansible all -m cron -a 'name="sync time form ntpserver" state=absent' 指明name加上absent

5  copy模块; 文件复制  -a 'dest= src= mode=  owner= group='

ansible wesrvs -m copy -a 'src=/etc/fstab dest=/tmp/fstab.tmp mode=600' 复制文件,设置文件权限

6 file模块 :设置文件的属性

ansible all -m file -a 'path=/tmp/testdir state=directory'   创建一个目录
ansible all -m file -a 'path=/tmp/fstab.symlink state=link src=/tmp/fstab.tmp' 创建一个链接文件

7 ping模块 就时单独的ping,没有参数

ansible all -m ping

8 yum:执行yum的安装命令

ansible all  -m yum -a 'name=nginx state=latest' 安装包
ansible all -m yum -a 'name=nginx state=absent' 卸载

9 service模块:设置和服务相关的配置 -a  'name= state={started|stopped|restarted} enabled='

ansible wesrvs -m service -a 'name=nginx state=started enabled=yes' 设置nginx启动,并开机自启动

10 shell  :执行命令

ansible all -m shell -a 'echo 123456 | passwd --stdin limi' 给用户创建密码,此用户必须先存在
使用 command模块会报错,需要放入一个子shell中所以使用shell模块

11 script模块:执行脚本

[root@centos7 ansible]# cat /tmp/test.sh
#!/bin/sh
# echo "$(hostname) ansible is good." >/tmp/ansible.txt [root@centos7 ansible]# ll /tmp/test.sh 不给执行权限,远程主机执行 了
-rw-r--r-- root root Sep : /tmp/test.sh
[root@centos7 ansible]# ansible all -m script -a '/tmp/test.sh'

12  setup 获取远程主机的facts,变量,属于查看类别

ansible all -m setup

13 playbooks,综合以上所有的模块

核心元素有:
tasks:任务
variables:变量
templates:模板
handlers:处理器
roles:角色
其中一个特点:中途发送错误,所有已经执行的任务都将回滚,需要更正之后重新执行一次

下面是一个文件例子:hosts,tasks这些冒号之前的文字变成蓝色的就是写正确了,否则就要检查

- hosts: all
remote_user: root
tasks:
- name: add a group // 加上 - 就是一个单独的模块以及处理命令:
group: gid= name=testgroup system=no
- name: excute a command
command: /bin/date 运行结果
[root@centos7 ~]# ansible-playbook test.yaml PLAY [all] *********************************************************************************** TASK [Gathering Facts] ***********************************************************************
ok: [192.168.31.17]
ok: [192.168.31.5] TASK [add a group] ***************************************************************************
changed: [192.168.31.5]
changed: [192.168.31.17] TASK [excute a command] **********************************************************************
changed: [192.168.31.17]
changed: [192.168.31.5] PLAY RECAP ***********************************************************************************
192.168.31.17 : ok= changed= unreachable= failed=
192.168.31.5 : ok= changed= unreachable= failed=

忽略错误的情况
比如执行mkdir 命令,再建用户的话将报错

方法一 /bin/true

tasks:
- name: run this command and ignore the result
command: /usr/bin/somecommand || /bin/true

方法二: ignore_errors

tasks:
- name: run this command and ignore the result
command: /usr/bin/somecommand
ignore_errors: True

handler: 用于当关注的资源发生变化时采取一定的操作

   - hosts: all
remote_user: root
tasks:
- name: ensuse apache laster version
yum: state=latest name=httpd
- name: apache configure file
copy: src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf force=yes
notify: #激活handlers中的name相同名称的服务
- restart apache
handlers:
- name: restart apache
service: name=apache state=restarted