Ansible有哪些常用模块 详细介绍使用方法!(2)
导读:Ansible有哪些常用模块,[root@bunian ansible-example]# ansible dbsrvs -m shell -a "touch /tmp/a.txt"[WARNING]: Consider using the file module with state=touch rather than running 'touch'. If you need to use command because f
Ansible有哪些常用模块
[root@bunian ansible-example]# ansible dbsrvs -m shell -a "touch /tmp/a.txt"
[WARNING]: Consider using the file module with state=touch rather than running 'touch'. If you need to use command because file is insufficient you can add 'warn: false' to
this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
10.10.108.30 | CHANGED | rc=0 >>
[root@bunian ansible-example]# ansible dbsrvs -m shell -a "ls -al /tmp/ | grep 'a.txt'"
10.10.108.30 | CHANGED | rc=0 >>
-rw-r--r--. 1 root root 0 Aug 9 09:37 a.txt
[root@bunian ansible-example]# ansible dbsrvs -m shell -a "ls -al /tmp/ | grep "a.txt""
10.10.108.30 | CHANGED | rc=0 >>
-rw-r--r--. 1 root root 0 Aug 9 09:37 a.txt
# 会报错,shell万能模块也不支持这种方式
[root@bunian ansible-example]# ansible dbsrvs -m shell -a "cat /etc/passwd |awk -F ':' '{print $1,$3}' >> /tmp/pwd.txt"
10.10.108.30 | FAILED | rc=1 >>
awk: cmd. line:1: {print ,}
awk: cmd. line:1: ^ syntax error
awk: cmd. line:1: {print ,}
awk: cmd. line:1: ^ syntax error
awk: cmd. line:1: {print ,}
awk: cmd. line:1: ^ unexpected newline or end of stringnon-zero return code
注意:你可能会注意到上面出现了WARNING警告。这不是报错,它只是告诉你,应该选择file模块进行创建文件的操作会更好,而不是使用shell模块操作。当然它还告诉你可以在ansible.cfg配置文件中设置command_warnings=False以关闭警告。
3.4 copy 模块
从ansible管理节点拷贝文件到远程主机。
[root@bunian ansible-example]# cat getPasswd.sh
#!/bin/bash
# -*- Author -*- : ayunw
cat /etc/passwd |awk -F ':' '{print $1}'
[root@bunian ansible-example]# ansible dbsrvs -m copy -a "src=getPasswd.sh dest=/usr/local/src/ mode=0755 owner=root group=root"
10.10.108.30 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "ce9c09f15cb6f62b550f819276d06b0e6cd59110",
"dest": "/usr/local/src/getPasswd.sh",
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "/usr/local/src/getPasswd.sh",
"secontext": "system_u:object_r:usr_t:s0",
"size": 54,
"state": "file",
"uid": 0
}
# 默认目标节点存在文件会覆盖,所以最好设置 backup=yes
[root@bunian ansible-example]# ansible dbsrvs -m copy -a "src=getPasswd.sh dest=/usr/local/src/ mode=0755 owner=root group=root backup=yes"
[root@bunian ansible-example]# ansible dbsrvs -m shell -a "ls -al /tmp/ | grep 'getPasswd.sh'"
10.10.108.30 | CHANGED | rc=0 >>
-rw-r--r--. 1 root root 54 Aug 9 09:50 getPasswd.sh
[root@bunian ansible-example]# ansible dbsrvs -m shell -a "cat /tmp/getPasswd.sh"
10.10.108.30 | CHANGED | rc=0 >>
#!/bin/bash
cat /etc/passwd |awk -F ':' '{print $1}'
[root@bunian ansible-example]# ansible dbsrvs -m shell -a "bash /usr/local/src/getPasswd.sh"
10.10.108.30 | CHANGED | rc=0 >>
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp
nobody
systemd-network
dbus
polkitd
sshd
postfix
# 拷贝目录下所有文件到远程,不包括目录本身。文件多了以后,速度会非常慢
[root@bunian ansible-example]# ansible dbsrvs -m copy -a "src=/etc/ansible/ dest=/opt/"
10.10.108.30 | CHANGED => {
"changed": true,
"dest": "/opt/",
"src": "/etc/ansible/"
}
相关阅读
-
谷歌浏览器所有网页打不开 谷歌浏览器打不开网页解决教程
全面的为大家介绍谷歌浏览器打不开网页解决教程方面的讲解,关于谷歌浏览器所有网页打不开 谷歌浏览器打不开网页解决教程,具体介绍如下: 谷歌浏览器使用的人非常的多,不过打不开网
-
关闭defenderwin10杀毒方法 windows defender 防病毒怎么关闭
IT袋网为大家说一说关闭defenderwin10杀毒方法的话题,关于关闭defenderwin10杀毒方法 windows defender 防病毒怎么关闭,一起来看看吧! 用户在更新了win10系统之后有比较多的IT袋朋友都看到了自带的
-
热血无赖win10能玩吗 win10热血无赖无法启动
本文为您带来的是win10热血无赖无法启动的相关话题,关于热血无赖win10能玩吗 win10热血无赖无法启动,请看下面详细的介绍。 很多的用户们在使用我们win10系统去启动热血无赖这款游戏的时候
-
电脑系统错误代码0xc000000e win10修复oxc0000098教程
今天为大家介绍电脑系统错误代码0xc000000e和win10修复oxc0000098教程方面的内容,具体详情如下: 我们在电脑上应用或开机的情况下,会发生提醒windows取决于连接到计算机的机器设备通讯时碰到


