天天看点

Ansible常用模块Ad-Hoc用法

1、配置主机清单
[root@rocky8 ~]#cd /data/ansible/
[root@rocky8 ~]#ansible-config init --disabled > ansible.cfg   #生成初始化配置文件
[root@rocky8 ansible]#vim ansible.cfg       #编辑配置文件
[defaults]
inventory = /data/ansible/hosts             #设置主机清单文件位置
[root@rocky8 ansible]#vim /data/ansible/hosts     #设置主机清单
[rocky]
10.0.0.28 ansible_connection=ssh ansible_ssh_port=6666 ansible_ssh_user=root ansible_ssh_password=redhat        #指定用户和密码
www.wang.org              #通用域名连接,需要在/etc/hosts解析

[ubuntu]
10.0.0.101
10.0.0.102

[centos]
10.0.0.7

[local]
10.0.0.18 ansible_connection=local    ##指定本地连接,无需ssh配置
[root@rocky8 ansible]#ansible --version          
ansible [core 2.12.2]
  config file = /data/ansible/ansible.cfg        #可以看到配置生效文件      

1-1、Ansible配置文件优先级:

ANSIBLE_CONFIG #环境变量,注意:指定目录下的ansible.cfg文件必须存在才能生效
./ansible.cfg #当前目录下的ansible.cfg,一般一个项目对应一个专用配置文件,推荐使用
~/.ansible.cfg #当前用户家目录下的.ansible.cfg
/etc/ansible/ansible.cfg #系统默认配置文件      

1-2、配置文件说明

[defaults]
#inventory = /etc/ansible/hosts #主机列表配置文件
#library = /usr/share/my_modules/ #库文件存放目录
#remote_tmp = $HOME/.ansible/tmp #临时py命令文件存放在远程主机目录
#local_tmp = $HOME/.ansible/tmp #本机的临时命令执行目录
#forks = 5 #默认并发数
#sudo_user = root #默认sudo 用户
#ask_sudo_pass = True #每次执行ansible命令是否询问ssh密码
#ask_pass = True
#remote_port = 22
#host_key_checking = False #检查对应服务器的host_key,建议取消此行注释,实现第一次连接自动信任目标主机
#log_path=/var/log/ansible.log #日志文件,建议启用
#module_name = command #默认模块,可以修改为shell模块
[privilege_escalation] #普通用户提权配置
#become=True
#become_method=sudo
#become_user=root
#become_ask_pass=False      
[root@rocky8 ansible]#export ANSIBLE_CONFIG=/opt     #通过环境变量ANSIBLE_CONFIG指定ansible配置文件路径
[root@rocky8 ansible]#cp ansible.cfg /opt/
[root@rocky8 ansible]#ansible --version
ansible [core 2.12.2]
  config file = /opt/ansible.cfg         #可以看到配置生效文件,ansible.cfg文件必须存在才能生效      

1-3、主机清单说明

ansible_ssh_host #将要连接的远程主机名.与你想要设定的主机的别名不同的话,可通过此变量设置.
ansible_ssh_port #ssh端口号.如果不是默认的端口号,通过此变量设置.这种可以使用 ip:端口
192.168.1.100:2222
ansible_ssh_user #默认的 ssh 用户名
ansible_ssh_pass #ssh 密码(这种方式并不安全,我们强烈建议使用 --ask-pass 或 SSH 密钥)
ansible_sudo_pass #sudo 密码(这种方式并不安全,我们强烈建议使用 --ask-sudo-pass)
ansible_sudo_exe (new in version 1.8) #sudo 命令路径(适用于1.8及以上版本)
ansible_connection #与主机的连接类型.比如:local, ssh 或者 paramiko. Ansible 1.2 以前默认使用 paramiko.1.2 以后默认使用 'smart','smart' 方式会根据是否支持 ControlPersist, 来判断'ssh' 方式是否可行.
ansible_ssh_private_key_file #ssh 使用的私钥文件.适用于有多个密钥,而你不想使用 SSH 代理的情况.
ansible_shell_type #目标系统的shell类型.默认情况下,命令的执行使用 'sh' 语法,可设置为'csh' 或 'fish'.
ansible_python_interpreter #目标主机的 python 路径.适用于的情况: 系统中有多个 Python,或者命令路径不是"/usr/bin/python",比如 \*BSD, 或者 /usr/bin/python 不是 2.X 版本的
Python.之所以不使用 "/usr/bin/env" 机制,因为这要求远程用户的路径设置正确,且要求 "python"可执行程序名不可为 python以外的名字(实际有可能名为python26).与
ansible_python_interpreter 的工作方式相同,可设定如 ruby 或 perl 的路径....      
2、查看帮助
[root@rocky8 ansible]#ansible-doc -l   #列出所有模块
[root@rocky8 ansible]#ansible-doc ping           #查看指定模块帮助
[root@rocky8 ansible]#ansible-doc -s ping        #查看指定模块简要帮助
- name: Try to connect to host, verify a usable python and return >
  ping:
      data:                  # Data to return for the `ping' retur>
                             # this parameter is
                             # set to `crash', the
                             # module will cause
                             # an exception.      

2-1、查看指定的插件

[root@rocky8 ansible]#ansible-doc -t lookup -l
[root@rocky8 ansible]#ansible-doc -t connection -l      
3、Ad-Hoc用法
ansible <host-pattern> [-m module_name] [-a args]
--version #显示版本
-m module #指定模块,默认为command
-v #详细过程 -vv -vvv更详细
--list-hosts #显示主机列表,可简写 --list
-C, --check #检查,并不执行
-T, --timeout=TIMEOUT #执行命令的超时时间,默认10s
-k, --ask-pass #提示输入ssh连接密码,默认Key验证
-u, --user=REMOTE_USER #执行远程执行的用户,默认root
-b, --become #代替旧版的sudo实现通过sudo机制实现提升权限
--become-user=USERNAME #指定sudo的runas用户,默认为root
-K, --ask-become-pass #提示输入sudo时的口令
-f FORKS, --forks FORKS #指定并发同时执行ansible任务的主机数
-i INVENTORY, --inventory INVENTORY #指定主机清单文件      
[root@rocky8 ansible]#ansible all -a 'sleep 5' -f10    #并发10执行
[root@rocky8 ansible]#ansible "*" -m ping       #使用*通配符
[root@rocky8 ansible]#ansible 10.0.0.* -m ping    
[root@rocky8 ansible]#ansible rocky -m ping     #ping rocky组中的主机

# 或关系
[root@rocky8 ansible]#ansible 'rocky:centos' -m ping   #ping 在rocky组中的主机或在centos组中的主机
# 逻辑与
[root@rocky8 ansible]#ansible 'rocky:&ubuntu' -m ping  #ping 在rocky组中的主机并且在ubuntu组中的主机
# 逻辑非
[root@rocky8 ansible]#ansible 'all:!centos:!rocky' -m ping  #ping 所有主机,但不在centos组中和ubuntu组中的主机
# 综合逻辑
[root@rocky8 ansible]#ansible 'rocky:ubuntu:¢os:!local' -m ping      

3-1、command

功能:在远程主机执行命令,此为默认模块,可忽略 -m 选项

注意:此命令不支持 $VARNAME < > | ; & 等,可能用shell模块实现

注意:此模块不具有幂等性

常见选项

chdir=dir #执行命令前,先切换至目录dir
creates=file #当file不存在时才会执行
removes=file #当file存在时才会执行      
[root@rocky8 ansible]#ansible rocky -m command -a 'chdir=/etc cat os-release'      
[root@rocky8 ansible]#ansible rocky -m command -a 'chdir=/etc creates=/data/f1.txt cat issue'    #当/data/f1.txt不存在时才会执行 切换目录 查看issue
[root@rocky8 ansible]#ansible rocky -m command -a 'chdir=/etc removes=/data/f1.txt cat issue '   #当/data/f1.txt存在时才会执行 切换目录 查看issue      

3-2、shell

功能:和command相似,用shell执行命令,支持各种符号,比如:*,$, > , 相当于增强版的command模块

注意:此模块不具有幂等性,建议能不能就用此模块,最好使用专用模块

常见选项

chdir=dir #执行命令前,先切换至目录dir
creates=file #当file不存在时才会执行
removes=file #当file存在时才会      
[root@rocky8 ansible]#ansible rocky -m shell -a 'echo $HOSTNAME'
[root@rocky8 ansible]#ansible rocky -m shell -a 'echo redhat | passwd --stdin wang'      
[root@rocky8 ansible]#vim ansible.cfg
[defaults]
inventory = /data/ansible/hosts
module_name = shell              #修改shell为默认模块      

3-3、script

功能:在远程主机上运行ansible服务器上的脚本(无需执行权限)

注意:此模块不具有幂等性

常见选项

chdir=dir #执行命令前,先切换至目录dir
cmd #指定ansible主机的命令
creates=file #当file不存在时才会执行
removes=file #当file存在时才会执行      
[root@rocky8 ansible]#ansible rocky -m script -a 'test.sh'      

3-4、copy

功能:复制ansible服务器主控端或远程的本机的文件到远程主机

注意: src=file 如果是没指明路径,则为当前目录或当前目录下的files目录下的file文件

常见选项

src #控制端的源文件路径
dest #被控端的文件路径
owner #属主
group #属组
mode #权限
backup #是否备份
validate #验证成功才会执行copy
remote_src #no是默认值,表示src文件在ansible主机,yes表示src文件在远程主机      
[root@rocky8 ansible]#ansible rocky -m copy -a 'src=/root/reset.sh dest=/opt/ owner=wang group=wang mode=777'
[root@rocky8 ansible]#ansible rocky -m copy  -a 'content=wang\nli\nzhang\n dest=/opt/test.log owner=wang group=wang'
      

3-5、get_url

功能: 用于将文件从http、https或ftp下载到被管理机节点上

常用参数如下

url #下载文件的URL,支持HTTP,HTTPS或FTP协议
dest #下载到目标路径(绝对路径),如果目标是一个目录,就用原文件名,如果目标设置了名称就用目标设置的名称
owner #指定属主
group #指定属组
mode #指定权限
force #如果yes,dest不是目录,将每次下载文件,如果内容改变替换文件。如果no,则只有在目标不在时才会下载
checksum #对目标文件在下载后计算摘要,以确保其完整性
  #示例: checksum="sha256:D98291AC[...]B6DC7B97",
      checksum="sha256:http://example.com/path/sha256sum.txt"
url_username #用于HTTP基本认证的用户名。 对于允许空密码的站点,此参数可以不使用`url_password'
url_password #用于HTTP基本认证的密码。 如果未指定`url_username'参数,则不会使用`url_password'参数
validate_certs #如果“no”,SSL证书将不会被验证。 适用于自签名证书在私有网站上使用timeout #URL请求的超时时间,秒为单位      
[root@rocky8 ansible]#ansible rocky -m get_url -a 'url=http://nginx.org/download/nginx-1.22.0.tar.gz dest=/opt/ checksum="md5:9b1fb7dae677b22ce4b4e271a4de3501"'      

3-6、fetch

功能:从远程主机提取文件至ansible的主控端,copy相反,目前不支持目录

常见选项

src #被控制端的源文件路径,只支持文件
dest #ansible控制端的目录路径      
[root@rocky8 ansible]#ansible rocky -m fetch -a 'src=/root/reset.sh dest=/opt'      

3-7、file

功能:设置文件属性,创建文件,目录和软链接等

常见选项

path #在被控端创建的路径
owner #属主
group #属组
mode #权限
state #状态
=touch #创建文件
=directory #创建目录
=link #软链接
=hard #硬链接
recurse #yes表示递归授权      
[root@rocky8 ansible]#ansible all -m file -a 'path=/opt/text.txt state=touch'
[root@rocky8 ansible]#ansible all -m file -a 'path=/opt/text.txt state=absent'
[root@rocky8 ansible]#ansible all -m file -a 'path=/opt/test state=directory'
[root@rocky8 ansible]#ansible all -m file -a 'path=/opt/test state=absent'
[root@rocky8 ansible]#ansible all -m file -a 'src=/etc path=/opt/etclink state=link'
[root@rocky8 ansible]#ansible all -m file -a 'path=/opt/etclink state=absent'
[root@rocky8 ansible]#ansible all -m file -a 'path=/opt/test owner=wang'   #修改目录本身的所有者为wang
[root@rocky8 ansible]#ansible all -m file -a 'path=/opt/test owner=wang recurse=yes' #递归修改目录的所有者为wang      

3-8、stat

功能:检查文件或文件系统的状态

注意:对于Windows目标,请改用win_stat模块

常见选项

path #文件/对象的完整路径(必须)      

常用的返回值判断

exists: 判断是否存在
isuid: 调用用户的ID与所有者ID是否匹配      
[root@rocky8 ansible]#ansible 127.0.0.1 -m stat -a 'path=/etc/passwd'      

3-9、unarchive

功能:解包解压缩

实现有两种用法:

将ansible主机上的压缩包传到远程主机后解压缩至特定目录,设置remote_src=no,此为默认值,可省略

将远程本主机上或非ansible的其它主机的某个压缩包解压缩到远程主机本机的指定路径下,需要设置remote_src=yes

常见参数:

remote_src #和copy功能一样且选项互斥,yes表示源文件在远程被控主机或其它非ansible的其它主机上,no表示文件在ansible主机上,默认值为no, 此选项代替copy选项
copy #默认为yes,当copy=yes,拷贝的文件是从ansible主机复制到远程主机上,如果设置为copy=no,会在远程主机上寻找src源文件,此选项已废弃
src #源路径,可以是ansible主机上的路径,也可以是远程主机(被管理端或者第三方主机)上的路径,如果是远程主机上的路径,则需要设置remote_src=yes
dest #远程主机上的目标路径
owner #默认递归
group #默认递归
mode #设置解压缩后的文件权限,默认递归
creates=/path/file #当绝对路径/path/file不存在时才会执行      
[root@rocky8 ansible]#ansible rocky -m unarchive -a 'src=https://releases.ansible.com/ansible/ansible-2.9.7.tar.gz dest=/opt remote_src=yes'        #远程主机下载并解压到远程主机的opt下
[root@rocky8 ansible]#ansible all -m unarchive -a 'src=/opt/nginx-1.22.0.tar.gz dest=/opt/ owner=wang copy=no'   #解压远程主机的nginx到远程主机的opt下      

3-10、archive

功能:打包压缩保存在被管理节点

常见选项

path #压缩的文件或目录
dest #压缩后的文件
format #压缩格式,支持gz,bz2,xz,tar,zip      
[root@rocky8 ansible]#ansible rocky -m archive -a 'path=/opt/ansible-2.9.7 dest=/opt/ansible.gz format=gz owner=wang'    #压缩远程主机的ansible-2-9-7目录为ansible.gz 属主是wang      

3-11、hostname

功能:管理主机名

常见选项

name #修改后的主机名称      
[root@rocky8 ansible]#ansible www.wang.org -m hostname -a 'name=node38'      

3-12、cron

功能:计划任务

支持时间:minute,hour,day,month,weekda

常见选项

name #描述脚本的作用
minute #分钟
hour #小时
weekday #周
user #任务由哪个用户运行;默认root
job #任务      
[root@rocky8 ansible]#ansible rocky -m cron -a 'hour=2 minute=20 weekday=1-5 name=test job="logger testlog" user=wang'
[root@rocky8 ansible]#ansible rocky -m cron -a 'hour=2 minute=20 weekday=1-5 name=test job="logger testlog" user=wang disabled=yes'
[root@rocky8 ansible]#ansible rocky -m cron -a 'hour=2 minute=20 weekday=1-5 name=test job="logger testlog" user=wang disabled=no'
[root@rocky8 ansible]#ansible rocky -m cron -a 'state=absent name=test user=wang'      

3-13、yum和apt

功能:管理软件包

yum 管理软件包,只支持RHEL,CentOS,fedora,不支持Ubuntu其它版本

apt 模块管理 Debian 相关版本的软件包

yum常见选项

name #软件包名称
state #状态
  =present #安装,此为默认值
  =absent #删除
  =latest #最新版
list #列出指定包
enablerepo #启用哪个仓库安装
disablerepo #不使用哪些仓库的包
exclude #排除指定的包
validate #是否检验,默认为yes      
[root@rocky8 ansible]#ansible rocky -m yum -a 'name=httpd state=present'
[root@rocky8 ansible]#ansible rocky -m yum -a 'name=https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/5.0/rhel/8/x86_64/zabbix-agent2-5.0.24-1.el8.x86_64.rpm state=present disable_gpg_check=yes'    #安装zabbix agent rpm包
[root@rocky8 ansible]#ansible rocky -m yum -a 'name=nginx state=present enablerepo=epel      #启用epel源进行安装
[root@rocky8 ansible]#ansible rocky -m yum -a 'name=nginx state=absent'   #卸载nginx

[root@rocky8 ansible]#ansible rocky -m yum -a 'name=* state=latest exclude=kernel*,foo*'      #升级除kernel和foo开头以外的所有包
[root@rocky8 ansible]#ansible ubuntu -m apt -a 'name=sl'
[root@rocky8 ansible]#ansible localhost -m yum -a 'list=tree'   #查看tree包      

3-14、yum_repository

功能: 此模块实现yum的仓库配置管理

常见选项

name #仓库id
description #仓库描述名称,对应配置文件中的name=
baseurl #仓库的地址
gpgcheck #验证开启
gpgkey #仓库公钥路径
state      
[root@rocky8 ansible]#ansible rocky -m yum_repository -a 'name=zibbix description="zibbipos" file=zibbixfile baseurl=https://mirrors.aliyun.com/zabbix/zabbix/6.0/rhel/$releasever/$basearch gpgcheck=no'      

3-15、service

此模块和sytemd功能相似,选项很多相同

功能:管理服务

常见选项

name #服务名称
state #服务状态
=started #启动
=stopped #停止
=restarted #重启
=reloaded #重载
enabled #开启自启动
daemon_reload #加载新的配置文件,适用于systemd模块      
[root@rocky8 ansible]#ansible rocky -m service -a 'name=httpd state=started enabled=yes'
[root@rocky8 ansible]#ansible rocky -m service -a 'name=httpd state=stopped'
[root@rocky8 ansible]#ansible rocky -m service -a 'name=httpd state=reloaded'
[root@rocky8 ansible]#ansible rocky -m service -a 'name=httpd state=stopped enabled=no'      

3-16、user

功能:管理用户

常见选项

name #创建的名称
uid #指定uid
group #指定基本组
shell #登录shell类型默认/bin/bash
create_home #是否创建家目录,默认会创建家目录,no不创建
password #设定对应的密码,必须是加密后的字符串才行,否则不生效
system #yes表示系统用户
groups #附加组
append #追加附加组使用,yes表示增加新的附加组
state #absen删除
remove #yes表示删除用户时将家目录一起删除
generate_ssh_key #创建私钥
ssh_keyu_bits #私钥位数
ssh_key_file #私钥文件路径      
[root@rocky8 ansible]#ansible rocky -m user -a 'name=user1 comment=test uid=2048 home=/data/user1 group=root'
[root@rocky8 ansible]#ansible all -m user -a 'name=nn comment=test uid=1088 group=1088 groups="root,daemon" shell=/sbin/nologin system=yes create_home=no home=/data/nn non_unique=yes'
[root@rocky8 ansible]#ansible all -m user -a 'name=nn state=absent remove=yes'   #remove=yes表示删除用户及家目录等数据,默认remove=no

[root@rocky8 ansible]#ansible localhost -m debug -a "msg={{ 'redhat' | password_hash('sha512') }}"       #生成redhat加密的密码
[root@rocky8 ansible]#ansible localhost -m debug -a "msg={{ 'redhat' | password_hash('sha512','salt') }}"      #加盐生成redhat加密的密码
[root@rocky8 ansible]#ansible rocky -m user -a 'name=nnn system=yes shell=/sbin/nologin password="6$salt$TXGPxOwUfTSW0I8V9dD9QXd4BCkZrvltTEmMoayMhN4MePeBDuYpasD6QdwDwKRi5xyDzI5ihtXMaGufO5jrW/"'   #用上面创建的密码创建用户

[root@rocky8 ansible]#ansible rocky -m user -a 'name=test generate_ssh_key=yes ssh_key_bits=4096 ssh_key_file=.ssh/id_rsa'       #创建用户test,并生成4096bit的私钥      

3-17、group

功能:管理组

常见选项

name #指定组名称
gid #指定gid
state
  =present #创建,默认
  =absent #删除
system #是否是系统组      
[root@rocky8 ansible]#ansible all -m group -a 'name=nn gid=1088'
[root@rocky8 ansible]#ansible all -m group -a 'name=nn state=absent'      

3-18、lineinfile

功能:相当于sed,主要用于修改一行的文件内容

常见选项

path #被控端文件的路径
regexp #正则匹配语法格式,表示被替换的内容
line #替换为的内容
state #absent表示删除
insertafter #插入到替换内容前面,如和regexp同时存在,只在没找到与regexp匹配时才使用
insertafter
insertbefore #插入到替换内容后面,如和regexp同时存在,只在没找到与regexp匹配时才使用
insertafter
backrefs #支持后面引用,yes和no
backup #修改前先备份
create #如果文件不存在,则创建,默认不存在会出错
mode #指定权限
owner #指定用户
group #指定组
#注意
regexp参数 :使用正则表达式匹配对应的行,当替换文本时,如果有多行文本都能被匹配,则只有最后面被匹配到的那行文本才会被替换,当删除文本时,如果有多行文本都能被匹配,这么这些行都会被删除。      

注意: 如果想进行多行匹配进行替换需要使用replace模块

[root@rocky8 ansible]#ansible rocky -m lineinfile -a "path=/etc/httpd/conf/httpd.conf regexp='^Listen' line='Listen 8080'"        #修改监听端口
[root@rocky8 ansible]#ansible rocky -m lineinfile -a "path=/etc/selinux/config regexp='^SELINUX=' line='SELINUX=enforcing'"       #修改SELinux 
[root@rocky8 ansible]#ansible rocky -m lineinfile -a 'path=/etc/selinux/config regexp="^SELINUX=" line="SELINUX=disabled"'
[root@rocky8 ansible]#ansible rocky -m lineinfile -a 'path=/etc/sysconfig/network-scripts/ifcfg-eth0 line="DNS4=223.5.5.5"'
[root@rocky8 ansible]#ansible rocky -m lineinfile -a 'path=/etc/sysconfig/network-scripts/ifcfg-eth0 insertafter="^DNS3" line="GATEWAY=10.0.0.1"'    ##给主机增加一个网关,但需要增加到DNS3行下面
[root@rocky8 ansible]#ansible rocky -m lineinfile -a 'path=/etc/sysconfig/network-scripts/ifcfg-eth0 regexp="GATEWAY=10.0.0.1" state=absent'      

3-19、replace

该模块有点类似于sed命令,主要也是基于正则进行匹配和替换,建议使用

功能: 多行修改替换

常见选项

path #被控端文件的路径
regexp #正则匹配语法格式,表示被替换的内容
replace #替换为的内容
after #插入到替换内容前面,
before #插入到替换内容后面
backup #修改前先备份
mode #指定权限
owner #指定用户
group #指定组      
[root@rocky8 ansible]#ansible rocky -m replace -a 'path=/etc/fstab regexp="^(UUID.*)" replace="#\1"'
[root@rocky8 ansible]#ansible rocky -m replace -a 'path=/etc/fstab regexp="^#(UUID.*)" replace="\1"'      

3-20、selinux

功能: 该模块管理 SELInux 策略

常见选项

policy #指定SELINUXTYPE=targeted
state #指定SELINUX=disabled      
[root@rocky8 ansible]#ansible www.wang.org -m selinux -a 'policy=targeted state=permissive'     #需要重启方能生效
[root@rocky8 ansible]#ansible www.wang.org -m selinux -a 'policy=targeted state=disabled'       #需要重启方能生效      

3-21、reboot

功能: 重启

常见选项

msg #重启提示
pre_reboot_delay #重启前延迟时间的秒数
post_reboot_delay #重启后延迟时间的秒数后,再验证系统正常启动
reboot_timeout #重启后延迟时间再执行测试成功与否的命令
test_command #执行测试成功与否的命令      
[root@rocky8 ansible]#ansible rocky -m reboot -a 'msg="this host will be reboot"'      

3-22、mount

功能: 挂载和卸载文件系统

常见选项

src #源设备路径,或网络地址
path #挂载至本地哪个路径下
fstype #设备类型; nfs
opts #挂载的选项
state #挂载还是卸载
  =present #永久挂载,但没有立即生效
  =absent #卸载临时挂载,并删除永久挂载
  =mounted #永久和临时挂载
  =unmounted #临时卸载      
[root@rocky8 ansible]#ansible www.wang.org -m mount -a 'src="/dev/vdb2" path=/mnt/vdo fstype=ext4 state=present'
[root@rocky8 ansible]#ansible www.wang.org -m mount -a 'path=/mnt/vdo fstype=ext4  state=absent'      

3-23、setup

功能: setup 模块来收集主机的系统信息,这些 facts 信息可以直接以变量的形式使用,但是如果主机较多,会影响执行速度

可以使用 gather_facts: no 来禁止 Ansible 收集 facts 信息

常见选项

filter #指定过滤条件      
[root@rocky8 ansible]#ansible rocky -m setup -a 'filter=ansible_nodename'
[root@rocky8 ansible]#ansible rocky -m setup -a 'filter=ansible_hostname'
[root@rocky8 ansible]#ansible rocky -m setup -a 'filter=ansible_domain'
[root@rocky8 ansible]#ansible rocky -m setup -a 'filter=ansible_memtotal_mb'
[root@rocky8 ansible]#ansible rocky -m setup -a 'filter=ansible_memory_mb'
[root@rocky8 ansible]#ansible rocky -m setup -a 'filter=ansible_memfree_mb'
[root@rocky8 ansible]#ansible rocky -m setup -a 'filter=ansible_os_family'
[root@rocky8 ansible]#ansible rocky -m setup -a 'filter=ansible_distribution'
[root@rocky8 ansible]#ansible rocky -m setup -a 'filter=ansible_distribution_major_version'
[root@rocky8 ansible]#ansible rocky -m setup -a 'filter=ansible_distribution_version'
[root@rocky8 ansible]#ansible rocky -m setup -a 'filter=ansible_processor_vcpus'
[root@rocky8 ansible]#ansible rocky -m setup -a 'filter=ansible_all_ipv4_address'
[root@rocky8 ansible]#ansible rocky -m setup -a 'filter=ansible_architecture'
[root@rocky8 ansible]#ansible rocky -m setup -a 'filter=ansible_uptime_seconds'
[root@rocky8 ansible]#ansible rocky -m setup -a 'filter=ansible_processor*'
[root@rocky8 ansible]#ansible rocky -m setup -a 'filter=ansible_env'      
[root@rocky8 ansible]#ansible all -m setup -a 'filter=ansible_python_version'
[root@rocky8 ansible]#ansible 10.0.0.101 -m setup -a 'filter=ansible_all_ipv4_addresses'
[root@rocky8 ansible]#ansible 10.0.0.101 -m setup -a 'filter=ansible_all_ipv4_addresses'      

3-24 debug

功能: 此模块可以用于输出信息,并且通过 msg 定制输出的信息内容,功能类似于echo命令

注意: msg后面的变量有时需要加 " " 引起来

常见选项

msg #指定命令输出的信息
var #指定变量名,和msg互斥
verbosity #详细度      
[root@rocky8 ansible]#ansible rocky -m debug     #debug 模块默认输出Hello world      

3-25、sysctl

功能: 修改内核参数

常见选项

name #内核参数
value #指定值
state #是否保存在sysctl.conf文件中,默认present
sysctl_set #使用sysctl -w 验证值生效      
[root@rocky8 ansible]#ansible rocky -m sysctl -a 'name=net.ipv4.ip_forward value=1 state=present'      

3-26、pam_limits

- name: Change Limit /etc/security/limit.conf
  pam_limits:
  domain: "*"
    limit_type: "{{ item.limit_type }}"
    limit_item: "{{ item.limit_item }}"
    value: "{{ item.value }}"
  loop:
    - { limit_type: 'soft', limit_item: 'nofile',value: '100000' }
    - { limit_type: 'hard', limit_item: 'nofile',value: '10000' }      

3-27、apt_repository

repo #仓库信息
state #添加或删除
update_cache #是否apt update,默认yes
filename #仓库文件,默认放在/etc/apt/sources.list.d/file.list      

3-28、apt_key 模块

url #key路径
state #添加或删除      

3-29、其它模块

nginx_status_info
nginx_status_facts
mysql_db #需要安装MySQL-python包
mysql_user #需要安装MySQL-python包
redis
mongodb
postgresql
haproxy
git      

继续阅读