ansible 常用模块
Posted Forande
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ansible 常用模块相关的知识,希望对你有一定的参考价值。
[toc]
一、ansible常用模块
1、远程命令模块
模块包括command、script、shell,都可以实现远程shell命令运行。
- command作为ansible的默认模块,可以运行远程权限范围所有的shell命令;
```shell ansible master -m command -a free -m ```
- scirpt功能是在远程主机上执行
主控端(ansible master主机)
存储的shell脚本,相当于scp + shell
组合;
```shell ansible master -m script -a /home/test.sh aa bb ```
- shell功能是执行远程主机的shell脚本文件;
```bash ansible master -m shell -a /home/test.sh ansible master -m shell -a ls && free -m ```
2、copy模块
- 功能
实现主控端向目标拷贝文件,类似scp的功能 其中选项有: 1. src:拷贝的源文件 2. dest:拷贝的目标地点 3. owner :拥有者 4. group:拥有组 5. mode:文件全力
- 例子
拷贝/home/test.sh
文件到master主机组的/tmp
目录下,并更新文件属组及权限(可以单独使用file实现权限的修改)
```bash
ansible master -m copy -a src=/home/test.sh dest=/tmp/ owner=root group=root mode=0755
```
3、fetch 模块
- 功能
实现目标服务器向主控端拷贝文件,类似scp的功能
- 例子
拷贝/home/test.sh
文件到master主机组的/tmp
目录下,并更新文件属组及权限(可以单独使用file实现权限的修改)
```bash
ansible master -m fetch -a src=/home/test.sh dest=/tmp/ owner=root group=root mode=0755
```
3、state 模块
- 功能
获取远程文件状态信息,包括atime、ctime、mtime、MD5、uid、gid
等信息
- 例子
```bash ansible master -m state -a path=/etc/sysctl.conf ```
4、get_url 模块
- 功能:
实现在远程主机下载指定URL到本地,支持sha256sum文件校验
- 例子:
```bash ansible master -m get_url -a url=http://www.baidu.com dest=/tmp/index.html mode=0440 froce=yes ```
5、yum 模块
- 功能
Linux 平台包管理操作,常见有yum、apt
管理方式
- 例子
```bash ansible master -m apt -a pkg=curl state=latest ansible master -m yum -a name=curl state=latest ```
6、cron 模块
- 功能
远程主机crontab配置
- 例子
```bash ansible master -m cron -a "name=check dirs hour=5,2 job=ls -alh > /dev/null" ``` >minute 分 > >hour >时 > >day >日 > >month 月 > >weekday 周 > >user 用什么用户 > >state 状态,启用或者停用absent(停用), present(默认启动)
- 效果如下:
```bash #Ansible: check dirs * 5,2 * * * ls -alh > /dev/null ```
7、mount 模块
- 功能
远程主机分区挂载
- 例子
```bash ansible master -m mount -a name=/mnt/data src=/dev/sd0 fstype=ext3 opts=ro state=present ```
8、service 模块
- 功能
远程主机系统服务管理
- 例子
```bash ansible master -m service -a name=nginx state=stopped ansible master -m service -a name=nginx state=started ansible master -m service -a name=nginx state=reloaded ```
9、sysctl 包管理模块
- 功能
远程Linux主机sysctl配置
- 例子
sysctl: name=kernel.panic value=3 sysctl_file=/etc/sysctl.conf checks=before reload=yessalt * pkg.upgrade
10、user 模块
- 功能
远程主机系统管理
- 例子
# 添加用户user1
ansible master -m user -a name=user1 comment="user done"
# 删除用户user1
ansible master -m user -a name=user1 state=absent remove=yes
11、unarchive(解压)
1. 功能 - 用于tar文档的解压 2. 例子
ansible master -m unarchive -a src=aa.tar.gz dest=/tmp
12、debug
- 功能
- 显示执行过程中打印的语句
- 可以获得对象主机的所有信息默认变量
- 例子1
- debug:
msg: System inventory_hostname has uuid ansible_product_uuid
- name: 显示主机已知的所有变量
debug: hostvars[invenroty_hostname]
verbosity: 4
- 例子2
$ ansible -i 10.0.0.20, all -m debug -a var=hostvars
10.0.0.20 | SUCCESS =>
"hostvars":
"10.0.0.20":
"ansible_check_mode": false,
"ansible_diff_mode": false,
"ansible_facts": ,
"ansible_forks": 5,
"ansible_inventory_sources": [
"10.0.0.20,"
],
"ansible_playbook_python": "/usr/bin/python2",
"ansible_verbosity": 0,
"ansible_version":
"full": "2.9.24",
"major": 2,
"minor": 9,
"revision": 24,
"string": "2.9.24"
,
"group_names": [
"ungrouped"
],
"groups":
"all": [
"10.0.0.20"
],
"ungrouped": [
"10.0.0.20"
]
,
"inventory_dir": "None",
"inventory_file": "10.0.0.20,",
"inventory_hostname": "10.0.0.20",
"inventory_hostname_short": "10",
"omit": "__omit_place_holder__d3bbd9ef9734011f8284f7cc38e07879fcc1cb6c",
"playbook_dir": "/root"
13、setup 获取主机所有信息
- 功能:用来获取主机的所有信息
- 例子:
root@entos7415:08:35 :~
$ ansible -i 10.0.0.20, all -m setup
10.0.0.20 | SUCCESS =>
"ansible_facts"
"ansible_all_ipv4_addresses"
"10.0.0.20"
"192.168.18.132"
"ansible_all_ipv6_addresses"
"fe80::20c:29ff:fe84:cc5c"
"fe80::20c:29ff:fe84:cc52"
"ansible_apparmor"
"status""disabled"
"ansible_architecture""x86_64"
"ansible_bios_date""07/29/2019"
"ansible_bios_version""6.00"
"ansible_cmdline"
"BOOT_IMAGE""/vmlinuz-3.10.0-693.el7.x86_64"
"LANG""en_US.UTF-8"
"crashkernel""auto"
"quiet" true
"rd.lvm.lv""centos/swap"
"rhgb" true
"ro" true
"root""/dev/mapper/centos-root"
"ansible_date_time"
"date""2021-08-24"
"day""24"
"epoch""1629788941"
"hour""15"
"iso8601""2021-08-24T07:09:01Z"
"iso8601_basic""20210824T150901271841"
"iso8601_basic_short""20210824T150901"
"iso8601_micro""2021-08-24T07:09:01.271841Z"
"minute""09"
"month""08"
"second""01"
"time""15:09:01"
"tz""CST"
"tz_offset""+0800"
"weekday""Tuesday"
"weekday_number""2"
"weeknumber""34"
"year""2021"
"ansible_default_ipv4"
"address""192.168.18.132"
"alias""ens33"
"broadcast""192.168.18.255"
"gateway""192.168.18.2"
"interface""ens33"
"macaddress""00:0c:29:84:cc:52"
"mtu"1500
"netmask""255.255.255.0"
"network""192.168.18.0"
"type""ether"
"ansible_default_ipv6"
"ansible_device_links"
"ids"
"dm-0"
"dm-name-centos-root"
"dm-uuid-LVM-mNuDaWzDXPB0zrhauSnwrfddWxv110bG4rV4VnftTT3SjKOwlJHofOMJKafVRbU1"
"dm-1"
"dm-name-centos-swap"
"dm-uuid-LVM-mNuDaWzDXPB0zrhauSnwrfddWxv110bGkJXIV2ocufNh3VPo5w1W4E7rIrc1MgRf"
"sda2"
"lvm-pv-uuid-cQ9ruQ-sA5U-wPIe-XB3Y-nmO7-CS3T-RFSw0c"
"sr0"
"ata-VMware_Virtual_IDE_CDROM_Drive_10000000000000000001"
"labels"
"sr0"
"CentOS\\\\x207\\\\x20x86_64"
"masters"
"sda2"
"dm-0"
"dm-1"
ansible plugins简介