Ansible常用模块

 本人只是简略的整理,可能忽略了很多细节

详细文档可以访问这个网址:Ansible「2.9」 中文官方文档 — Ansible Documentation

目录

查看模板

ping模块

command模块(重要)

shell模块(重要)

user模块

file模块

yum模块

service模块

copy模块

script模块

fetch模块

archive模块

cron模块

mount模块

setup 模块


查看模板

用法      ansible-doc 模块名
'在忘记模块具体用法时,可以ansible-doc +模块名,进入后输入/EX'
[root@localhost ~]  ansible-doc copy
...skipping...
EXAMPLES:

- name: Copy file with owner and permissions
  copy:
    src: /srv/myfiles/foo.conf
    dest: /etc/foo.conf
    owner: foo
    group: foo
    mode: '0644'

- name: Copy file with owner and permission, using symbolic representation
  copy:
    src: /srv/myfiles/foo.conf
    dest: /etc/foo.conf
    owner: foo
    group: foo
    mode: u=rw,g=r,o=r

- name: Another symbolic mode example, adding some permissions and removing others
  copy:
    src: /srv/myfiles/foo.conf
    dest: /etc/foo.conf
    owner: foo
    group: foo
    mode: u+rw,g-wx,o-rwx

:

ping模块

用法
ansible  hosts主机清单  -m  模块名

用来测试主机清单中是否可以连接成功

​
[root@localhost ~] ansible servers -m ping
host02 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
host01 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
host03 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}

​


command模块(重要)

用法

ansible  hosts主机清单  -m  模块名

在远程主机上执行指定得命令 如:cat ls ,不能使用特殊得符号 :| > >>

好处是默认使用,可以省略

[root@localhost ~] ansible servers -a 'pwd'
host01 | CHANGED | rc=0 >>
/root
host02 | CHANGED | rc=0 >>
/root
host03 | CHANGED | rc=0 >>
/root

shell模块(重要)

shell模块可以在远程主机上执行复杂命令,,非常好用,如果一些模块忘记,或者嫌麻烦可以直接用shell执行命令行


user模块

用法

ansible  hosts主机清单  -m  模块名

​
创建用户
name:             指定用户名,如果用户不存在,则创建该用户指定用户的uid号,附加组
append=yes	       增量添加附加组
uid:               指定用户的uid
groups:           指定用户的附加组


删除用户
remove=yes	        删除用户的同时删除家目录
state=absent        删除用户

ansible dbserver -m user -a 'name=xx state=absent remove=yes'	#即删除用户,又删除了用户家目录


创建无家目录,不能登录的用户
ansible 操作对象 -m user -a 'name=用户名 create_home=no shell=/sbin/nologin state=present'  

​

file模块

创建一个目录
ansible all -m file -a 'path=/test state=directory'

创建一个文件
ansible all -m file -a 'path=/test/123 state=touch'

递归修改owner,group,mode
ansible all -m file -a 'path=/test recurse=yes owner=han group=http mode=1777'

删除目录(连同目录里的所有文件)
ansible all -m file -a 'path=/test state=absent'

创建文件并指定owner,group,mode等
ansible all -m file -a 'path=/tmp/123 state=touch owner=han group=http mode=1777'

删除文件
ansible all -m file -a 'path=/tmp/123 state=absent'

创建软链接文件
ansible all -m file -a 'src=/etc/abc path=/tmp/abc state=link'

创建硬链接文件
ansible all -m file -a 'src=/etc/abc2 path=/tmp/abc2 state=hard'

yum模块

用法

ansible  hosts主机清单  -m  模块名

state:installed             安装软件包
	   removed               卸载软件包

disable_gpg_check=yes        取消密钥的认证
update_cache=yes             更新缓存,需要在指定安装包时使用

在执行命令之前,要保证有可用的仓库

1.安装一个httpdyum源
ansible servers -m yum -a 'name=wget* state=installed'

2.安装一个独立的rpm软件包
ansible servers -m yum -a 'name=rpm软件包'

3.安装一个nginx,并清除缓存
ansible servers -m yum -a 'name=nginx update_cache=yes'


service模块

用法

ansible  hosts主机清单  -m  模块名

name                    服务名
state=started           开启 / stopped 停止 / reloaded 重新加载 /  restarted重启
enabled=yes             开机自启

1.启动httpd服务
[root@localhost ~] ansible servers -m service -a 'name=httpd state=started'

2.让httpd服务开机自启
[root@localhost ~] ansible servers -m service -a 'name=httpd enabled=yes'

3.检测httpd是否开机自启
[root@localhost ~] ansible servers -m shell -a 'systemctl is-enabled httpd'
enabled			开机自启状态
disablednon-zero return code		非开机自启状态

copy模块

用法

ansible  hosts主机清单  -m  模块名

src             源文件路径
dest            目标文件路径
content         将指定内容覆盖写入到目标主机文件中
force=no 	    当主控端拷贝的文件名和目标名一致,但是内容不一致,放弃拷贝
force=yes       当主控端拷贝的文件名和目标名一致,但是内容不一致,则进行覆盖
backup=yes	    当主控端拷贝的文件名和目标名一致,但是内容不一致,则进行备份

1.将源文件,copy到被控端
[root@localhost ~] ansible servers -m copy -a 'src=/root/1.txt dest=/tmp/1.txt'

2.将指定内容覆盖到被控端文件内
[root@localhost ~] ansible servers -m copy -a "content='this is content\n hello world' dest=/tmp/1.txt"

3.将文件发送到被控端,如果内容不同则进行备份,并覆盖
[root@localhost ~] ansible servers -m copy -a 'src=/root/1.txt dest=/tmp/1.txt backup=yes'
[root@localhost ~] ansible servers -m shell -a 'ls /tmp/1.*'
host01 | CHANGED | rc=0 >>
/tmp/1.txt
/tmp/1.txt.17171.2023-07-27@17:36:14~
host02 | CHANGED | rc=0 >>
/tmp/1.txt
/tmp/1.txt.17563.2023-07-27@17:36:14~
host03 | CHANGED | rc=0 >>
/tmp/1.txt
/tmp/1.txt.18041.2023-07-27@17:36:14~



权限参数
owner  属主
group  属组
mode   权限
1. 将 2.txt 文件拷贝到被控端,并设置好属主,属组,权限
[root@localhost ~] ansible servers -m copy -a 'src=/root/2.txt dest=/tmp owner=root group=han mode=744'
[root@localhost ~] ansible servers -m shell -a 'ls -lhd /tmp/2.txt'
host03 | CHANGED | rc=0 >>
-rwxr--r--. 1 root han 5 7月  27 17:38 /tmp/2.txt
host02 | CHANGED | rc=0 >>
-rwxr--r--. 1 root han 5 7月  27 17:38 /tmp/2.txt
host01 | CHANGED | rc=0 >>
-rwxr--r--. 1 root han 5 7月  27 17:38 /tmp/2.txt

script模块

用法

ansible  hosts主机清单  -m  模块名

chdir=/目录        进入到指定目录
creates            文件存在 脚本不执行
removes            文件存在 脚本执行

1.在ansible创建一个脚本,并赋予执行权限
[root@localhost ~] vim 1.sh
#!/bin/bash
echo 'hello world!' > /tmp/1.txt
[root@localhost ~] chmod +x 1.sh 

2.在被控端执行脚本
[root@localhost ~] ansible hosts01 -m script -a 'chdir=/root 1.sh'
[root@localhost ~] ansible servers -m command -a 'cat /tmp/1.txt'
10.10.10.131 | CHANGED | rc=0 >>
hello world!

3.可以针对某个文件存在或者不存在来执行脚本
[root@localhost ~] ansible host01 -m script -a 'creates=/etc/passwd chdir=/root 2.sh'
10.10.10.131 | SKIPPED		/etc/passwd文件存在,所以后面的语句被跳过

fetch模块

用法

ansible  hosts主机清单  -m  模块名

src:  被控端源文件
dest: 主控端目录位置

拉取/tmp/1.txt 到主控端
[root@localhost ~] ansible host01 -m fetch -a 'src=/tmp/1.txt dest=/tmp/host01-1.txt'
[root@localhost ~] ll /tmp/db-1.txt/
10.10.10.131/tmp/1.txt 			以IP地址的形式保存在主控端


archive模块

用法

ansible  hosts主机清单  -m  模块名

path:源路径,准备打包的文件
dest:打成什么格式的包,放到什么位置
mode:权限

#打包
[root@localhost ~] ansible servers -m archive -a 'path=/var/*.txt dest=/root/var.tar.gz'

cron模块

用法

ansible  hosts主机清单  -m  模块名

minute	分		minute=*             每分钟
hour 	时		special_time=hourly  每小时
day	    天
month	月
weekday	周
job              计划任务的工作
name=*           计算任务名称
disbaled=true    禁用某个计划任务
disabled=false   再次开启某个计划任务
state=absent     删除某个计划任务
 
 1.每天10点查看磁盘状态
 [root@localhost ~] ansible servers -m cron -a 'hour=10  job="df -h" name=fdisk'

2.将设置好的计划任务禁用(必须带上 job=工作任务,name=计划任务名称)
[root@localhost ~]# ansible servers -m cron -a 'disabled=true name=fdisk job="df -h"'

3.删除创建好的任务计划(必须带上 job=工作任务,name=计划任务名称)
[root@localhost ansible] ansible servers -m cron -a 'name=fdisk job="df -h" state=absent'


mount模块

用法

ansible  hosts主机清单  -m  模块名

path	    挂载点
src	        挂载的文件
fstype	    挂载的硬盘类型 比如iso9660、ext4、xfs、nfs、cifs   samba的共享文件系统 ntfs windows磁盘文件系统

opts	    传递给mount命令的参数
state	    present 开机挂载,仅将挂载配置写入/etc/fstab并不会真的挂载
mounted     挂载设备,并将配置写入/etc/fstab
unmounted   卸载设备,不会清除/etc/fstab写入的配置
absent      卸载设备,并清理/etc/fstab写入的配置


#将host01的/dev/sdb2挂载到/AAB目录下
ansible host01 -m mount -a 'path=/AAB src="/dev/sdb2" fstype=xfs state=mounted'

#卸载
ansible host011 -m mount -a 'path=/AAB src="/dev/sdb2" fstype=xfs state=absent'

setup 模块

非常详细

[root@localhost ~] ansible host01 -m setup 
host01 | SUCCESS => {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "10.10.10.129"
        ], 
        "ansible_all_ipv6_addresses": [
            "fe80::326b:97eb:8b57:251f", 
            "fe80::f93d:8ea1:7c34:6298", 
            "fe80::2305:7587:e8cf:11f3"
        ], 
        "ansible_apparmor": {
            "status": "disabled"
        }, 
        "ansible_architecture": "x86_64", 
        "ansible_bios_date": "11/12/2020", 
        "ansible_bios_version": "6.00", 
        "ansible_cmdline": {
            "BOOT_IMAGE": "/vmlinuz-3.10.0-1160.el7.x86_64", 
            "LANG": "zh_CN.UTF-8", 
            "crashkernel": "auto", 
            "quiet": true, 
            "rd.lvm.lv": "centos/swap", 
后面非常长,省略

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值