Ansible Ad-Hoc基础模块

参考文档:Ansible自动化运维(二)ad-hoc 模式详解_ad-hoc执行过程-CSDN博客

Ansible Ad-Hoc(临时命令,如果无需反复执行,可以采用该方式)

1.命令格式

ansible 主机组名称 -m 模块 -i 资产文件 -a “具体命令”
为了方便测试,我这里直接让控制节点控制自己
cat << EOF >>/etc/ansible/hosts
[localhost]
192.168.113.254		#这里改成自己要管理的节点,当然你也可以像我一样写上自己的管理节点
EOF
[root@docker ~]# echo "mkdir /root/ansible-test" > create.sh
[root@docker ~]# cat create.sh 
mkdir ansible-test
注意:要添加免密认证
cat /root/.ssh/id_rsa.pub >/root/.ssh/authorized_keys

2.shell模块

不指定模块的情况下,Ansible会默认使用command模块,command模块不支持管道

使用shell模块

[root@docker ~]# ansible localhost -i /etc/ansible/hosts -m shell -a  "echo 'ansible' > /root/test.txt"
192.168.113.254 | CHANGED | rc=0 >>

验证

#验证
[root@docker ~]# ansible localhost -i /etc/ansible/hosts -a "cat /root/test.txt"
192.168.113.254 | CHANGED | rc=0 >>
ansible
#或者
[root@docker ~]# cat test.txt 
ansible

3.script模块--执行脚本

注意:指定脚本时一定要写绝对路径,否则就会报下列错误

[root@docker .ssh]# ansible localhost -i /etc/ansible/hosts -m script  -a "create.sh"
192.168.113.254 | FAILED! => {
    "changed": false, 
    "msg": "Could not find or access 'create.sh'\nSearched in:\n\t/root/.ssh/files/create.sh\n\t/root/.ssh/create.sh\n\t./files/create.sh\n\t./create.sh on the Ansible Controller.\nIf you are using a module and expect the file to exist on the remote, see the remote_src option"
}

修改为绝对路径后

[root@docker .ssh]# ansible localhost -i /etc/ansible/hosts -m script  -a "/root/create.sh"

验证

# ansible localhost -i /etc/ansible/hosts -m shell -a "ls -l /root/|grep ansible-test"
192.168.113.254 | CHANGED | rc=0 >>
drwxr-xr-x. 2 root root    6 5月  21 11:19 ansible-test

# ll|grep ansible-test
drwxr-xr-x. 2 root root    6 5月  21 11:06 ansible-test

4.COPY模块

常用参数:

src:源路径
dest:目标路径
backup:备份原文件再替换
owner:拷贝后文件的所有者
group:所属组
mode:权限

1)例如:我们需要给每台主机添加yum源

vim myrepo.repo
[base]
name=CentOS7
baseurl=https://repo.huaweicloud.com/centos/$releasever/os/$basearch/
gpgcheck=0

ad-hoc给主机添加yum源

src可以是相对路径

ansible localhost -i /etc/ansible/hosts -m copy -a "src=/root/myrepo.repo dest=/etc/yum.repos.d/"

验证

ansible localhost -i /etc/ansible/hosts -m shell -a "cat /etc/yum.repos.d/myrepo.repo"

2)使用备份参数

稍微修改一下参数

#稍微修改一下参数
vim myrepo.repo
[base]
name=CentOS7
baseurl=https://repo.huaweicloud.com/centos/$releasever/os/$basearch/
gpgcheck=0
enabled=1
ansible localhost -i /etc/ansible/hosts -m copy -a "src=/root/myrepo.repo dest=/etc/yum.repos.d/ backup=yes"

验证

ansible localhost -i /etc/ansible/hosts -m shell -a "ls -l /etc/yum.repos.d/|grep myrepo"

3)指定属组和属主

ansible localhost -i /etc/ansible/hosts -m copy -a "src=/root/myrepo.repo dest=/etc/yum.repos.d/ owner=lyh group=lyh backup=yes"

4)权限修改

ansible localhost -i /etc/ansible/hosts -m copy -a "src=/root/myrepo.repo dest=/etc/yum.repos.d/ owner=lyh group=lyh mode=0755 backup=yes"

结束后删除实验文件

ansible localhost -i /etc/ansible/hosts -m shell -a "rm -f /etc/yum.repos.d/myrepo*"

5.yum_repository模块(远程配置yum仓库)

参数:

name			#yum源中[]中的名字
baseurl			#yum源地址,仓库地址
description		#概述,描述,即yum源下的name字段
state:present(确认使用),absent(删除)

举个例子

ansible localhost -i /etc/ansible/hosts -m yum_repository -a "name=nginx-repo baseurl=http://nginx-test description=mytest-nginx-repo state=present"

验证

ansible localhost -i /etc/ansible/hosts -m shell -a "cat /etc/yum.repos.d/nginx-repo.repo"

移除

ansible localhost -i /etc/ansible/hosts -m yum_repository -a "name=nginx-repo baseurl=http://nginx-test description=mytest-nginx-repo state=absent"

再次查看发现仓库已被移除

ansible localhost -i /etc/ansible/hosts -m shell -a "cat /etc/yum.repos.d/nginx-repo.repo"

6.yum模块(远程安装软件)

参数

absent	#卸载
removed	#移除

installed	#安装
latest	#安装最新版
present	#安装
ansible localhost -i /etc/ansible/hosts --module-name yum -a "name=nginx state=present"

7.systemd模块/service模块

这里个人更喜欢使用systemd,service模块大家可以自行搜索了解

参数

daemon_reload:后台重载systemd
enabled:开机自启
name:服务名
state:(started,stopped,restarted,reloaded)

后台重载

ansible localhost -i /etc/ansible/hosts  -m systemd -a "daemon_reload=yes"

设置开机自启

ansible localhost -i /etc/ansible/hosts -m systemd -a "name=sshd enabled=1"
ansible localhost -i /etc/ansible/hosts -m systemd -a "name=nginx enabled=1"

启动

ansible localhost -i /etc/ansible/hosts -m systemd -a "name=nginx state=started"

8.file模块

参数

path:路径
src:源路径(创建链接时使用)
recurse:递归
owner:属主
group:属组
mode:权限
state:(directory,touch,link,hard,absent)
	directory:创建目录
	touch:创建文件
	link:软连接
	hard:硬链接
	absent:删除

创建文件

ansible localhost -i /etc/ansible/hosts -m file -a "path=/root/ name=touchfile state=touch"

创建链接

ansible localhost -i /etc/ansible/hosts -m file -a "src=/root/touchfile path=/usr/local/link state=link"

-src:源文件位置
-path:软链接文件位置

9.cron模块(定时任务)

参数

name:任务描述
minute:分
hour:时
day:天
month:月
weekday:周
user:执行用户
job:操作指令
state:
	present:创建
	absent:删除

每分钟打印一次hello 

ansible localhost -i /etc/ansible/hosts -m cron -a "name="mycron-1min" minute=*/1 hour=* day=* month=* weekday=*  user=lyh job='echo hello' state=present"

删除计划任务

ansible localhost -i /etc/ansible/hosts -m cron -a "name="mycron-1min" minute=*/1 hour=* day=* month=* weekday=*  user=lyh job='echo hello' state=absent"

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值