文章目录
一、前置知识点
1.主机清单
'//主机清单可编辑以下文件手动设置'
vim /etc/ansible/hosts '//默认主机清单配置文件'
20 ## [webservers] '//模板解释得比较清楚'
21 ## alpha.example.org
22 ## beta.example.org
23 ## 192.168.1.100
24 ## 192.168.1.110
--详解(例)--
[webserver] '//方括号内设置组名'
www1.example.org '//定义被监控主机,这里也可以是IP地址'
www2.example.org:1234 '//也可以定义远程连接端口,默认为ssh的22端口'
'//如果主机数量较多,且按照序列号排序,可以使用以下列表的方式标识各个主机'
[webserver]
www[01:50].example.org ansible_ssh_user=root ansible_ssh_pass=123123
'//标识01-50的主机在webserver组中,且设置了用户名和密码(变量、免交互)'
[webserver]
db-[a:f].example.org '//还支持字符主机名(需是连续的)'
2.Inventory 变量(自定义)
1)主机变量
[webserver]
www1.magedu.com http_port=80 maxRequesChild=808
'//对主机www1.magedu.com设定变量http_port是80端口,使用时调用该端口变量名即可'
'//maxRequesChild为访问进程变量,最大请求数'
www2.magedu.com http_port=8080 maxRequestsChild=909
2)组变量
[servers:vars] '//服务器组群'
ntp_server=ntp.example.org
nfs_server=nfs.example.org
3)组嵌套
[apache]
httpl.example.org
http2.example.org
[nginx]
ngxl.example.org
ngx2.example.org
[webservers: children]
'//想要同时管理apache与nginx组,只需再定义一个变量即可'
apache
nginx
4)inventory变量参数
--如下图所示-- '//内置变量,不可改动'
参数 | 说明 |
---|---|
ansible_ssh_host | 将要连接的远程主机名与你想要设定的主机别名不一致时,可通过此变量设置 |
ansible_ssh_port | ssh 端口号,若不是默认端口号,可通此变量设置 |
ansible_ssh_user | 默认的 ssh 用户名 |
ansible_ssh_pass | ssh 密码;该方式不安全,建议使用 --ask-pass 或 SSH 秘钥 |
ansible_ssh_private_key_file | ssh 使用的私钥文件;适用于有多个秘钥,而你不想使用 SSH 代理的情况 |
ansible_ssh_common_args | 此设置附加到 sftp、scp 和 ssh 的缺省命令行 |
ansible_sftp_extra_args | 此设置附加到默认 sftp 命令行 |
ansible_scp_extra_args | 此设置附加到默认 scp 命令行 |
ansible_ssh_extra_args | 此设置附加到默认 ssh 命令行 |
ansible_ssh_pipelining | 确认是否使用 SSH 管道;可以覆盖 ansible.cfg 中的配置 |
ansible_shell_type | 目标系统的 shell 类型;默认情况下,命令的执行使用“sh”语法,可设置为“csh”或“fish” |
ansible_python_interpreter | 目标主机的 python 路径;适用于系统中有多个 python |
ansible_*_interpreter | “*”可以是 ruby、perl 或其他语言的的解释器,作用与 ansible_python_interpreter 类似 |
ansible_shell_executable | 将设置 ansible 控制器在目标机器上使用的 shell,覆盖 ansible.cfg 中配置,默认为 /bin/sh |
3.YAML 格式
--概述--
1.YAML是一种标记语言,用来写配置文件的语言,非常强大(简洁)
2.YAML语法与其他语言类似(json),结构通过空格来表示
--语法规则--
1.对大小写敏感
2.使用缩进标识层级关系
3.缩进式不允许使用Tab键,只允许使用空格
4.缩进的空格数目不重要,只要相同层级元素左侧对齐即可
--支持的数据结构--
1.对象:键值对的集合,又称之为映射/哈希/字典
例如,name:Example Developer
'[键:值]'
2.数组:一组按次排列的值,又称为序列/列表
例如,-Apple
-Orange '//一个名字能存储多个值,调用时加“-”即可'
3.纯量:单个的、不可再分的值
例如,number:12.30
sure:true
--示例--
name:zhangsan '//集合'
age:20 '//具体的单个值'
name:lisi
age:21
people: '//定义的对象'
-name:zhangsan '//数组、具体的一些对象'
age:20
-name:lisi
age:22
--什么是对象?--
1.对象当中包含多个属性
2.例如,一张桌子有长、宽、高这三个属性,各用一个单一的变量来表示
--什么是集合/数组?--
1.集合由一个个对象所组成
2.这些对象中的属性集合起来成为一个整体,称之为数组
二、Playbook 剧本的一些基本操作
1.概述 - 组成
playbook 剧本是 ansible 的脚本
通过 task 调用 ansible 的模板将多个 play 组织在一个 playbook 中运行
playbooks 本身由以下部分组成:
- **Tasks:**任务,即调用模板完成得某操作;具有事务性,操作不成功会回滚
- **Variables:**变量(自定义)
- **Templates:**模板;便于修改配置文件,定义自己所需要的内容
- **Handlers:**处理器,当某条件满足时,触发执行的操作
- **Roles:**角色;将其进行分门别类的存放,然后进行统一部署
2.示例使用
示例:
- hosts: webserver '//定义的主机组,即应用的主机'
vars: '//定义变量'
http_port: 80
max_clients: 200 '//自定义变量,采用键值对形式'
user: root '//指定用户'
tasks: '//执行的任务'
- name: ensure apache is at the latest version
'//具体的任务:显示提示信息(apache是否为最新版本);语法使用注意对齐'
yum: pkg=httpd state=latest '//模块:参数(检查是否为最新版本)'
- name: write the apache config file '//写入apache配置文件'
template: src=/srv/httpd.j2 dest=/etc/httpd.conf
'//模板;放至管理端,定义至被管理端'
notify: '//调用(变量);当template有改变后,将会调用以下具体操作'
- restart apache '//改完配置文件需重启服务'
- name: ensure apache is running
service: name=httpd state=started '//输出表示启动,若配置文件有误则会报错/检查'
handlers: '//处理器,触发执行'
- name: restart apache
service: name=httpd state=restarted
'//停止为stopped'
执行:
ansible-playbook [yaml文件名]
例如: ansible-playbook ping.yml
参数: -k(-ask-pass)用来交互输入ssh密码
-K(-ask-become-pass)用来交互输入sudo密码(提权使用大K)
-u 指定用户
补充:
ansible-playbook nginx.yaml --syntax-check
'//检查yaml文件的语法'
ansible-playbook nginx.yaml --list-task
'//检查tasks任务'
ansible-playbook nginx.yaml --list-hosts
'//检查生效的主机'
ansible-playbook nginx.yaml --start-at-task='Copy Nginx.conf'
3.剧本基础 - 撰写你的第一个剧本
--hosts与users--
- hosts: webserver '//指定主机组,可以是一个或多个组'
remote_userz: root '//指定远程主机执行的用户名(root权限)'
例:
[root@ansible ~]# vim /etc/ansible/hosts
'//加入以下主机清单,指向另外两台主机'
25 [webserver]
26 192.168.126.12
27 [mysql]
28 192.168.126.13
[root@ansible ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): '//输入要保存密钥的文件,这里直接选择默认回车'
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): '//输入密码'
Enter same passphrase again: '//再次确认密码'
...
...
[root@ansible ~