Ansible部署
1、覆盖清单位置
/etc/ansible/hosts文件被视为系统的默认静态清单文件。
不过,通常的做法是不使用该文件,而是在Ansible配置文件中为清单文件定义一个不同的位置。
2、构建Ansible清单
修改默认清单文件/etc/ansible/hosts添加以下内容:
192.168.145.136
[webservers]
192.168.145.137
192.168.145.138
使用以下命令列出默认清单文件中的所有受管主机
[root@localhost ~]# ansible all --list-hosts
使用以下命令列出不属于任何组的受管主机
[root@localhost ~]# ansible ungrouped --list-hosts
使用以下命令列出属于某组的受管主机
[root@localhost ~]# ansible webservers --list-hosts
3、配置Ansible的优先级
使用ANSIBLE_CONFIG环境变量 > 使用./ansible.cfg > 使用~/.ansible.cfg > 使用/etc/ansible/ansible.cfg
使用/etc/ansible/ansible.cfg
ansible软件包提供一个基本的配置文件,它位于/etc/ansible/ansible.cfg。如果找不到其他配置文件,则使用此文件。
使用~/.ansible.cfg
Ansible在用户的家目录中查找.ansible.cfg文件。如果存在此配置文件并且当前工作目录中也没有ansible.cfg文件,则使用此配置取代/etc/ansible/ansible.cfg。
使用./ansible.cfg
如果执行ansible命令的目录中存在ansible.cfg文件,则使用它,而不使用全局文件或用户的个人文件。这样,管理员可以创建一种目录结构,将不同的环境或项目存储在单独的目录中,并且每个目录包含为独特的一组设置而定制的配置文件。
使用ANSIBLE_CONFIG环境变量
我们可以通过将不同的配置文件放在不同的目录中,然后从适当的目录执行Ansible命令,以此利用配置文件。但是,随着配置文件数量的增加,这种方法存在局限性并且难以管理。有一个更加灵活的选项,即通过ANSIBLE_CONFIG环境变量定义配置文件的位置。定义了此变量时,Ansible将使用变量所指定的配置文件,而不用上面提到的任何配置文件。
ANSIBLE_CONFIG环境变量指定的任何文件将覆盖所有其他配置文件。如果没有设置该变量,则接下来检查运行ansible命令的目录中是否有ansible.cfg文件。如果不存在该文件,则检查用户的家目录是否有.ansible.cfg文件。只有在找不到其他配置文件时,才使用全局/etc/ansible/ansible.cfg文件。如果/etc/ansible/ansible.cfg配置文件不存在,Ansible包含它使用的默认值。
4、获取Ansible命令帮助
1.查看某个模块的帮助信息
[root@localhost ~]# ansible-doc +模块名
2.列出所有模块
[root@localhost ~]# ansible-doc -l
3.列出指定模块的所有选项
[root@localhost ~]# ansible-doc yum -s
4.查看指定模块的文档
[root@localhost ~]# ansible-doc cron
5、Ansible模块的使用
service模块
首先查看service模块的配置方法
[root@localhost ~]# ansible-doc service
开启httpd服务
[root@localhost ~]# ansible test -m service -a "name=httpd state=started"
设置开机自启
[root@localhost ~]# ansible test -m service -a "name=httpd enabled=yes"
远程下载httpd到server2上
[root@server1~]# ansible test -m yum -a "name=httpd state=present"
删除远程主机的应用软件
[root@server1 ~]# ansible test -m yum -a "name=httpd state=absent"
查看服务部署过程的使用方法(在部署命令后加 -vvv,v的个数越多消息越详细)
[root@server1 ~]# ansible test -m yum -a "name=httpd state=present" - vvv
查看防火墙规则的书写格式
[root@localhost ~]# ansible-doc firewalld
设置防火墙的规则
[root@localhost ~]# ansible-doc firewalld
启动防火墙服务
开启防火墙
[root@localhost ~]# ansible test -m service -a "name=firewalld state=started"
设置开机自启
[root@localhost ~]# ansible test -m service -a "name=firewalld enabled=yes"
查看防火墙的状态
[root@localhost ~]# systemctl is-enabled firewalld
6、常用的模块
ping模块
测试终端和远程主机是否能够连接成功
语法: ansible hosts主机清单 -m 模块名
[root@localhost ~]# ansible webserver -m ping
[root@localhost ~]# ansible all -m ping
all代表所有主机清单
command模块
在远程主机上执行指定得命令 如:cat ls ,不能使用特殊得符号 :| > >>
语法:
ansible 主机清单 -m 模块名 -a '执行命令'
[root@localhost ~]# ansible dbserver -m command -a 'ls /root'
creates:当指定文件存在时,后一条命令不执行 / 指定文件不存在,后一条命令执行
removes:当指定文件存在时,后一条命令执行 / 指定文件不存在,后一条命令不执行
shell模块
在远程主机上执行复杂的命令,比较好用得模块
语法:
ansible 主机清单 -m 模块名 -a '执行命令'
[root@localhost ~]# ansible dbserver -m shell -a 'ls /root | grep .cfg'
[root@localhost ~]# ansible dbserver -m shell -a 'mkdir /root/ansible'
user用户模块
管理用户,创建用户
常见的用户操作:
useradd 用户名 # 创建普通用户
passwd 用户名 # 设置用户密码
useradd -M -s /sbin/nologin 用户名 # 创建的用户没有家目录,不能登录
useradd -u 用户id -g gid 用户名 # 创建用户时指定uid和gid
useradd -u 用户id -g gid 用户名 # 创建用户时指定uid和gid
创建用户
[root@localhost ~]# ansible dbserver -m user -a 'name=zs'
删除用户
remove=yes 删除用户的同时删除家目录
state=absent 删除用户
[root@localhost ~]# ansible dbserver -m user -a 'name=zs state=absent remove=yes'
group组模块
创建组管理组
name:指定组
gid:指定gid
state=absent 删除指定组
创建组并指定gid
[root@localhost ~]# ansible dbserver -m group -a 'name=gourp1 gid=1100'
删除组
[root@localhost ~]# ansible dbserver -m group -a 'name=gourp2 state=absent'
script模块
在远程主机执行主控制端脚本
chdir=/目录 进入到指定目录
creates 文件存在 脚本不执行
removes 文件存在 脚本执行
copy模块
将主控端的文件复制到远程主机,只针对文件
src 源文件路径
dest 目标文件路径
content 将指定内容覆盖写入到目标主机文件中
force=no 当主控端拷贝的文件名和目标名一致,但是内容不一致,放弃拷贝
force=yes 当主控端拷贝的文件名和目标名一致,但是内容不一致,则进行覆盖
backup=yes 当主控端拷贝的文件名和目标名一致,但是内容不一致,则进行备份
权限参数
owner 属主
group 属组
mode 权限
unarchive模块
copy:copy=no为源tar包从被控端解压。 copy=tes 为源tar包从主控端解压
src:源tar包路径
dest:解压到的目标位置
mode:解压后的文件权限
解压软件包
[root@localhost ~]# ansible db -m unarchive -a 'src=/root/tmp.tar.gz dest=/root copy=no'
yum模块
在远程主机上使用安装软件
state:installed 安装软件包
removed 卸载软件包
disable_gpg_check=yes :取消密钥的认证
update_cache=yes 更新缓存,需要在指定安装包时使用
file模块
创建或者和删除远程主机上的文件或者目录
path 指定文件 如果远程主机上没有该文件,则进行创建
state 创建类型 touch 文件 directory 目录
state=absent 删除文件或者目录
link 软连接 src=源文件名 path=目标链接文件名
hard 硬链接 src=源文件名 path=目标链接文件名
以下三个参数,既可以修改,也可以自动添加
mod:权限 可以在添加时设置特殊权限,前提要有执行权限( set 粘滞位)
owner:属主
group:属组
set:4777 属主特殊权限
2777 属组特殊权限
1777 执行特殊权限
7777 全部特殊权限
Cron模块
minute 分 minute=* 每分钟
hour 时 special_time=hourly 每小时
day 天
month 月
weekday 周
job 计划任务的工作
name=* 计算任务名称
disbaled=true 禁用某个计划任务
disabled=false 再次开启某个计划任务
state=absent 删除某个计划任务
mount模块
永久性,临时性挂载某个文件
fstype 指定挂载文件的系统类型,必须指定
path 定义挂载到哪个目录,必须指定
src 定义挂载内容
state 挂载状态
mounted进行挂载,修改/etc/fstab信息
unmounted临时卸载,不修改/etc/fstab信息
absent永久性卸载,并修改 /etc/fstab信息
- name: 永久挂载
mount: src=/dev/sr0 path=/media state=mounted fstype=iso9660
永久性卸载
[root@localhost ansible]# ansible nginx -m mount -a \
> 'fstype=iso9660 src=/dev/sr0 path=/media state=absent'
浙公网安备 33010602011771号