美文网首页
ansible 常用模块及应用

ansible 常用模块及应用

作者: 霸道ki | 来源:发表于2020-03-08 15:34 被阅读0次

1. 命令类型模块

1.1 command 模块

1. chdir -- 在执行命令之前对目录进行切换
[root@muban ~]# ansible 172.16.1.31 -m command -a "chdir=/tmp touch zun.txt"

2. creates -- 如果文件存在了,不执行命令操作
[root@muban ~]# ansible 172.16.1.31 -m command -a "creates=/tmp/hosts touch zun.txt" 

3. removes -- 如果文件存在了,这个步骤将执行
[root@muban ~]# ansible 172.16.1.31 -m command -a "removes=/tmp/hosts chdir=/tmp touch zun.txt"

4. free_form(required) -- 使用command模块的时候,-a参数后面必须写上一个合法linux命令信息

注意事项:
有些符号信息无法识别:  <", ">", "|", ";" and "&"

1.2 shell 模块

功能与command类似

1.3 scripts模块

功能与command类似

2. 文件类型模块

2.1 copy 模块

1. 在传输文件时修改文件的属主和属组信息
[root@muban ~]# ansible 172.16.1.31 -m copy -a "src=/etc/ansible/file/rsync/rsync.password dest=/etc/ owner=zun group=zun"

2. 在传输文件时修改文件的权限信息
[root@muban ~]# ansible 172.16.1.31 -m copy -a "src=/etc/ansible/file/rsync/rsync.password dest=/etc/ mode=1777"
    
3. 在传输数据文件信息时对远程主机源文件进行备份 
[root@muban ~]# ansible 172.16.1.31 -m copy -a "src=/etc/ansible/file/rsync/rsync.password dest=/etc/ backup=yes"

4. 创建一个文件并直接编辑文件的信息
[root@muban ~]# ansible 172.16.1.31 -m copy -a "content='abc123' dest=/etc/rsync.password"

PS:ansible软件copy模块复制目录信息
1) src后面目录没有/: 将目录本身以及目录下面的内容都进行远程传输复制
[root@muban ~]# ansible 172.16.1.31 -m copy -a "src=/zun dest=/tmp"  

2) src后面目录有/:   只将目录下面的内容都进行远程传输复制
[root@muban ~]# ansible 172.16.1.31 -m copy -a "src=/zun/ dest=/tmp"  

2.2 file 模块

1. 创建目录 /abc
[root@muban ~]# ansible all -m file -a "dest=/abc state=directory"

2. 在 /abc 目录下创建名为 haha 的文件
[root@muban ~]# ansible all -m file -a "dest=/abc/haha state=touch"

3. 创建链接文件
# 硬连接
[root@muban ~]# ansible all -m file -a "src=/abc/haha dest=/tmp/haha_hard state=hard"
# 软链接
[root@muban ~]# ansible all -m file -a "src=/abc/haha dest=/tmp/haha_link state=link"

4. 删除文件/目录
[root@muban ~]# ansible all -m file -a "dest=/tmp/haha_link state=absent"
[root@muban ~]# ansible all -m file -a "dest=/abc/ state=absent"

5. 递归授权(目录及目录一下的文件)
[root@muban ~]# ansible all -m file -a "dest=/abc mode=777 recurse=yes"

2.3 fetch 模块

# 将被管理端主机的文件拉去到管理端主机
[root@muban ~]# ansible all -m fetch -a "src=/tmp/zun.txt dest=/tmp"

3. 其他模块

3.1 yum 模块

# 安装软件
[root@muban ~]# ansible all -m yum -a "name=ab state=installed"

# 卸载软件
[root@muban ~]# ansible all -m yum -a "name=htop state=absent"

3.2 service 模块

# 启动 sshd 服务并设为开机自启
[root@muban ~]# ansible all -m service -a "name=sshd state=started enabled=yes"

started     启动
restarted   重启
stoped      关闭

3.3 cron 模块

# 添加定时任务(每天2:00)
[root@muban ~]# ansible all -m cron -a "name='time sync' minute=0 hour=2 job='/usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1'"

# 删除定时任务
[root@muban ~]# ansible all -m cron -a "name='time sync' state=absent"

# 注释定时任务
[root@muban ~]# ansible all -m cron -a "name='time rsyn' hour=2 minute=0 job='/usr/sbin/update ntp1.aliyun.com >/dev/null 2>&1' disabled=yes"

# 取消注释
[root@muban ~]# ansible all -m cron -a "name='time rsyn' hour=2 minute=0 job='/usr/sbin/update ntp1.aliyun.com >/dev/null 2>&1' disabled=no"

name -- 注释
job -- 执行的任务
minute -- 分钟
hour -- 小时
day -- 日期
month -- 月份
weekday -- 周

mount 模块

# 挂载
[root@muban ~]# ansible 172.16.1.99 -m mount -a "src=/dev/sdb path=/mnt fstype=xfs state=mounted"

src:  需要挂载的存储设备或文件信息
path: 指定目标挂载点目录
fstype: 指定挂载时的文件系统类型
state
present: 不会实现立即挂载,修改fstab文件,实现开机自动挂载
mounted: 会实现立即挂载, 并且会修改fstab文件,实现开机自动挂载 *****
absent:会实现立即卸载, 并且会删除fstab文件信息,禁止开机自动挂载
unmounted:  会实现立即卸载, 但是不会会删除fstab文件信息  *****

user 模块

1. 指定用户uid信息
[root@muban ~]# ansible 172.16.1.31 -m user -a "name=zun02 uid=1234"

2. 指定用户组信息
[root@muban ~]# ansible 172.16.1.31 -m user -a "name=zun03 group=zun02"
[root@muban ~]# ansible 172.16.1.31 -m user -a "name=zun04 groups=zun02"
    
3. 批量创建虚拟用户
[root@muban ~]# ansible 172.16.1.31 -m user -a "name=rsync create_home=no  shell=/sbin/nologin"

4. 给指定用户创建密码
PS: 利用ansible程序user模块设置用户密码信息,需要将密码明文信息转换为密文信息进行设置
# 环境需要
[root@muban ~]# yum install -y python-pip
[root@muban ~]# pip install passlib

# 生成加密密码
[root@muban ~]# python -c "from passlib.hash import sha512_crypt; import getpass; print(sha512_crypt.using(rounds=5000).hash(getpass.getpass()))"
Password: 
$6$EKCL5OheycvN3AsN$kA4M64s98u5WyM.Icsy.SndY/8txD3zgE0XhKTwh0IJRmigFgIwCsTl7yziY1ZH.AEcc9uoobf.3J49yp25Sa/

# 创建用户指定密码
[root@muban ~]# ansible all -m user -a 'name=zun666 password=$6$EKCL5OheycvN3AsN$kA4M64s98u5WyM.Icsy.SndY/8txD3zgE0XhKTwh0IJRmigFgIwCsTl7yziY1ZH.AEcc9uoobf.3J49yp25Sa/'

相关文章

网友评论

      本文标题:ansible 常用模块及应用

      本文链接:https://www.haomeiwen.com/subject/pmnddhtx.html