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

自动化运维工具----ansible的更多相关文章

  1. 自动化运维工具Ansible详细部署 (转载)

    自动化运维工具Ansible详细部署 标签:ansible 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://sofar.blog. ...

  2. 自动化运维工具Ansible详细部署 - 人生理想在于坚持不懈 - 51CTO技术博客

    自动化运维工具Ansible详细部署 - 人生理想在于坚持不懈 - 51CTO技术博客 自动化运维工具Ansible详细部署

  3. CentOS7Linux中自动化运维工具Ansible的安装,以及通过模块批量管理多台主机

    使用自动化运维工具Ansible集中化管理服务器 Ansible概述 Ansible是一款为类Unix系统开发的自由开源的配置和自动化工具.它用Python写成,类似于saltstack和Puppet ...

  4. 自动化运维工具Ansible介绍

    一个由 Python 编写的强大的配置管理解决方案.尽管市面上已经有很多可供选择的配置管理解决方案,但他们各有优劣,而 ansible 的特点就在于它的简洁. 让 ansible 在主流的配置管理系统 ...

  5. 在CentOS7.6上安装自动化运维工具Ansible以及playbook案例实操

    前言 Ansible是一款优秀的自动化IT运维工具,具有远程安装.远程部署应用.远程管理能力,支持Windows.Linux.Unix.macOS和大型机等多种操作系统. 下面就以CentOS 7.6 ...

  6. 自动化运维工具-Ansible基础

    目录 自动化运维工具-Ansible基础 什么是Ansible 同类型软件对比 Ansible的功能及优点 Ansible的架构 Ansible的执行流程 安装Ansible ansible配置文件 ...

  7. 自动化运维工具-Ansible之7-roles

    自动化运维工具-Ansible之7-roles 目录 自动化运维工具-Ansible之7-roles Ansible Roles基本概述 Ansible Roles目录结构 Ansible Roles ...

  8. 自动化运维工具-Ansible之6-Jinja2模板

    自动化运维工具-Ansible之6-Jinja2模板 目录 自动化运维工具-Ansible之6-Jinja2模板 Ansible Jinja2模板概述 Ansible Jinja2模板使用 Ansib ...

  9. 自动化运维工具-Ansible之5-流程控制

    自动化运维工具-Ansible之5-流程控制 目录 自动化运维工具-Ansible之5-流程控制 playbook条件语句 单条件 多条件 多条件运算 示例 playbook循环语句 with_ite ...

  10. 自动化运维工具-Ansible之3-playbook

    自动化运维工具-Ansible之3-playbook 目录 自动化运维工具-Ansible之3-playbook PlayBook初识 YAML语法 PlayBook部署httpd PlayBook实 ...

随机推荐

  1. SyntaxError: Non-ASCII character '\xe4' in file t.py on line 3, but no encoding declared

    问题 报错代码 #!/usr/bin/python s = "你好" print s 执行报错: File "t.py", line 3 SyntaxError ...

  2. CSS 仿 iOS 系统通知数字样式

    /** 仿 iOS 系统通知数字样式 **/ .num_span{ background-color: #f00; background-image: -webkit-linear-gradient( ...

  3. Spring Cloud(Dalston.SR5)--Hystrix 监控

    在服务调用者加入 Actuator ,可以对服务调用者的健康情况进行实时监控,例如,断路器是否打开.当前负载情况等. 服务调用者 需要增加 actuator依赖, 修改 POM.xml 中增加以下依赖 ...

  4. Python程序的执行过程 解释型语言和编译型语言

    转载地址:http://blog.csdn.net/lujiandong1/article/details/50067655 1. Python是一门解释型语言? 我初学Python时,听到的关于Py ...

  5. 使用Vivado的block design

    使用Vivado的block design (1)调用ZYNQ7 Processing System (2)配置ZYNQ7系统 (3)外设端口配置 根据开发板原理图MIO48和MIO49配置成了串口通 ...

  6. idea下的调试配置

    react和ts的整合 https://github.com/Microsoft/TypeScript-React-Starter vue的 https://github.com/ducksoupde ...

  7. 在Outlook客户端使用SSL加密,弹出安全证书警告的解决方法。

    这是使用自己的域名.第三方的邮件系统是产生的问题. 如万网的邮箱系统,但在Outlook中,设置的pop3/imap/smtp的域名是你自己的域名. imap.youname.domain pop3. ...

  8. object.key 对象的键排序 可能出现的问题

    // 09-集成提测工作流var node_list_info09 = { '090101': '客户端集成自测', '090201': '编译配置', '090202': '编译出包', '0903 ...

  9. 【git】之修改git仓库地址

    方法1 git remote set-url origin <url> 方法2 git remote rm origin git remote add origin [url] 方法三 直 ...

  10. 使用jquery.mCustomScrollbar自定义滚动条(1)

    参考博客:https://blog.csdn.net/cdnight/article/details/41351505 api网址:http://manos.malihu.gr/jquery-cust ...