IT袋

当前位置:主页 > 经验教程 > 系统教程 >

Ansible有哪些常用模块

Ansible有哪些常用模块 详细介绍使用方法!(2)

时间:2023-09-17 20:35:04 来源:IT袋 作者:苏晓敏
导读: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/"
}

相关阅读