Ansible的三个命令模块和部分文件操模块

目录

1.command,shell,raw,script模块的作用和区别

2.file模块

3.copy模块

4.fetch模块

5.synchronize模块


1.command,shell,raw,script模块的作用和区别

(1)command 模块的使用: 去执行一个脚本文件command.sh, command.sh文件的功能是echo "I am command module" 

受控主机上创建文件
[root@client_1 ~]# vim command.sh
echo "I am command module"

[root@client_1 ~]# chmod +x command.sh 

[root@client_1 ~]# ll command.sh 
-rwxr-xr-x. 1 root root 27 Aug  4 22:41 command.sh
控制主机操作
[root@server ~]# ansible client_1 -m command -a "sh /root/command.sh chdir=/root"
client_1 | CHANGED | rc=0 >>
I am command module

(2)shell模块执行命令 ls /root | grep cfg

[root@server ~]# ansible client_1 -m shell -a "ls /root | grep cfg"
client_1 | CHANGED | rc=0 >>
anaconda-ks.cfg
initial-setup-ks.cfg

(3)raw模块执行pwd命令

[root@server ~]# ansible client_1 -m raw -a "pwd"
client_1 | CHANGED | rc=0 >>
/home/student
Shared connection to client_1 closed.

(4)script模块执行 script.sh文件,文件的内容为 echo "I am script module"

控制主机创建文件
[root@server ~]# vim script.sh
echo "I am script module"

[root@server ~]# chmod +x script.sh 

[root@server ~]# ll script.sh 
-rwxr-xr-x 1 root root 26 Aug  4 23:16 script.sh

[root@server ~]# ansible client_1 -m script -a "script.sh chdir=/root"
client_1 | CHANGED => {
    "changed": true,
    "rc": 0,
    "stderr": "Shared connection to client_1 closed.\r\n",
    "stderr_lines": [
        "Shared connection to client_1 closed."
    ],
    "stdout": "I am script module\r\n",
    "stdout_lines": [
        "I am script module"
    ]
}

2.file模块

(1)创建文件,并指定用户,用户组为student, 且权限为600

[root@server ~]# ansible client_1 -m file -a "path=/home/student/file_test state=touch owner=student group=student mode=600"
client_1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "dest": "/home/student/file_test",
    "gid": 1112,
    "group": "student",
    "mode": "0600",
    "owner": "student",
    "secontext": "unconfined_u:object_r:user_home_t:s0",
    "size": 0,
    "state": "file",
    "uid": 1112
}
[student@client_1 ~]$ ll
total 0
-rw-------. 1 student student 0 Aug  5 00:32 file_test

(2)创建目录,并指定用户,用户组为student,   且权限为755

[root@server ~]# ansible client_1 -m file -a "path=/home/student/dir_test state=directory owner=student group=student mode=755"
client_1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "dest": "/home/student/dir_test",
    "gid": 1112,
    "group": "student",
    "mode": "0755",
    "owner": "student",
    "secontext": "unconfined_u:object_r:user_home_t:s0",
    "size": 0,
    "state": "file",
    "uid": 1112
}
[student@client_1 ~]$ ll
total 0
drwxr-xr-x. 2 student student 6 Aug  5 00:35 dir_test
-rw-------. 1 student student 0 Aug  5 00:32 file_test

(3)创建链接文件

[root@server ~]# ansible client_1 -m file -a "path=/home/student/linkfile state=link src=/home/student/file_test"
client_1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "dest": "/home/student/linkfile",
    "gid": 0,
    "group": "root",
    "mode": "0777",
    "owner": "root",
    "secontext": "unconfined_u:object_r:user_home_t:s0",
    "size": 23,
    "src": "/home/student/file_test",
    "state": "link",
    "uid": 0
}
[student@client_1 ~]$ ll
total 0
drwxr-xr-x. 2 student student  6 Aug  5 00:35 dir_test
-rw-------. 1 student student  0 Aug  5 00:32 file_test
lrwxrwxrwx. 1 root    root    23 Aug  5 00:39 linkfile -> /home/student/file_test

(4)删除第一个创建的文件

[root@server ~]# ansible client_1 -m file -a "path=/home/student/file_test state=absent"
client_1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "path": "/home/student/file_test",
    "state": "absent"
}
[student@client_1 ~]$ ll
total 0
drwxr-xr-x. 2 student student  6 Aug  5 00:35 dir_test
lrwxrwxrwx. 1 root    root    23 Aug  5 00:39 linkfile -> /home/student/file_test

3.copy模块

从控制主机到受控主机

(1)复制文件

[root@server ~]# ansible client_1 -m copy -a "src=/root/test1.txt dest=/home/student"
client_1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
    "dest": "/home/student/test1.txt",
    "gid": 0,
    "group": "root",
    "md5sum": "d41d8cd98f00b204e9800998ecf8427e",
    "mode": "0644",
    "owner": "root",
    "secontext": "unconfined_u:object_r:user_home_t:s0",
    "size": 0,
    "src": "/home/student/.ansible/tmp/ansible-tmp-1659631512.7965088-8284-63540058956816/source",
    "state": "file",
    "uid": 0
}
[student@client_1 ~]$ ll
total 0
drwxr-xr-x. 2 student student  6 Aug  5 00:35 dir_test
lrwxrwxrwx. 1 root    root    23 Aug  5 00:39 linkfile -> /home/student/file_test
-rw-r--r--. 1 root    root     0 Aug  5 00:45 test1.txt

(2)复制目录

[root@server ~]# ansible client_1 -m copy -a "src=/root/tar_test dest=/home/student"
client_1 | CHANGED => {
    "changed": true,
    "dest": "/home/student/",
    "src": "/root/tar_test"
}
[student@client_1 ~]$ ll
total 0
drwxr-xr-x. 3 student student 20 Aug  5 00:47 dir_test
lrwxrwxrwx. 1 root    root    23 Aug  5 00:39 linkfile -> /home/student/file_test
drwxr-xr-x. 2 root    root    45 Aug  5 00:51 tar_test
-rw-r--r--. 1 root    root     0 Aug  5 00:45 test1.txt
[student@client_1 ~]$ ll tar_test/
total 4
-rw-r--r--. 1 root root 30 Aug  5 00:51 afile
-rw-r--r--. 1 root root  0 Aug  5 00:51 bfile
-rw-r--r--. 1 root root  0 Aug  5 00:51 zfile
[student@client_1 ~]$ 

4.fetch模块

从被控制主机上取文件

[root@server ~]# ansible client_1 -m fetch -a "src=/home/student/file_fetch dest=/root" 
client_1 | CHANGED => {
    "changed": true,
    "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
    "dest": "/root/client_1/home/student/file_fetch",
    "md5sum": "d41d8cd98f00b204e9800998ecf8427e",
    "remote_checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
    "remote_md5sum": null
}

5.synchronize模块

(1)pull: 从被控制主机上拉取目录

[root@server ~]# ansible client_1 -m synchronize -a "src=/home/student/synchronize_test dest=/root mode=pull"
client_1 | CHANGED => {
    "changed": true,
    "cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh='/usr/bin/ssh -S none -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null' --rsync-path='sudo -u root rsync' --out-format='<<CHANGED>>%i %n%L' student@client_1:/home/student/synchronize_test /root",
    "msg": "cd+++++++++ synchronize_test/\ncd+++++++++ synchronize_test/synchronize/\n",
    "rc": 0,
    "stdout_lines": [
        "cd+++++++++ synchronize_test/",
        "cd+++++++++ synchronize_test/synchronize/"
    ]
}
[root@server ~]# ll -d synchronize_test
drwxrwxr-x 3 1112 1112 25 Aug  5 01:00 synchronize_test

(2)push:往被控制主机上推送目录

[root@server ~]# ansible client_1 -m synchronize -a "src=/root/my_key_cert dest=/home/student/"
client_1 | CHANGED => {
    "changed": true,
    "cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh='/usr/bin/ssh -S none -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null' --rsync-path='sudo -u root rsync' --out-format='<<CHANGED>>%i %n%L' /root/my_key_cert student@client_1:/home/student/",
    "msg": "cd+++++++++ my_key_cert/\n<f+++++++++ my_key_cert/cert.crt\n<f+++++++++ my_key_cert/rsa_private.key\n",
    "rc": 0,
    "stdout_lines": [
        "cd+++++++++ my_key_cert/",
        "<f+++++++++ my_key_cert/cert.crt",
        "<f+++++++++ my_key_cert/rsa_private.key"
    ]
}
[student@client_1 ~]$ ll -d my_key_cert
drwxr-xr-x. 2 root root 45 Jul 30 23:52 my_key_cert
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

俗人不俗鸭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值