码迷,mamicode.com
首页 > 其他好文 > 详细

ansible安装及使用

时间:2018-12-06 21:24:15      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:book   syntax   12月   handlers   ice   变量   模板   user   控制   

软件环境:rhel7.3 因为低版本有软件依赖太多要解决
server17 172.25.135.17 控制端
server18 172.25.135.18 web 管理
server19 172.25.135.19 mysql 管理
[root@server17 ~]# yum install epel-release-7-11.noarch.rpm
安装epel软件源
[root@server17 ~]# yum install ansibe
[root@server17 ~]# ansible --version #查看版本
[root@server17 ~]# yum install tree
[root@server17 ~]# tree /etc/ansible/
/etc/ansible/
├── ansible.cfg
├── hosts
└── roles
[root@server17 ~]# vim /etc/ansible/hosts #末尾添加
[web]
172.25.135.18
[mysql]
172.25.135.19

[root@server17 ~]# ssh-keygen -t rsa #一路回车做免密
[root@server17 ~]# ssh-copy-id root@172.25.135.18
[root@server17 ~]# ssh-copy-id root@172.25.135.19
[root@server17 ~]# ansible-doc -l #查看ansible所有模块,q退出
conmand模块

格式:ansible+组名 -m 指定模块 -a ‘命令’
[root@server17 ~]# ansible all -m command -a ‘date‘
172.25.135.18 | CHANGED | rc=0 >>
2018年 12月 06日 星期四 07:04:45 EST

172.25.135.19 | CHANGED | rc=0 >>
2018年 12月 06日 星期四 07:04:45 EST

user模块
[root@server17 ~]# ansible all -m user -a ‘name="westos" system=yes‘ #all指定所有组,name=westos,system=yes固定参数。
[root@server17 ~]# ansible all -m user -a ‘name="westos" state=absent‘ #all指定所有组,删除所有用户。
yum模块

用yum模块给被控制主机一键安装众多服务
service模块

启动、关闭、查看服务状态

ansible web -a ‘systemctl status httpd‘ #查看服务状态

ansible web -a ‘systemctl start httpd‘ #开启服务

ansible web -a ‘systemctl stopped httpd‘ #关闭服务
[root@server17 ~]# ansible all -m yum -a ‘name=httpd‘
[root@server17 ~]# ansible web -a ‘systemctl start httpd‘
script模块
[root@server17 ~]# vim westos.sh
[root@server17 ~]# chmod +x westos.sh
[root@server17 ~]# ./westos.sh
ceshi
[root@server17 ~]# ansible mysql -m script -a ‘/root/westos.sh‘
setup模块

ansible mysql -m setup #查看被控制主机信息
[root@server17 ~]# ansible mysql -m ping
ansible的剧本是使用yaml语言进行编写的,基本语法如下
基本语法:
1、大小写
2、同级别要对齐,缩进表示层级
3、缩进不允许使用Tab键
4、缩进空格数不规定,相同元素区分即可
支持的数据结构:
1、对象:键值对集合,又称映射/哈希/字典
例如:name:example 键:值
2、数组:一组按次序排列的值,又称序列/列表/
例如:-apple
3、纯量:单个的、不再分的值
例如:number:12.30
sure:true
play-book剧本
通过tasks调用ansible的模板将多个play组织在一个playbook中运行。
palybook构成部分:
(1)tasks:任务,相当于执行事务
(2)variables:变量(定义场景:hosts文件;剧本中;命令中)
(3)templates:模板
(4)handlers:处理器,满足条件,触发执行操作
(5)roles:角色
[root@server17 ~]# cd /opt/
[root@server17 opt]# ls
[root@server17 opt]# vim index.html
westos.org
[root@server17 ~]# ansible-playbook book.yml

  • hosts: mysql #hosts定义了配置文件中的组名
    remote_user: root #剧本中的演员:root用户,也可以是你推送秘钥的任意用

    tasks: #任务,以下是执行什么任务
    • name: download apache #自行定义的名称
      yum: name=httpd #指定模块,模块后跟相对应的操作
    • name: stopped firewalld
      service: name=firewalld state=stopped
    • name: copy index.html
      template: src=/opt/index.html dest=/var/www/html/index.html #这里>的模板要注意,需要创建推送的文件并写入你指定的内容。
    • name: started apache
      service: name=httpd state=started
      [root@server17 ~]# ansible-playbook book.yml --syntax-check #检查yaml语法
      [root@server17 ~]# ansible-playbook book.yml --list-tasks #检查tasks任务

ansible安装及使用

标签:book   syntax   12月   handlers   ice   变量   模板   user   控制   

原文地址:http://blog.51cto.com/13810716/2327283

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!