ansible模块大全上【建议收藏】

目录

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

1.command 模块的使用:

​编辑

2.shell模块执行命令 ls /root | grep txt

 3.raw模块执行pwd命令

 4.script模块执行 script.sh文件,

二.file模块:   

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

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

3.创建链接文件

4.删除第一个创建的文件

三.copy模块

1.复制文件

2.复制目录

四.fetch模块   

1.从被控制主机上取文件

五.synchronize模块   

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

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

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

1.command 模块的使用:

去执行一个脚本文件command.sh, command.sh文件的功能是echo "I am command module" 

[root@good ~]# ansible rhce -m command -a "sh command.sh chdir=/root"

2.shell模块执行命令 ls /root | grep txt

让远程主机在shell进程下执行命令,从而支持shell的特性,如管道等。与command模块几乎相同,但在执行命令的时候使用的是/bin/sh。

 

[root@good ~]# ansible rhce -m shell -a "ls /root | grep txt chdir=/root"

 3.raw模块执行pwd命令

[root@good ~]# ansible rhce -m raw -a "pwd"

 4.script模块执行 script.sh文件,

文件的内容为 echo "I am script module"

先把script.sh文件内容链接到远程主机

 

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

 可在远程主机看到已执行

 

二.file模块:
   

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

[root@rhcsa ~]# ansible rhel -m file -a "path=/home/student/test2.txt owner=student group=student mode=600 state=touch"
rhel | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "dest": "/home/student/test2.txt",
    "gid": 2002,
    "group": "student",
    "mode": "0600",
    "owner": "student",
    "secontext": "unconfined_u:object_r:user_home_t:s0",
    "size": 0,
    "state": "file",
    "uid": 2002
}

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

[root@rhcsa ~]# ansible rhel -m file -a "path=/home/student/test_dir owner=student group=student mode=755 state=directory"
rhel | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "gid": 2002,
    "group": "student",
    "mode": "0755",
    "owner": "student",
    "path": "/home/student/test_dir",
    "secontext": "unconfined_u:object_r:user_home_t:s0",
    "size": 6,
    "state": "directory",
    "uid": 2002
}

3.创建链接文件

[root@rhcsa ~]# ansible rhel -m file -a "path=/home/student/test4.txt src=test2.txt state=link"
rhel | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "dest": "/home/student/test4.txt",
    "gid": 0,
    "group": "root",
    "mode": "0777",
    "owner": "root",
    "secontext": "unconfined_u:object_r:user_home_t:s0",
    "size": 9,
    "src": "test2.txt",
    "state": "link",
    "uid": 0
}

4.删除第一个创建的文件

[root@rhcsa ~]# ansible rhel -m file -a "path=/home/student/test2.txt state=absent"
rhel | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "path": "/home/student/test2.txt",
    "state": "absent"
}

三.copy模块

1.复制文件

[root@rhcsa ~]# ansible rhel -m copy -a "src=/root/test.txt dest=/home/student"
rhel | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
    "dest": "/home/student/test.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-1659519729.6693015-3382-265136890969898/source",
    "state": "file",
    "uid": 0
}

2.复制目录

[root@rhcsa test_dir]# ansible rhel -m copy -a "src=/root/test_dir dest=/home/student"
rhel | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
    "dest": "/home/student/test_dir/666.xt",
    "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-1659521079.1086252-4202-160722513414600/source",
    "state": "file",
    "uid": 0
}

四.fetch模块
   

1.从被控制主机上取文件

[root@rhcsa test_dir]# ansible rhel -m fetch -a "dest=/root src=/home/student/test4.txt" 
rhel | CHANGED => {
    "changed": true,
    "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
    "dest": "/root/rhel/home/student/test4.txt",
    "md5sum": "d41d8cd98f00b204e9800998ecf8427e",
    "remote_checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
    "remote_md5sum": null
}

五.synchronize模块
   

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

[root@rhcsa student]# ansible rhel -m synchronize -a "src=/home/student/test2_dir dest=/home/student mode=pull"
rhel | 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@rhel:/home/student/test2_dir /home/student",
    "msg": "cd+++++++++ test2_dir/\n>f+++++++++ test2_dir/d\n>f+++++++++ test2_dir/e\n",
    "rc": 0,
    "stdout_lines": [
        "cd+++++++++ test2_dir/",
        ">f+++++++++ test2_dir/d",
        ">f+++++++++ test2_dir/e"
    ]
}

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

[root@rhcsa ~]# ansible rhel -m synchronize -a "src=/root/test6_dir dest=/home/student mode=push"
rhel | 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/test6_dir student@rhel:/home/student",
    "msg": "cd+++++++++ test6_dir/\n",
    "rc": 0,
    "stdout_lines": [
        "cd+++++++++ test6_dir/"
    ]
}

学习Python运维的路线可以分为以下几个步骤: 1. 入门阶段: 首先,你需要学习Python的基础知识,包括语法、数据类型、函数、模块等等。可以通过参考中提到的各种学习方法和资源来自学或参加培训课程。 2. 系统管理: 掌握Linux系统管理的基本知识是Python运维的基础。你需要了解Linux命令行操作、文件管理、进程管理、用户管理等内容,以及如何使用Python来实现自动化系统管理任务。可以参考相关的学习资料和教程。 3. 自动化运维工具: 掌握一些常用的自动化运维工具是Python运维的重要一步。例如,掌握使用Ansible、SaltStack等工具来进行配置管理、自动化部署和监控等任务。可以参考相关的文档和教程。 4. 网络管理: 网络管理是Python运维的另一个重要方面。你需要了解网络的基本概念和常用的网络协议,以及如何使用Python编写网络管理脚本。可以参考相关的学习资料和实践项目。 5. 容器化技术: 学习容器化技术如Docker和Kubernetes可以提升你的运维能力。了解如何使用Python来管理和部署容器化应用,以及如何进行容器编排和集群管理。可以参考相关的学习资料和实践项目。 6. 监控和故障排查: 运维工作中经常需要处理系统监控和故障排查的任务。学习使用Python编写监控脚本,以及使用Python进行故障排查和日志分析等工作。可以参考相关的学习资料和实践项目。 总之,学习Python运维需要掌握Python的基础知识,并结合系统管理、自动化工具、网络管理、容器化技术、监控和故障排查等方面的知识来应用。参考中提到的应用范围,你可以根据自己的兴趣和需求选择适合自己的进阶方向。但无论选择哪个方向,都需要保持持续学习和实践的态度。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [一文讲清Python的7大学习路线(建议收藏)](https://blog.csdn.net/m0_60571990/article/details/127176246)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值