核心组件
ansbile:核心程序
modules:包括ansible自带的核心模块以及自定义模块
plugins:完成模块功能的补充,包括连接插件,邮箱插件
palybooks:剧本,定义ansbile多任务配置文件,由ansible自动执行
inventory:定义ansbile管理的主机清单
connection plugins:负责和被监控端实现通信
Ansible分为控制端(控制节点)和被控制端(受管主机)
加载自己的配置文件,默认为/etc/ansible/ansible.cfg
加载自己对应的模块文件
通过ansible将模块或命令生成对应的临时py文件,并将该文件传输至远程服务器
对应执行用户的家目录的.ansible/tmp/xx.py文件
给文件+x执行权限
执行并将返回结果,删除临时py文件,sleep 0 退出
注:
yum install -y ansible
定义Ansible将要管理的一批主机
分类
静态清单:人为的写入被管理端的IP或主机名称(INI样式,每行一个)
动态清单:通过脚本文件解析出来的IP或主机名称
默认主机清单配置文件为/etc/ansible/hosts,可在该文件中进行服务器分组/主机分组
all
主机组含有清单中明确列出的每一个主机ungrouped
主机组含有清单中明确列出、但不属于任何其他组的每一个主机查看/配置当前主机清单的位置
ansible --version
查看当前配置文件的位置,过滤/配置inventory
字段,默认为/etc/ansible/hosts
[student@workstation ~]$ ansible --version|grep .cfg
config file = /etc/ansible/ansible.cfg
[student@workstation ansible]$ grep 'inventory' /etc/ansible/ansible.cfg
#inventory = /etc/ansible/hosts
...
定义嵌套组
:children
的主机组名称来实现# vim /etc/ansible/hosts
[dev]
servera
[test]
serverb
[webservers:children]
dev
test
# 此时webservers组既包含了dev组内的主机,也包含了test组内的主机
定义范围组
[START:END]
范围匹配从START到END的所有值192.168.[1:4].[0:255] #此例包含192.169.1.0到192.168.7.255中所有的IP地址
server[01:20] # 此例包含server01到server20,注意此例不匹配server1-server9
[a:c].com # 此例包含a.com到c.com
验证清单
ansible 主机名/组名 --list-host
,-i指定清单文件[student@workstation ~]$ mkdir ansible
[student@workstation ~]$ cd ansible/
[student@workstation ansible]$ vim inventory
[student@workstation ansible]$ ansible -i inventory webservers --list-host
hosts (2):
servera
serverb
补充
定义端口
10.1.1.13:2222
定义别名(定义10.1.1.12:2222这台服务器的别名为nginx1)
nginx1 ansible_ssh_host=10.1.1.13 ansible_ssh_port=2222
指定用户名和密码
nginx1 ansible_ssh_host=10.1.1.13 ansible_ssh_port=2222 ansible_ssh_user=root ansible_ssh_pass="123456"
利用别名分组
ginx1 ansible_ssh_host=10.1.1.13 ansible_ssh_port=2222 ansible_ssh_user=root ansible_ssh_pass="123456"
nginx2 ansible_ssh_host=10.1.1.12
默认配置文件在/etc/ansible/ansible.cfg
编写简易的配置文件
#
和;
作为注释字符[student@workstation ansible]$ pwd
/home/student/ansible
[student@workstation ansible]$ vim ansible.cfg
[defaults]
inventory=/home/student/ansible/inventory # 配置用户清单
remote_user=student # 远程执行用户
ask_pass=no # 远程登录是否需要密码
[privilege_escalation]
become=True # 是否提权,yes==True
become_method=sudo # 提权采用的方式sudo
become_user=root # 提权成谁
become_ask_pass=False # 提权是否询问密码,False==no
[student@workstation ansible]$ ansible --version
ansible 2.9.21
config file = /home/student/ansible/ansible.cfg
configured module search path = ['/home/student/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.6/site-packages/ansible
executable location = /usr/bin/ansible
python version = 3.6.8 (default, Mar 18 2021, 08:58:41) [GCC 8.4.1 20200928 (Red Hat 8.4.1-1)]
# 此时再验证清单就不需要-i指定用户清单了
[student@workstation ansible]$ ansible webservers --list-hosts
hosts (2):
servera
serverb
ansible host-pattern -m module [-a 'module arguments'] [-i inventory]
官网模块文档地址: https://docs.ansible.com/ansible/latest/modules/list_of_all_modules.html
ansible-doc 模块名
:查看模块参数ansible-doc -l
:可以查询ansible具有的所有模块[student@workstation ansible]$ ansible all -m ping
servera | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
serverb | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
# 这里建议用ip作受控主机标识
[student@workstation ansible]$ ansible servera -m hostname -a 'name=server1' -b -u root ;
servera | CHANGED => {
"ansible_facts": {
"ansible_domain": "",
"ansible_fqdn": "server1",
"ansible_hostname": "server1",
"ansible_nodename": "server1",
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "server1"
}
官网:https://docs.ansible.com/ansible/latest/modules/file_module.html#file-module
操作文件(创建, 删除, 软硬链接等)
常用参数
- name: Sets attributes of files
file:
force: # 需要在两种情况下强制创建软链接,一种是源文件不存在,但之后会建立的情况下;另一种是目标软链接已存在,需要先取消之前的软链,然后创建新的软链,有两个选项:yes|no
group: # 定义文件/目录的属组
mode: # 定义文件/目录的权限
owner: # 定义文件/目录的属主
path: # 必选项,定义文件/目录的路径
recurse:# 递归设置文件的属性,只对目录有效
src: # 被链接的源文件路径,只应用于state=link的情况
dest: # 被链接到的路径,只应用于state=link的情况
state:
directory:# 如果目录不存在,就创建目录
file: # 即使文件不存在,也不会被创建
link: # 创建软链接
hard: # 创建硬链接
touch: # 如果文件不存在,则会创建一个新的文件,如果文件或目录已存在,则更新其后修改时间
# 创建目录
ansible mygroup -m file -a 'path=/test state=directory'
# 创建文件
ansible mygroup -m file -a 'path=/test/file1 state=touch'
# 创建软连接
ansible mygroup -m file -a 'src=/etc/fstab path=/tmp/fstab state=link'
# 创建硬链接
ansible mygroup -m file -a 'src=/etc/fstab path=/tmp/fstab1 state=hard'
# 递归修改owner,group,mode
ansible mygroup -m file -a 'path=/test recurse=yes owner=bin group=daemon mode=1777'
# 删除文件
ansible mygroup -m file -a 'path=/test/file1 state=absent'
文件远程拷贝,默认会覆盖
所有需要修改的远程文件,都可以在本地改好,通过copy模块传至远程
拷贝目录要注意/
/etc/
表示拷贝etc目录里的所有文件/etc
表示拷贝整个etc目录官网https://docs.ansible.com/ansible/latest/modules/copy_module.html#copy-module
常用参数
- name: Copies files to remote locations
copy:
backup:在覆盖之前,将源文件备份,备份文件包含时间信息。
content:用于替代“src”,可以直接设定指定文件的值
dest:必选项。要将源文件复制到的远程主机的绝对路径
directory_mode:递归设定目录的权限,默认为系统默认权限
force:强制覆盖目的文件内容,默认为yes
others:所有的file模块里的选项都可以在这里使用
src:被复制到远程主机的本地文件,可以是绝对路径,也可以是相对路径。如果路径是一个
目录,它将递归 复制
# 拷贝
ansible mygroup -m copy -a 'src=/etc/fstab dest=/tmp/fstab'
# 如果文件存在,不覆盖
ansible mygroup -m copy -a 'src=/etc/fstab dest=/tmp/fstab force=no'
# 如果文件存在,先备份,后覆盖
ansible mygroup -m copy -a 'src=/etc/fstab dest=/tmp/fstab backup=yes'
# 如果想要目标非空目录内的内容完全一致,copy模块不能做到
# 因为copy模块默认不会删除目标目录内的多余文件
# 可以先删除目标文件|目录,再使用copy模块实现
ansible mygroup -m file -a 'path=/etc/yum.repos.d/ state=absent'
ansible mygroup -m copy -a 'src=/etc/yum.repos.d dest=/etc/'
ansible 192.168.180.20 -m fetch -a 'src=/tmp/fstab dest=/tmp/'
用户管理
https://docs.ansible.com/ansible/latest/modules/user_module.html#user-module
常用参数
comment # 用户的描述信息
createhom # 是否创建家目录
force # 在使用`state=absent'是, 行为与`userdel --force'一致.
group # 指定基本组
groups # 指定附加组,如果指定为('groups=')表示删除所有组
home # 指定用户家目录
name # 指定用户名
password # 指定用户密码
remove # 在使用 `state=absent'时, 行为是与 `userdel --remove'一致.会尝试删除与用户关联的目录
shell # 指定默认shell
state #设置帐号状态,不指定为创建,指定值为absent表示删除
system # 当创建一个用户,设置这个用户是系统用户。这个设置不能更改现有用户
uid #指定用户的uid
update_password # 更新用户密码
expires #指明密码的过期时间
# 创建普通用户
ansible mygroup -m user -a 'name=zhangsan state=present'
# 创建系统用户
ansible mygroup -m user -a 'name=aaa state=present system=yes shell="/sbin/nologin"'
# 创建用户,指定uid,指定密码
ansible mygroup -m user -a "name=lisi state=present uid=2000 password='1'"
# 用户添加到组中
ansible mygroup -m user -a 'name=zhangsan state=present group=1500'
# 删除用户
ansible mygroup -m user -a 'name=zhangsan state=absent'
# 删除用户的同时删除家目录
ansible mygroup -m user -a 'name=aaa state=absent remove=yes'
- name: 添加或删除组
action: group
gid # 设置组的GID号
name # 管理组的名称
state # 指定组状态,默认为创建,设置值为absent为删除
system # 设置值为yes,表示为创建系统组
# 创建
ansible mygroup -m group -a 'name=group_test state=present gid=1500'
# 删除组(先删组中用户)
ansible mygroup -m user -a 'name=zhangsan state=absent'
ansible mygroup -m group -a 'name=group_test state=absent'
# 创建
ansible mygroup -m cron -a 'name=cron1 user=root job="touch /tmp/111" minute=*/2 hour=1'
# 删除
ansible mygroup -m cron -a 'name=cron1 state=absent'
软件包安装与卸载
https://docs.ansible.com/ansible/latest/modules/yum_module.html#yum-module
常用参数
- name: Manages packages with the yum package manager
yum:
conf_file #设定远程yum安装时所依赖的配置文件。如配置文件没有在默认的位置。
disable_gpg_check #是否禁止GPG checking,只用于`present' or `latest'。
disablerepo #临时禁止使用yum库。 只用于安装或更新时。
enablerepo #临时使用的yum库。只用于安装或更新时。
name #所安装的包的名称
state #present安装,latest安装新的, absent 卸载软件。
update_cache #强制更新yum的缓存
# 安装
ansible mygroup -m yum -a 'name=vsftpd state=present'
# 安装最新版
ansible mygroup -m yum -a 'name=httpd,httpd-devel state=latest'
# 卸载
ansible mygroup -m cron -a 'name=cron1 state=absent'
控制服务的启动,关闭,开机自启动等
https://docs.ansible.com/ansible/latest/modules/service_module.html#service-module
常用参数
- name: Manage services
service:
arguments #命令行提供额外的参数
enabled #设置开机启动。
name #服务名称
runlevel #开机启动的级别,一般不用指定。
sleep #在重启服务的过程中,是否等待。如在服务关闭以后等待2秒再启动。
state #started启动服务, stopped停止服务, restarted重启服务, reloaded重载
# 启动服务,并设置为开机自启
ansible mygroup -m service -a 'name=vsftpd state=started enabled=on'
# 关闭服务,并设为开机不自动启动
ansible mygroup -m service -a 'name=vsftpd state=stopped enabled=false'
在远程机器上执行本地脚本
https://docs.ansible.com/ansible/latest/modules/script_module.html#script-module
示例
# 准备脚本
# vim /tmp/1.sh
#!/bin/bash
yum install -y mariadb-server &> /dev/null
systemctl start mariadb
systemctl enable mariadb
mysql << EOF
create database abc;
quit
EOF
# 远程执行脚本
ansible mygroup -m script -a '/tmp/1.sh'
# ansible mygroup -m shell -a 'useradd xiaoming'
192.168.139.20 | CHANGED | rc=0 >>
192.168.139.30 | CHANGED | rc=0 >>
# ansible mygroup -m shell -a 'id xiaoming'
192.168.139.20 | CHANGED | rc=0 >>
uid=2002(xiaoming) gid=2002(xiaoming) 组=2002(xiaoming)
192.168.139.30 | CHANGED | rc=0 >>
uid=2002(xiaoming) gid=2002(xiaoming) 组=2002(xiaoming)
# ansible mygroup -m shell -a 'tail -1 /etc/passwd'
192.168.139.20 | CHANGED | rc=0 >>
xiaoming:x:2002:2002::/home/xiaoming:/bin/bash
192.168.139.30 | CHANGED | rc=0 >>
xiaoming:x:2002:2002::/home/xiaoming:/bin/bash
ansible all -m setup #获取远程主机的详细信息
这么多模块记不住吧,大概留个印象即可,后面写剧本多写写,再带着ansible-doc
看看就行了