实验环境搭建:
- 主控主机
hostname : ansible.westos.com
ipaddress:172.25.254.100 - 被控主机
- 主机1
hostname : node1.westos.com
ipaddress : 172.25.254.1 - 主机2
hostname : node2.westos.com
ipaddress : 172.25.254.2 - 主机3
hostname : node3.westos.com
ipaddress : 172.5.254.3
- 主机1
ansible实现管理的方式
Ad-Hoc | 利用ansible命令直接完成管理,主要用于临时命令使用场景 |
---|---|
playbook | ansible脚本,主要用于大型项目场景,需要前期的规划 |
Ad-Hoc执行方式中如何获得帮助
ansible-doc 模块 | 显示模块的详细帮助信息 |
---|---|
ansible-doc -s 模块 | 显示模块的简略的详细帮助信息 |
ansible-doc -l | 显示ansible 的所有模块 |
ansible命令运行方式及常用参数
格式:ansible 清单 -m 模块 -a 参数
-
常用参数
-version 显示版本 -m module 指定模块,默认为command模块 -v 详细过程 -vv -vvv更详细过程 –list 显示主机列表,也可以用–list-hosts -k 提示输入ssh连接密码,默认key认证 -C 预执行检测 -T 执行命令的超时时间,默认10s -u 指定远程执行的用户 -b 执行sudo切换身份操作 -become-user=USERNAME 指定sudo的用户 -K 提示输入sudo密码 -
查看清单
ansible-inventory --list 查看清单的详细分布信息 ansible 组名或者all --list 查看具体组的详细信息
ansible的基本颜色代表信
绿色 | 执行成功但为对远程主机没有做任何改变 |
---|---|
黄色 | 执行成功并对远程主机做改变 |
红色 | 执行失败 |
ansible中的常用模块
实验环境搭建:
-
主控主机
-
切换到普通用户westos,配置好普通用户家目录下的
/home/westos.ansible.cfg
的文件。 -
建立清单
vim /home/westos/inventory
-
-
受控主机
- 3台受控主机给自身主机的westos下放root 用户的所有权限,实现免密使用sudo
command
功能:在远程主机执行命令,此模块为默认模块(在不修改配置文件的情况下)
chdir | 执行命令前先进入到指定目录 |
---|---|
creates | 如果文件存在将不运行 |
removes | 如果文件存在在将运行 |
示例:
ssh.sh 脚本内容用于观察效果
-
给ansible_list清单上的主机添加一个lee用户
ansible ansible_list -m command -a 'useradd lee'
-
ansible ansible_list -m command -a 'chdir=/mnt touch file'
-
如果/mnt/file存在,则不建立/mnt/file1 文件
ansible ansible_list -m command -a 'chdir=/mnt creates=file touch file1'
-
如果/mnt/file存在,则建立/mnt/file1 文件
注意 : Linux中的很多通配符在command模块中不支持
shell
chdir | 执行命令前先进入到指定目录 |
---|---|
creates | 如果文件存在将不运行 |
removes | 如果文件存在在将运行 |
executable | 指定执行环境,默认为sh |
示例:
ansible ansible_list -m shell -a 'executable=/bin/bash ps ax | grep $$'
script
功能 : 在ansible主机中写好的脚本在受控主机中执行
示例:ansible ansible_list -m script -a 'echo.sh'
copy
功能:从ansible主控主机复制文件到受控主机
-
常用参数:
src |源文件
|–|--|
dest |目的地文件
owner |指定目的地文件拥有人
group|指定目的地文件拥有组
mode |指定目的地文件权限
backup=yes |当受控主机中存在文件时备份原文件
content |指定文本内容直接在受控主机中生成文件 -
示例:
-
ansible ansible_list -m copy -a 'src=echo.sh dest=/mnt/ owner=westos group=westos backup=yes mode=777'
-
ansible ansible_list -m copy -a 'src=/etc/passwd dest=/mnt/echo.sh owner=westos group=westos backup=yes mode=777'
-
ansible ansible_list -m copy -a "content='hello westos\nhello linux\n' dest=/mnt/westosfile1 owner=westos mode=600"
-
fetch
功能:从受控主机把文件复制到ansible主机,但不支持目录
- 常用参数
rc 受控主机的源文件 est 本机目录 - 示例
ansible ansible_list -m fetch -a 'src=/etc/hostname dest=.'
file
功能:设置文件的属性
- 常用参数
path 指定文件名称 state 指定操作状态 state=touch 建立 state=absent 删除 state=directory 目录 state=link 建立软链接 state=hard 建立硬链接 mode 设定权限 owner 设定文件用户 group 设定文件组 src 源文件 dest 目标文件 recurse=yes 递归 - 常用示例
-
ansible all -m file -a 'path=/mnt/file state=touch owner=westos group=westos mode=666'
-
ansible all -m file -a 'path=/mnt/dir1 state=directory owner=westos group=westos mode=666'
-
ansible all -m file -a 'path=/mnt/dir1/ owner=westos group=westos recurse=yes'
-
archive
功能:压缩
- 常用参数
path 打包目录名称 dest 声称打包文件名称 format 打包格式 owner 指定文件拥有者 mode 指定文件权限 - 示例
ansible all -m archive -a 'path=/mnt/dir1 dest=/mnt/dir1.zip format=zip owner=westos group=westos mode=777'
unarchive
功能 : 解压缩
-
常用参数
copy 默认为yes 从ansible主机复制文件到受控主机 ##设定为no 从受控主机中寻找src源文件 remote_src 功能同copy且相反##设定为yes 表示包在受控主机 ##设定为no表示包在ansible主机 rc 压缩包路径,可以是ansible主机也可以是受控主机 est 受控主机目录 ode 解压后文件权限 -
示例
-
在主控机生成压缩文件
ansible 172.25.254.100 -m archive -a 'path=/mnt dest=/home/westos/mnt.gz format=gz'
-
将主控机的压缩文件解压到受控机
ansible ansible_list -m unarchive -a 'copy=yes src=mnt.gz dest=/mnt/ mode=777'
-
在受控机的压缩文件解压到主控机
压缩:ansible ansible_list -m archive -a 'path=/home/westos dest=/mnt/westos.bz2 format=bz2 '
解压:`ansible ansible_list -m unarchive -a ‘copy=no src=/mnt/westos.bz2 dest=/home/westos mode=777’
-
hostname
功能:管理主机名称
-
常用参数
name 指定修改后的主机名称 -
示例
ansible 172.25.254.1 -m hostname -a 'name=haha.westos.com'
cron
作用:计划任务
-
常用参数
minute 分钟 hour 小时 day 天 month 月 weekday 周 name 任务名称 job 任务脚本或命令 disabled yes 禁用计划任务 no 启动计划任务 state absent 删除计划任务 -
示例:
ansible ansible_list -m cron -a 'job="touch /mnt/file" name=content disabled=no'
- 删除指定定时任务
ansible ansible_list -m cron -a 'job="touch /mnt/file" name=content state=absent'
yum_repository
作用:配置系统软件仓库源文件
- 常用参数
name | 指定仓库名称 |
---|---|
baseurl | 指定源路径 |
description | 指定仓库描述 |
file | 指定仓库文件名称 |
enabled | 仓库是否启用 |
gpgcheck | 仓库是否检测gpgkey |
state | 默认值present 建立 #absent 为删除 |
- 示例
ansible ansible_list -m yum_repository -a 'file=westos name=AppStream description="redhat AppStream" baseurl=http://172.25.254.103/rhel8.2/AppStream gpgcheck=0 enabled=1 state=present'
ansible ansible_list -m yum_repository -a 'file=westos name=BaseOS description="redhat BaseOS" baseurl=http://172.25.254.103/rhel8.2/BaseOS gpgcheck=0 enabled=1 state=present'
添加而不是覆盖
dnf
作用:管理系统中的dnf仓库及管理软件
-
常用参数
ame 指定包 state 指定动作 present 安装latest 更新absent 删除 list 列出指定信息 disable_gpg_check 禁用gpgkey检测 enablerepo 指定安装包来源 disablerepo 禁用安装包来源 autoremove 如果为yes则卸载当初安装这个软件所需要的依赖性软件包 no 的话则不卸载 -
示例
ansible ansible_list -m dnf -a 'name=httpd,dhcp-server state=present'
ansible all -m dnf -a 'name="@Virtual Tools" state=present'
安装组ansible ansible_list -m dnf -a 'name=httpd state=absent autoremove=no '
service
作用:管理系统服务状态
- 常用参数
name 指定服务名称 state 指定对服务的动作 started stoped restarted reloaded enabled 设定服务开机是否启动 yes 开机启动 no 开机不启动 - 示例
ansible all -m service -a "name=httpd state=started enabled=yes"
user
作用:模块可以帮助我们管理远程主机上的用户,比如创建用户、修改用户、删除用户、为用户创建密钥对等操作
-
常用参数
name 必须参数,用于指定要操作的用户名称。 group 指定用户所在的基本组。 gourps 指定用户所在的附加组。 append 指定添加附加组默认值为no shell 指定用户的默认 shell。 uid 指定用户的 uid 号。 comment 指定用户的注释信息。 state 用于指定用户是否存在于远程主机 present 建立 absent 删除 remove 当删除用户是删除用户家目录,默认值为no password 此参数用于指定用户的密码。但密码为明文, 可以用openssl password -6 '密码’生成加密字符 generate_ssh_key 生成sshkey - 示例
ansible all -m user -a 'name=lee'
ansible all -m user -a 'name=lee state=absent'
ansible all -m user -a 'name=lee remove=yes state=absent'
ansible all -m user -a 'name=lee remove=yes group=888'
ansible all -m user -a 'name=lee group=888 groups="user1,user2"'
ansible all -m user -a 'name=lee groups="user3"'
ansible all -m user -a 'name=lee groups="user1,user2" append=yes'
openssl passwd -6 'westos'
ansible all -m user -a 'name=lee password="$6$F4OBwqoXAigDV.dn$I2OgEPB3kfyl8CPmdh3Y8vKDqewZKrVMIDPPIt8GKnhs/DW4gZHfxrZX5ziQN7rVjISX7l14KwDQHEd.uprlV/"'
ansible all -m user -a 'name=lee generate_ssh_key=yes'
group
作用:group 模块可以帮助我们管理远程主机上的组。
-
常用参数
name 用于指定要操作的组名称。 state 用于指定组的状态present 建立absent 删除 gid 用于指定组的gid。 -
示例
ansible all -m group -a 'name=westoslee'
ansible all -m group -a 'name=westoslee state=absent'
ansible all -m group -a 'name=westoslee gid=8888'
lineinfile
作用:对指定文件的内容更改
-
常用参数
path 指定要操作的文件。 line 指定文本内容。 regexp 使用正则表达式匹配对应的行当替换文本时,如果有多行文本都能被匹配,则只有最后面被匹配到的那行文本才会被替换,当删除文本时,如果有多行文本都能被匹配 这么这些行都会被删除。 state 当想要删除对应的文本时需要将state参数的值设置为absent state的默认值为present。 backrefs 向后引用regexp变量信息 insertafter 借助insertafter参数可以将文本插入到“指定的行”之后 insertafter参数的值可以设置为EOF或者正则表达式 insertbefore 借助insertbefore参数可以将文本插入到“指定的行”之前 insertbefore参数的值可以设置为BOF或者正则表达式 backup 是否在修改文件之前对文件进行备份。 create 当要操作的文件并不存在时,是否创建对应的文件。 -
示例
ansible all -m lineinfile -a 'path=/mnt/westos line="hello westos"'
ansible all -m lineinfile -a 'path=/mnt/westos regexp="^westos" line="hello westos" '
ansible all -m lineinfile -a 'path=/mnt/westos regexp="^test" line="westos test"'
ansible all -m lineinfile -a 'path=/mnt/westos regexp='^test' line="westos test new" backrefs=yes'
ansible all -m lineinfile -a 'path=/mnt/westos regexp="(h.{4}).*(w.{5})" line="\1" backrefs=yes'
ansible all -m lineinfile -a 'path=/mnt/westos line="###### westos end #####" insertafter=EOF'
ansible all -m lineinfile -a 'path=/mnt/westos line="###### westos end lee #####" insertafter="hello"'
ansible all -m lineinfile -a 'path=/mnt/westos line="###### westos test #####" insertbefore=BOF'
ansible all -m lineinfile -a 'path=/mnt/westos line="###### westos test lee #####" insertbefore="hello"'
replace
作用:replace 模块可以根据我们指定的正则表达式替换文件中的字符串,文件中所有被匹配到>的字符串都会被替换
-
常用参数
path 指定要操作的文件 regexp 指定一个正则表达式 文件中与正则匹配的字符串将会被替换。 replace 指定最终要替换成的字符串。 backup 是否在修改文件之前对文件进行备份,最好设置为yes -
示例
ansible all -m replace -a 'path=/mnt/westos regexp="WESTOS" replace="westos_lee" backup=yes'
setup
作用:setup模块用于收集远程主机的一些基本信息
- 常用参数
filter 用于条件过滤,如果符合则输出 - 示例
ansible all -m setup -a "filter='ansible_all_ipv4_addresses'"
debug
作用:调试模块,用于在调试中输出信息
- 常用参数
msg 调试输出信息 var 将某个任务执行的输出作为变量传递给debug模块