当前位置:   article > 正文

ansible模块:command、shell、raw、copy、hostname、yum、service、user、script、unarchive模块语法及使用_ansible unarchive

ansible unarchive

目录

一、command模块

二、shell模块

三、raw模块

四、copy模块

五、hostname模块

六、yum模块

七、service模块

八、user模块

九、script模块

十、unarchive用于在远程主机上解压文件案列


一、command模块

command模块在远程主机执行命令,但是不支持管道,重定向等shell的特征,常用参数如下(不支持管道,不建议使用)。

  1. chdir:在远程主机上运行命令前要提前进入的目录
  2. creates:在命令运行时创建一个文件,如果文件已经存在,则不会创建任务
  3. removes:在命令运行时移除一个文件,如果文件不存在,则不会执行移除任务
  4. executable:指明运行命令的shell程序

案列

  1. [root@ansible ~] ansible Rich -m command -a "useradd Rich"
  2. #Rich是我在/etc/ansible/hosts里定义的主机名 输入IP192.168.1.XX也可以
  3. 192.168.1.134 | CHANGED | rc=0 >>
  4. 192.168.1.133 | CHANGED | rc=0 >>
  5. [root@client1 ~] hostname
  6. client1
  1. [root@ansible ~] ansible Rich -m command -a "uptime"
  2. 192.168.1.134 | CHANGED | rc=0 >>
  3. 16:51:24 up 7:54, 3 users, load average: 0.05, 0.03, 0.05
  4. 192.168.1.133 | CHANGED | rc=0 >>
  5. 16:51:24 up 7:54, 3 users, load average: 0.00, 0.01, 0.05

二、shell模块

shell模块在远程主机执行命令,相当于调用远程主机的shell进程,然后在该shell下打开一个子shell运行命令,和command模块的区别是它支持shell特征,如管道,重定向等。

1、测试重定向

  1. [root@ansible ~] ansible 192.168.1.133 -m shell -a "echo cool >1.txt"
  2. 192.168.1.133 | CHANGED | rc=0 >>
  3. [root@client1 ~]# cat 1.txt
  4. cool

2、测试管道符

三、raw模块

最原始的方式运行命令(不依赖python,仅通过ssh实现)

案列:清除yum缓存

  1. [root@ansible ~] ansible 192.168.1.134 -m raw -a "yum clean all"
  2. 192.168.1.134 | CHANGED | rc=0 >>
  3. 已加载插件:fastestmirror
  4. 正在清理软件源: c7-media epel
  5. Cleaning up list of fastest mirrors
  6. Other repos take up 180 M of disk space (use --verbose for details)
  7. Shared connection to 192.168.1.134 closed.

四、copy模块

copy模块用于复制指定主机文件到远程主机的指定位置,常见参数如下

  1. dest:指出复制文件的目标目录位置,使用绝对路径。如果源是目录,指目标也要是目录,如果目标文件已经存在会覆盖原有内容。
  2. src:指出源文件的路径,可以使用相对路径或绝对路径,支持直接指定目录,如果源是目录则目标也要是目录
  3. mode:指出复制时,目标文件的权限 可选
  4. owner:指出复制时,目标文件的属主 可选
  5. group:指出复制时,目标文件的属组 可选
  6. content:指出复制到目标主机上的内容,不能与src一起使用,相当于复制content指明的数据到目标文件中

特别提示:

参数:backup=yes===>意思是,如果目标路径下,有与我同名但不同内容的文件时,在覆盖前,对目标文件先进行备份。

所有被管理端节点必须安装libselinux-python

实验案例

将Rich组中主机的/etc/hosts文件拷贝到/tmp下 指定权限为777 更改属主为Rich更改属组为root

  1. [root@ansible ~] ansible Rich -m copy -a "src=/etc/hosts dest=/tmp mode=777 owner=Rich group=root"
  2. 192.168.1.134 | CHANGED => {
  3. "ansible_facts": {
  4. "discovered_interpreter_python": "/usr/bin/python"
  5. },
  6. "changed": true,
  7. "checksum": "7335999eb54c15c67566186bdfc46f64e0d5a1aa",
  8. "dest": "/tmp/hosts",
  9. "gid": 0,
  10. "group": "root",
  11. "md5sum": "54fb6627dbaa37721048e4549db3224d",
  12. "mode": "0777",
  13. "owner": "Rich",
  14. "secontext": "unconfined_u:object_r:admin_home_t:s0",
  15. "size": 158,
  16. "src": "/root/.ansible/tmp/ansible-tmp-1689330661.87-9796-230501467348237/source",
  17. "state": "file",
  18. "uid": 1000
  19. [root@client1 ~] ls /tmp/
  20. hosts systemd-private-0eee05b8fb594c28b1d829918eb10657-chronyd.service-LWLJ47

五、hostname模块

hostname模块用于管理远程主机上的主机名,常用参数如下

name:指明主机名

案列:更改client主机名

  1. [root@ansible ~] ansible 192.168.1.133 -m hostname -a "name=Rich"
  2. 192.168.1.133 | CHANGED => {
  3. "ansible_facts": {
  4. "ansible_domain": "",
  5. "ansible_fqdn": "Rich",
  6. "ansible_hostname": "Rich",
  7. "ansible_nodename": "Rich",
  8. "discovered_interpreter_python": "/usr/bin/python"
  9. },
  10. "changed": true,
  11. "name": "Rich"
  12. }
  13. #client上查看
  14. [root@client1 ~] hostname
  15. Rich

六、yum模块

Yum模块基于yum机制,对远程主机管理程序包,常用参数如下。

  1. name:程序包的名称,可以带上版本号,如不指定版本号默认安装为最新版本
  2. state=present | latest | absent:指明对程序包执行的操作,present表示安装程序包,latest表示安装最新版本的程序包,absent表示卸载程序包
  3. disablerepo:在用yum安装时禁用某个仓库的ID
  4. enablerepo:在用yum安装时启用某个参考的ID
  5. conf_file:yum运行时的配置文件而不是使用默认的配置文件
  6. diable_gpg_check=yes | no:是否启用完整性校验功能

 案例:client端yum安装bind

  1. [root@ansible ~] ansible Rich -m copy -a "src=/etc/hosts dest=/tmp mode=777 owner=Rich group=root"
  2. 192.168.1.134 | CHANGED => {
  3. "ansible_facts": {
  4. "discovered_interpreter_python": "/usr/bin/python"
  5. },
  6. "changed": true,
  7. "checksum": "7335999eb54c15c67566186bdfc46f64e0d5a1aa",
  8. "dest": "/tmp/hosts",
  9. "gid": 0,
  10. "group": "root",
  11. "md5sum": "54fb6627dbaa37721048e4549db3224d",
  12. "mode": "0777",
  13. "owner": "Rich",
  14. "secontext": "unconfined_u:object_r:admin_home_t:s0",
  15. "size": 158,
  16. "src": "/root/.ansible/tmp/ansible-tmp-1689330661.87-9796-230501467348237/source",
  17. "state": "file",
  18. "uid": 1000
  19. }
  20. [root@client1 ~] rpm -qa bind
  21. bind-9.11.4-26.P2.el7_9.13.x86_64

 

七、service模块

Service模块为用来管理远程主机上的服务的模块,常见参数如下:

  1. name:被管理的服务名称
  2. state=started | stopped | restarted:动作包含启动关机或重启
  3. enabled=yes | no:表示是否设置该服务开机自启动

runlevel:如果设定了enabled开机自启动,则要定义在哪些运行目标下自启动

案例:启动httpd服务并设置为开启自启动

 

  1. [root@ansible ~] ansible 192.168.1.133 -m service -a "name=httpd state=started enabled=yes"
  2. 192.168.1.133 | CHANGED => {
  3. "ansible_facts": {
  4. "discovered_interpreter_python": "/usr/bin/python"
  5. },
  6. "changed": true,
  7. [root@client1 ~] netstat -anptl |grep :80
  8. tcp6 0 0 :::80 :::* LISTEN 11163/httpd

八、user模块

User模块用于管理远程主机上的用户账户,常见参数如下:

  1. name:必选参数  账号名称
  2. state=present | absent:创建账号或者删除账号,present表示创建,absent表示删除
  3. system=yes | no:是否为系统账号
  4. uid:用户UID
  5. group:用户的基本组
  6. groups:用户的附加组
  7. shell:默认使用的shell
  8. home:用户的家目录
  9. move_home=yes | no:如果设置的家目录已经存在,是否将已经存在的家目录进行移动
  10. password:用户的密码,建议使用加密后的字符串
  11. comment:用户的注释信息
  12. remove=yes | no:当state=absent时,是否删除用户的家目录

案列:给client1(192.168.1.133)创建用户 

  1. [root@ansible ~] ansible 192.168.1.133 -m user -a 'name=user1 system=yes uid=502 group=root groups=sshd shell=/sbin/nologin home=/home/user1 password=user1 comment="test user"'
  2. [WARNING]: The input password appears not to have been hashed. The 'password' argument must
  3. be encrypted for this module to work properly.
  4. 192.168.1.133 | CHANGED => {
  5. "ansible_facts": {
  6. "discovered_interpreter_python": "/usr/bin/python"
  7. },
  8. "changed": true,
  9. "comment": "test user",
  10. "create_home": true,
  11. [root@client1 ~] tail /etc/passwd
  12. user1:x:502:0:test user:/home/user1:/sbin/nologin

删除用户及家目录

  1. [root@ansible ~] ansible 192.168.1.133 -m user -a 'name=user1 state=absent remove=yes'
  2. 192.168.1.133 | CHANGED => {
  3. "ansible_facts": {
  4. "discovered_interpreter_python": "/usr/bin/python"
  5. },
  6. "changed": true,
  7. "force": false,

九、script模块

script模块能够实现远程服务器批量运行本地的shell脚本

所有被管理端需要挂载光盘,并创建本地yum仓库文件

 

  1. [root@ansible ~] vim test.sh
  2. #!/bin/bash
  3. touch /tmp/file{1..20}.txt
  4. [root@ansible ~] ansible 192.168.1.133 -m script -a "test.sh"
  5. 192.168.1.133 | CHANGED => {
  6. "changed": true,
  7. "rc": 0,
  8. "stderr": "Shared connection to 192.168.1.133 closed.\r\n",
  9. "stderr_lines": [
  10. "Shared connection to 192.168.1.133 closed."
  11. ],
  12. "stdout": "",
  13. "stdout_lines": []
  14. [root@client1 ~] ls /tmp
  15. file10.txt file2.txt
  16. file11.txt file3.txt
  17. file12.txt file4.txt
  18. file13.txt file5.txt
  19. file14.txt file6.txt
  20. file15.txt file7.txt
  21. file16.txt file8.txt
  22. file17.txt file9.txt
  23. file18.txt hosts
  24. file19.txt systemd-private-0eee05b8fb594c28b1d829918eb10657-chronyd.service-LWLJ47
  25. file1.txt systemd-private-0eee05b8fb594c28b1d829918eb10657-httpd.service-dqgrrv
  26. file20.txt

十、unarchive用于在远程主机上解压文件
案列

  1.  [root@ansible ~] ansible Rich -m unarchive -a "src=/root/0.4.0.tar.gz dest=/mnt/"
  2. 192.168.1.134 | CHANGED => {
  3.     "ansible_facts": {
  4.         "discovered_interpreter_python": "/usr/bin/python"
  5.     }, 
  6.     "changed": true
  7.     "dest": "/mnt/"
  8.     "extract_results": {
  9. [root@client1 ~] ls /mnt
  10. hgfs  jpress-0.4.0
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/运维做开发/article/detail/837413
推荐阅读
相关标签
  

闽ICP备14008679号