官方文档传送门:
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/index.html#plugins-in-ansible-builtin
ping模块 (检查主机连通性)
[root@master ansibleProject]# ansible -i ./inventory -m ping all
shell模块( 批量执行shell命令)
ansible 指定主机清单 主机组 指定模块 指定动作[参数]
ansible -i inventory node -m shell -a "hostname"
[root@master ansibleProject]# ansible -i inventory node -m shell -a "hostname"
192.168.253.134 | CHANGED | rc=0 >>
node134
192.168.253.133 | CHANGED | rc=0 >>
node133
192.168.253.130 | CHANGED | rc=0 >>
node130
command模块
command是ansible默认的模块(不指定模块默认就使用command模块),command 适用于执行简单的命令(不支持特殊符号)
[root@master ansibleProject]# ansible -i inventory node -m command -a "whoami"
scrpit模块(执行脚本)
首先自定义一个脚本,如下
[root@master ansibleProject]# cat ctping.sh
#!/bin/bash
yum install -y tcping
[root@master ansibleProject]# ansible -i inventory node -m script -a "./script/tcping.sh"
file模块(文件目录删除创建)
官方文档:ansible.builtin.file module – Manage files and file properties — Ansible Documentation
state状态(模式) :state=directory 创建目录,state=touch 创建文件,state=link创建软连接等等...
案例1
创建目录 /server/scripts/python
[root@master ansibleProject]# ansible -i inventory all -m file -a "state=directory path=/server/scripts/python"
案例2
创建文件 /server/scripts/python/test.py
[root@master ansibleProject]# ansible -i inventory all -m file -a "state=touch path=/server/scripts/python/test.py"
案例3 创建软链接
/server/scripts/python/ ---> /python
[root@master ansibleProject]# ansible -i inventory all -m file -a "state=link src=/server/scripts/python path=/python"
copy模块(远程复制,把ansible的文件传输到节点)
案例1 分发单个文件,把yum.sh 分发到对方/tmp目录下
[root@master ansibleProject]# ansible -i inventory all -m copy -a "src=./script/yum.sh dest=/tmp/yum.sh"
案例2 分发目录,目录后面加/跟不加的区别:#注意:ansible中对于路径 oldboy oldboy/区别 oldboy 目录+目录内容 oldboy/目录的内容
[root@master ansibleProject]# ansible -i inventory all -m copy -a "src=./script dest=/tmp/"
案例3 传输过程中修改权限 644 所有者为nginx
[root@master ansibleProject]# ansible -i inventory all -m copy -a "src=./script/yum.sh dest=/tmp/yum.sh mode=644 owner=nginx group=nginx"
案例4 批量分发hosts文件,如果文件要发生变动则先备份在修改
[root@master ansibleProject]# ansible -i inventory all -m copy -a "src=/etc/hosts dest=/etc/hosts backup=yes"
systemd(服务管理)
name服务名称
enable为是否自启动
state:(开启 started,停止stopped,重启,restarted,重载配置reloaded)
案例1 服务的开机自启动
[root@master ansibleProject]# ansible -i inventory all -m systemd -a "name=crond enabled=yes"
案例2 服务的开启与关闭
[root@master ansibleProject]# ansible -i inventory all -m systemd -a "name=crond state=stopped"
yum_repository(yum源管理)
name:yum源中模块的名字如上面的[epel]
comdescription:yum源的注释
baseurl:yum源中的下载地址
enabled(yes/no)是否开启这个yum源
gpgcheck(yes/no)是否开启gpgcheck
file:指定yum源文件自动添加.repo默认跟name一样
repo文件常用格式:
[epel] #模块名字
name=Extra Packages for Enterprise Linux 7 - $basearch #注释
baseurl=http://mirrors.aliyun.com/epel/7/$basearch #下载地址
enabled=1 #是否开启这个源
bpgcheck=0 #是否开启gpgcheck功能
bpgkey=file:///etc/pki/rpm-gpe/RPM-GPG-KEY-EPEL-7 #如果开启gpgcheck指定的路径和文件
案例1 添加nginx源
[root@master ansibleProject]# ansible -i inventory all -m yum_repository -a 'name="nginx_yum_repo" description="nginx_yum_repo" baseurl="https://nginx.org/packages/centos/$releasever/$basearch/" enabled=yes gpgcheck=no file=nginx '
yum(软件包管理)
name:指定软件包名字
state:installed安装,remnoved删除,lastest安装或更新
案例1 安装vim
[root@master ansibleProject]# ansible -i inventory node130 -m yum -a "name=vim state=installed"
案例2 通过rpm包安装,
disable_gpg_check=yes为关闭校验
[root@master ansibleProject]# ansible -i inventory all -m yum -a 'name=/root/zabbix-agent-5.2.0-0.1alpha1.el8.x86_64.rpm state=installed disable_gpg_check=yes'
get_url(下载网络文件到指定目录)
url:下载地址
dest:下载的目录
案例1 下载软件包到所有机子上
[root@master ansibleProject]# ansible -i inventory all -m get_url -a 'url="https://mirrors.aliyun.com/zabbix/zabbix/5.1/rhel/8/x86_64/zabbix-agent-5.2.0-0.1alpha1.el8.x86_64.rpm" dest=/root '
发表评论