一、ansible服务器分组
ansible通过一个主机清单功能来实现服务器分组。
ansible的默认主机清单配置文件为:/etc/ansible/hosts
示例:ansible的服务器分组格式
[web] 组名
web[1:10].com 表示web1.com到web10.com这10台机器
web[a:z].com 表示weba.com到webz.com共26台机器
10.1.1.[11:15] 表示10.1.1.11到10.1.1.15这5台机器
[web]
10.1.1.13:2222 表示10.1.1.13这台,但ssh端口为2222
示例: 定义10.1.1.12:2222这台服务器的别名为web1
[web]
web1 ansible_ssh_host=10.1.1.13 ansible_ssh_port=2222
示例: 没有做免密登录的服务器可以指定用户名与密码
[web]
web1 ansible_ssh_host=10.1.1.13 ansible_ssh_port=2222 ansible_ssh_user=root ansible_ssh_pass="123456"
示例: 利用别名来分组
web1 ansible_ssh_host=10.1.1.13 ansible_ssh_port=2222 ansible_ssh_user=root ansible_ssh_pass="123456"
web1 ansible_ssh_host=10.1.1.12
[web]
web1
web1
二、ansible模块
ansible是基于模块工作的,支持的模块非常的多,这里介绍一些常见的模块,其它的模块在需要用到时再查询
官网模块文档地址: 链接: https://docs.ansible.com/ansible/latest/modules/list_of_all_modules.html
1.查看所有支持的模块
# ansible-doc -l
a10_server Manage A10 Networks AX/SoftAX...
a10_server_axapi3 Manage A10 Networks AX/SoftAX...
a10_service_group Manage A10 Networks AX/SoftAX...
a10_virtual_server Manage A10 Networks AX/SoftAX...
aci_aaa_user Manage AAA users (aaa:User)
2.hostname模块
hostname模块用于修改主机名(注意: 它不能修改/etc/hosts文件)
将其中一远程机器主机名修改为web1.test
master# ansible 10.1.1.12 -m hostname -a 'name=web1.test'
基本格式为: ansible 操作的机器名或组名 -m 模块名 -a "参数1=值1 参数2=值2"
3.file模块
file模块用于对文件相关的操作
示例: 创建一个目录
master# ansible group1 -m file -a 'path=/test state=directory'
示例: 创建一个文件
master# ansible group1 -m file -a 'path=/test/111 state=touch'
示例: 递归修改owner,group,mode
master# ansible group1 -m file -a 'path=/test recurse=yes owner=bin group=daemon mode=1777'
示例: 删除目录(连同目录里的所有文件)
master# ansible group1 -m file -a 'path=/test state=absent'
示例: 创建文件并指定owner,group,mode等
master# ansible group1 -m file -a 'path=/tmp/111 state=touch owner=bin group=daemon mode=1777'
示例: 创建软链接文件
master# ansible group1 -m file -a 'src=/etc/fstab path=/tmp/fstab state=link'
示例: 创建硬链接文件
master# ansible group1 -m file -a 'src=/etc/fstab path=/tmp/fstab2 state=hard'
本篇就先介绍两个模块,大家有空练习一下
每天学一点,就掌握一点
请各位看官移步下一章:运维必备技能ansible(三)
关注我,不迷路。