ansible模块介绍(三)
stat模块
查看文件/目录状态
参数:
checksum_algorithm 确定文件校验和的算法,如果主机无法使用指定的算法,则会报错
设置的值有:sha1 md5 sha224 sha256 sha384 sha512
follow:是否遵循符号链接,设置为yes或no
get_attributes:如果存在,通过lasttr获取文件属性,设置为yes或no
get_checksum:是否返回文件的校验和,设置为yes或no
get_mime:使用file magic并返回有关文件性质的信息,设置为yes或no
path:目录/文件的路径
命令格式:ansible change -m stat -a'path=/opt/es' 判断/opt/es是否存在
输出状态
文件/目录不存在
192.168.88.128 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "stat": { "exists": false } }
目录存在
192.168.88.160 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "stat": { "atime": 1678176744.7570179, "attr_flags": "", "attributes": [], "block_size": 4096, "blocks": 0, "charset": "binary", "ctime": 1677745804.1540623, "dev": 64770, "device_type": 0, "executable": true, "exists": true, "gid": 1001, "gr_name": "esuser", "inode": 2648597, "isblk": false, "ischr": false, "isdir": true, "isfifo": false, "isgid": false, "islnk": false, "isreg": false, "issock": false, "isuid": false, "mimetype": "inode/directory", "mode": "0750", "mtime": 1677745804.1540623, "nlink": 2, "path": "/opt/es", "pw_name": "esuser", "readable": true, "rgrp": true, "roth": false, "rusr": true, "size": 20, "uid": 1001, "version": "18446744072857729717", "wgrp": false, "woth": false, "writeable": true, "wusr": true, "xgrp": true, "xoth": false, "xusr": true } }
文件存在,不为软链接文件
192.168.88.159 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "stat": { "atime": 1678177720.5080998, "attr_flags": "", "attributes": [], "block_size": 4096, "blocks": 8, "charset": "utf-8", "checksum": "f0230c2daa61042aa327a81d7851b8bd2b270fe2", "ctime": 1676007310.4569924, "dev": 64772, "device_type": 0, "executable": false, "exists": true, "gid": 0, "gr_name": "root", "inode": 33574987, "isblk": false, "ischr": false, "isdir": false, "isfifo": false, "isgid": false, "islnk": false, "isreg": true, "issock": false, "isuid": false, "mimetype": "text/x-shellscript", "mode": "0644", "mtime": 1676007310.4569924, "nlink": 1, "path": "/root/lvm_create.sh", "pw_name": "root", "readable": true, "rgrp": true, "roth": true, "rusr": true, "size": 2796, "uid": 0, "version": "666573522", "wgrp": false, "woth": false, "writeable": true, "wusr": true, "xgrp": false, "xoth": false, "xusr": false } }
文件存在,且为软链接文件
192.168.88.159 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "stat": { "atime": 1678177411.8550813, "attr_flags": "", "attributes": [], "block_size": 4096, "blocks": 0, "charset": "binary", "ctime": 1677745804.1622763, "dev": 64772, "device_type": 0, "executable": true, "exists": true, "gid": 0, "gr_name": "root", "inode": 18100766, "isblk": false, "ischr": false, "isdir": false, "isfifo": false, "isgid": false, "islnk": true, "isreg": false, "issock": false, "isuid": false, "lnk_source": "/var/es1", "lnk_target": "/var/es1", "mimetype": "inode/symlink", "mode": "0777", "mtime": 1677745804.1622763, "nlink": 1, "path": "/opt/es/esfile", "pw_name": "root", "readable": true, "rgrp": true, "roth": true, "rusr": true, "size": 8, "uid": 0, "version": null, "wgrp": true, "woth": true, "writeable": true, "wusr": true, "xgrp": true, "xoth": true, "xusr": true } }
setup模块
用来收集主机的相关信息
参数:
ansible_all_ipv4_addresses: 仅显示ipv4的信息。
ansible_default_ipv4.address:服务器主ip信息,需要用在playbook中
ansible_devices: 仅显示磁盘设备信息。
ansible_distribution: 显示是什么系统,例:centos,suse等。
ansible_distribution_major_version: 显示是系统主版本。
ansible_distribution_version: 仅显示系统版本。
ansible_machine: 显示系统类型,例:32位,还是64位。
ansible_eth0: 仅显示eth0的信息。
ansible_hostname: 仅显示主机名。
ansible_kernel: 仅显示内核版本。
ansible_lvm: 显示lvm相关信息。
ansible_memtotal_mb: 显示系统总内存。
ansible_memfree_mb: 显示可用系统内存。
ansible_memory_mb: 详细显示内存情况。
ansible_swaptotal_mb: 显示总的swap内存。
ansible_swapfree_mb: 显示swap内存的可用内存。
ansible_mounts: 显示系统磁盘挂载情况。
ansible_processor: 显示cpu个数(具体显示每个cpu的型号)。
ansible_processor_vcpus: 显示cpu个数(只显示总的个数)。
例:输出主机所有的ipv4地址信息
ansible change -m setup -a'filter=ansible_all_ipv4_addresses'
输出主机名称信息
ansible change -m setup -a'filter=ansible_hostname'
setup在playbook用法:
ansible_default_ipv4.address
例:输出每台服务器的主ip
--- - hosts: change tasks: - debug: msg: ip is {{ ansible_default_ipv4.address }}
script模块
用于远程执行ansible主机上的脚本
参数:
chdir:在执行脚本前需要在远程主机上进入的目录
cmd:脚本在ansible主机上的路径
creates:如果远程主机上存在该文件的路径,那么不执行脚本
removes:如果远程主机上不存在该文件的路径,那么不执行脚本
decrypt:使用vault自动解密源文件
executable:调用脚本的可执行文件的名称或路径
free_form:本地脚本文件的路径 一般不用这个参数
批量执行脚本:/root/test.sh
ansible change -m script -a'/root/test.sh'
在playbook中的写法:
--- - hosts: change gather_facts: no tasks: - name: batch excute script script: /root/test.sh register: tes - debug: var=tes.stdout ignore_errors: yes ##or,使用cmd参数 --- - hosts: change gather_facts: no tasks: - name: batch excute script script: cmd: /root/test.sh register: tes - debug: var=tes.stdout ignore_errors: yes
test.sh脚本:
#!/bin/bash if [ -f "/root/11212.srw" ];then echo "success: excute script" else echo "error: excute failed" exit 10 fi echo 'excute finish'