Ansible模块实战,操作技巧
使用临时命令通过模块执行任务
安装软件包集合(Collections)
[root@ansible ~]# cd /var/www/html
[root@ansible html]# ls
ansible-automation-platform materials project4 rhel9 roles
[root@ansible html]# ls
ansible-automation-platform materials project4 rhel9 roles
[root@ansible html]# cd materials/
[root@ansible materials]# ls
ansible-posix-1.5.1.tar.gz hardware.empty
community-general-6.3.0.tar.gz name_list.yml
community-proxysql-1.5.1.tar.gz newhosts.j2
community-rabbitmq-1.2.3.tar.gz topsec.yml
community-zabbix-1.9.1.tar.gz
[student@master ansible]$ ansible-galaxy collection install http://ansible.example.com/materials/ansible-posix-1.5.1.tar.gz -p collections/
[student@master ansible]$ ansible-galaxy collection install http://ansible.example.com/materials/community-general-6.3.0.tar.gz -p collections/
查看模块帮助
-
查看所有已安装模块:
ansible-doc -l
-
查看特定模块(如
ping
)的帮助:ansible-doc ping
常用模块分类说明
1. 文件操作模块
- copy:复制本地文件到远程主机
- file:设置文件权限、属性、创建/删除文件或目录
- template:复制配置文件(支持模板变量)
- fetch:从远程主机拉取文件到本地
2. 软件包管理模块
- package:使用操作系统本机的自动检测软件包管理器管理软件包
- yum:YUM 软件包管理器管理软件包
- apt:APT 软件包管理器管理软件包
- dnf:DNF 软件包管理器管理软件包
- gem:Ruby gem 管理
- pip:从PyPI管理Python软件包
3. 系统管理模块
- firewalld:使用firewalld管理任意端口和服务
- reboot:重启系统
- service:服务管理
- user:添加、删除和管理用户账户
- cron:计划任务管理
4. 网络工具模块
- get_url:通过 HTTP/HTTPS/FTP 下载文件
- uri:与 Web 服务交互
模块使用语法
ansible [主机或组] -m [模块名] -a '[模块参数]'
命令 主机名称 指定模块 模块名称 模块动作 具体命令
执行状态颜色含义:
- 🟢 绿色:执行成功并且不需要做改变的动作
- 🟡 黄色:执行成功并且对目标主机做变更
- 🔴 红色:执行失败
模块使用案例
案例1:user 模块
-
创建用户并指定 UID:
#临时命令使用user模块来确保newbie用户存在于node1.example.com上,并且其UID为4000: ansible node1 -m user -a 'name=newbie uid=4000 state=present'
-
创建用户并设置密码(若用户存在则更新密码):
openssl passwd -1 redhat #$1$zcVeWQiB$dIsAdkcv91mTjrCaayN3F/ ansible all -m user -a 'name=chenyu password="加密密码" update_password=always'
-
创建用户并设置密码(若用户存在则不更新密码):
openssl passwd -1 redhat #$1$zcVeWQiB$dIsAdkcv91mTjrCaayN3F/ ansible all -m user -a 'name=chenyu12 password="加密密码" update_password=on_create'
案例2:shell 模块
-
删除用户:
#临时命令使用shell模块来删除node1.example.com节点中的用户newbie ansible node1 -m shell -a 'userdel -r newbie'
案例3:copy 模块
-
复制文件并设置属主和组:
ansible node1 -m copy -a 'src=/etc/fstab dest=/var/tmp/fstab owner=chenyu group=chenyu' ansible node1 -m copy -a "content='welcome to luoqi' dest=/www/index.html"
案例4:template 模块
-
复制模板配置文件:
#template模块用法和copy模块用法基本一致,它主要用于复制配置文件 ansible all -m template -a 'src=/usr/share/doc/httpd/httpd-vhosts.conf dest=/etc/httpd/conf.d/httpd-vhosts.conf owner=root group=root mode=0644'
案例5:file 模块
-
修改文件权限属性和context值:
ansible node1 -m file -a 'path=/var/tmp/fstab mode=0666 owner=galaxy group=galaxy setype=samba_share_t' mode:#设置权限可以是mode=g+w 也可以是mode=666 group:#设置文件的所属组 owner:#设置文件的所有者 setype:#修改文件的context值
-
创建文件/目录/软硬链接/删除:
ansible node1 -m file -a 'path=/var/tmp/bbb state=touch' #创建文件 ansible node1 -m file -a 'path=/var/tmp/cc state=directory' #创建目录 ansible node1 -m file -a 'path=/var/tmp/cc state=absent' #删除文件或者目录 ansible node1 -m file -a 'src=/var/tmp/bbb dest=/var/tmp/chenyu state=link' #创建软链接 ansible node1 -m file -a 'src=/var/tmp/aaa dest=/var/tmp/chenyu1 state=hard' #创建硬链接
案例6:yum_repository 模块
-
配置 YUM 仓库:
ansible all -m yum_repository -a 'file=server name=BASE description="software base" baseurl=http://ansible.example.com/rhel9/BaseOS enabled=yes gpgcheck=yes gpgkey=http://ansible.example.com/rhel9/RPM-GPG-KEY-redhat-release'ansible all -m yum_repository -a 'file=server name=STREAM description="software stream" baseurl=http://ansible.example.com/rhel9/AppStream enabled=yes gpgcheck=yes gpgkey=http://ansible.example.com/rhel9/RPM-GPG-KEY-redhat-release'
案例7:yum 模块
-
安装/卸载软件:
ansible all -m yum -a 'name=httpd state=installed' ------安装 ansible all -m yum -a 'name=httpd state=removed' ------卸载
案例8:service 模块
-
启动httpd服务并设置开机自启:
ansible all -m service -a 'name=httpd state=started enabled=yes'
案例9:fetch 模块
-
拉取远程文件到本地:
和copy工作方式类似,只不过是从远程主机将文件拉取到本地端,存储时使用主机名作为目录树,且只能拉取文件,不能拉取目录
#将远程主机的/etc/fstab文件拉取到本地来,存储的名字为/tmp/node1(node2)/etc/fstab ansible all -m fetch -a 'src=/etc/fstab dest=/tmp' #将某台远程主机的/etc/fstab文件拉取到本地来,存储的名字为/tmp/fstab ansible node1 -m fetch -a 'src=/etc/fstab dest=/tmp/ flat=yes' #将远程主机的/etc/fstab文件拉取到本地来,存储的名字为/tmp/fstab-node1(node2) ansible all -m fetch -a 'src=/etc/fstab dest=/tmp/fstab-{{inventory_hostname}} flat=yes'
案例10:firewalld 模块
-
允许 HTTP 流量:
#允许http流量的传入 ansible all -m firewalld -a 'service=http permanent=yes state=enabled immediate=yes'
-
设置富规则:
#允许172.16.30.0/24主机http流量的传入 ansible all -m firewalld -a 'zone=public rich_rule="rule family=ipv4 source address=172.16.30.0/24 service name=http accept" permanent=yes state=enabled immediate=yes'
案例11:replace 模块
-
替换文件中的字符串,backup=yes 设置文件备份:
#replace模块可以根据我们指定的正则表达式替换文件中的字符串,文件中所有被匹配的字符串都会被替换 参数: #path参数:2.3版本之前只能用dest、destfile、name指定操作文件,2.4版本中仍然可以用这些参数名,也可以用path #regexp参数:必须参数,指定一个python正则表达式,文件中与正则匹配的字符串将会被替换 #replace参数:指定最终要替换成的字符串 #backup参数:是否在修改文件之前对文件进行备份,最好设置为yes。 ansible all -m replace -a 'path=/tmp/cy regexp="abc" replace="yyy" backup=yes'
案例12:parted 模块
-
创建分区:
#新建扩展分区ba ansible node1 -m parted -a 'device=/dev/sda number=4 part_type=extended part_start=46GiB part_end=49.8GiB state=present'#新建逻辑分区 ansible node1 -m parted -a 'device=/dev/sda number=5 part_type=logical part_start=46.1GiB part_end=48.2GiB state=present'
案例13:filesystem 模块
-
创建文件系统:
ansible node1 -m filesystem -a 'fstype=xfs dev=/dev/sda5'
案例14:mount 模块
-
挂载文件系统:
#新建挂载点/common ansible node1 -m file -a 'path=/common state=directory'#查看/dev/sda5的UUID ansible node1 -m shell -a 'blkid /dev/sda5'#将分区/dev/sda5挂载到/common目录 ansible node1 -m mount -a 'path=/common src="UUID=d162b8b9-2326-4ee4-a559-80861461c4f0" fstype=xfs state=mounted'#卸载 ansible node1 -m mount -a 'path=/common src="UUID=d162b8b9-2326-4ee4-a559-80861461c4f0" fstype=xfs state=absent'
案例15:lvg 模块
-
创建卷组:
ansible node1 -m lvg -a 'vg=vg0 pvs=/dev/sda5'
案例16:lvol 模块
-
创建逻辑卷:
ansible node1 -m lvol -a 'lv=lv0 vg=vg0 size=1000M'
-
在线扩容逻辑卷:
ansible node1 -m lvol -a 'lv=lv0 size=1600M vg=vg0 resizefs=yes'*
案例17:debug 模块
-
输出调试信息:
#用户输出自定义的信息,类似于echo、print等输出命令。ansible中的debug主要用于输出变量值、表达式值,以及用于when条件判断时。 ansible all -m debug -a 'msg="Hello, Ansible!"'
案例18:cron 模块
-
创建计划任务:
ansible node1 -m cron -a 'name="shuchu" job="/bin/echo I AM RHCE" user=root minute=0 hour=14 state=present'
案例19:get_url 模块
-
下载文件:
ansible node1 -m get_url -a 'url=http://example.com/file.tar.gz dest=/tmp/'
案例20:SELinux 上下文设置
-
设置 SELinux 上下文:
#文件/share,context值改为samba_share_t ansible node1 -m sefcontext -a 'target="/share(/.*)?" setype=samba_share_t state=present' ansible node1 -m shell -a 'restorecon -Rv /share'