Ansible中常用模块

目录

一、Ansible实现管理的方式

二、Ad-Hoc执行方式中如何获得帮助​​​​​​​

三、Ansible命令运行方式及常用参数

四、Ansible的基本颜色代表信

五、Ansible中的常用模块

1、command模块

2、shell模块、script模块

3、copy模块、fetch模块

4、file模块

5、archive模块、unarchive模块

6、hostname模块、cron模块

7、yum_repository模块、yum(dnf)模块

8、service模块、firewalld模块

9、user模块、group模块

10、lineinfile模块、replace模块

11、setup模块、debug模块


一、Ansible实现管理的方式

Ad-Hoc 利用ansible命令直接完成管理,主要用于临时命令使用场景
playbook ansible脚本,主要用于大型项目场景,需要前期的规划

二、Ad-Hoc执行方式中如何获得帮助

ansible-doc 显示模块帮助的指令
格式
ansible-doc [参数] [模块...]

常用参数
-l   列出可用模块
-s   显示指定模块的playbook片段
vim test.yml
cat test.yml
ansible-playbook test.yml

ansible-doc
ansible-doc -l | wc -l
ansible-doc -l | grrep shell

 

三、Ansible命令运行方式及常用参数

参数 功能
-i 指定hosts文件路径,默认在/etc/ansible/hosts
-m 指定使用的module名称,默认command模块
-a 指定模块参数
-k(小写) 提示输入ssh密码,并非基于ssh密钥认证
-K(大写) 提示输入sudo密码
-b 使用sudo执行命令
-become-user= 指定sudo的用户
-f,-forks=NUM NUM默认是整数5,指定fork开启同步进程的个数
-u 指定远程主机的执行用户
-v 详细模式,如果执行成功,输出详细结果,-vv -vvv更详细过程
-C 预执行检测
-T 执行命令的超时时间,默认10s
--list 显示主机列表,也可以用--list-hosts
--version 显示版本
ansible --version
ansible all -m shell -a 'whoami' -C
ansible all -m shell -a 'whoami' -u test
ansible all -m shell -a 'whoami' -utest
ansible all -m shell -a 'whoami'
ansible all -m shell -a 'whoami' -b -become-user=yyl
ansible all -m shell -a 'whoami' -b -become-user=yyl -K

 

四、Ansible的基本颜色代表信

绿色 执行成功但未对远程主机做任何改变
黄色 执行成功并对远程主机做改变
红色 执行失败

五、Ansible中的常用模块

1、command模块

在远程主机执行命令,此模块为默认模块

常用参数 功能
chdir 执行命令前先进入到指定目录
cmd 运行命令指定
creates 如果文件存在将不运行
removes 如果文件存在将运行
ansible all -m command -a 'chdir=/mnt pwd'
ansible all -m command -a 'chdir=/mnt cmd=pwd'
ansible all -m command -a 'chdir=/mnt  creates=/etc/passwd cat passwd'
ansible all -m command -a 'chdir=/mnt  removes=/etc/passwd cat passwd'
ansible all -m command -a 'chdir=/mnt  removes=/mnt/file cat passwd'
ansible all -m command -a 'chdir=/mnt touch file'
ansible all -m command -a 'chdir=/mnt  creates=/mnt/file pwd'
ansible all -m command -a 'chdir=/mnt  removes=/mnt/file pwd'

注意
Linux中的很多通配符在command模块中不支持

ansible all -m command -a "chdir=/mnt touch file{1..10}"

2、shell模块、script模块

(1)shell模块
shell模块与command模块类似

常用参数 功能
chdir 执行命令前先进入到指定目录
cmd 运行命令指定
creates 如果文件存在将不运行
removes 如果文件存在将运行
executable 指定执行环境,默认为sh
ansible all -m shell -a 'chdir=/mnt/ touch file{1..3}'
ansible all -m shell -a 'chdir=/mnt/ ls -ld /mnt'
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt'

ansible all -m shell -a 'ps ax | grep $$'
ansible all -m shell -a 'executable=/bin/bash ps ax | grep $$'

(2)script模块
在ansible主机中写好的脚本在受控主机中执行

vim test.sh
cat test.sh
ansible all -m script -a "test.sh"
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt'

3、copy模块、fetch模块

(1)copy模块
从ansible主机复制文件到受控主机

参数 功能
src 源文件
dest 目的地文件
owner 指定目的地文件所有人
group 指定目的地文件所有组
mode 指定目的地文件权限
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值