ansible的常见模块用法

旧城等待, 2022-01-17 18:57 462阅读 0赞

配置文件详解:

1,主配置文件:/etc/asiable/ansiable.cfg

  1. module_name =command ##ansible的默认模块是command模块,但是在使用的时候非常的有局限性,建议改成shell模块
  2. host_key_checking = False ##检查对应要控制主机的的host_key,建议取消注释,以减轻管理时需要输入的密码
  3. log_path = /var/log/ansible.log ##ansible的登录日志文件所在的位置
  4. executable = /bin/sh ##默认登录到对方用户下面使用的shell版本

2,被管理主机的配置文件:/etc/ansible/hosts

  1. green.example.com ##定义单个被管理的主机,可以是FQDN,也可以是IP地址
  2. [webservers] ##把被管理的主机放在一个组中
  3. alpha.example.org
  4. www[001:006].example.com ##支持类似通配符写法,此项代表从www001.ex ample.com到www006.ex ample.com
  5. 之间的所有主机

ansible的使用用法:

前提:

  1. 由于ansible默认是基于ssh服务来管理主机的,所以首先要在管理的主机上生成公钥文件,并传递给要管理的主机
  2. 之上,才能实现基于密钥的管理

1,在管理者的主机上生成公钥文件

  1. [root@localhost ~] ssh-keygen -t rsa ##生成对称密钥,出现提示选择默认即可
  2. Generating public/private rsa key pair.
  3. Enter file in which to save the key (/root/.ssh/id_rsa):
  4. Enter passphrase (empty for no passphrase):
  5. Enter same passphrase again:
  6. Your identification has been saved in /root/.ssh/id_rsa.
  7. Your public key has been saved in /root/.ssh/id_rsa.pub.
  8. The key fingerprint is:
  9. SHA256:06qoPmoSy7UGkKie95RnHn6bPOFEnusk/B0m+/+g8C0 root@localhost.localdomain
  10. The key's randomart image is:
  11. +---[RSA 2048]----+
  12. | |
  13. | |
  14. |.. |
  15. |+ o |
  16. |o S o |
  17. |o. . o B |
  18. |oo+ .o *++oo . |
  19. |o=.+..=.*=OE+ . |
  20. |+o=oo..ooB+=oo.. |
  21. +----[SHA256]-----+

2,把公钥传递给被管理的主机上

  1. [root@localhost ~] ssh-copy-id -i 192.168.1.20 ##传递到远程的主机上进行管理
  2. /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
  3. The authenticity of host '192.168.1.20 (192.168.1.20)' can't be established.
  4. ECDSA key fingerprint is SHA256:htIQABZZdudyHVZbppjWeY2d/pQQ0km8k+i/39SZ04Q.
  5. ECDSA key fingerprint is MD5:78:6e:b3:3d:fc:29:b2:b0:fc:2f:6d:d6:ff:3c:63:1a.
  6. Are you sure you want to continue connecting (yes/no)? yes
  7. /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
  8. /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
  9. root@192.168.1.20's password:
  10. Number of key(s) added: 1
  11. Now try logging into the machine, with: "ssh '192.168.1.20'"
  12. and check to make sure that only the key(s) you wanted were added.

3,把被管理的主机加入到/etc/ansible/hosts文件中

  1. [web] ##给被管理的主机进行分组
  2. 192.168.1.19
  3. 192.168.1.20
  4. [db]
  5. 192.168.1.21

基于模块的使用方法:

1,ping模块:查看被管理主机的模块是否处于在线状态、

  1. [root@localhost ~] ansible db -m ping ##查看db组中被管理的主机是否在线
  2. 192.168.1.21 | SUCCESS => {
  3. "changed": false,
  4. "ping": "pong"
  5. }
  6. [root@localhost ~] ansible all -m ping ##all代表所有被管理的主机
  7. 192.168.1.21 | SUCCESS => {
  8. "changed": false,
  9. "ping": "pong" ##如果处于在线状态,会放回一个pong的提示
  10. }
  11. 192.168.1.19 | SUCCESS => {
  12. "changed": false,
  13. "ping": "pong"
  14. }
  15. 192.168.1.20 | SUCCESS => {
  16. "changed": false,
  17. "ping": "pong"
  18. }

2,user模块:在远程主机上创建用户

  1. [root@localhost ~] ansible db -m user -a 'name=mysql state=present' ##present表示建立,创建一个用户名为mysql
  2. 的用户
  3. 192.168.1.21 | CHANGED => {
  4. "changed": true,
  5. "comment": "",
  6. "create_home": true,
  7. "group": 1000,
  8. "home": "/home/mysql",
  9. "name": "mysql",
  10. "shell": "/bin/bash",
  11. "state": "present",
  12. "system": false,
  13. "uid": 1000
  14. }
  15. [root@localhost ~] ansible db -m user -a 'name=mariadb state=present system=yes' ##创建一个用户名为mariadb的
  16. 系统用户
  17. 192.168.1.21 | CHANGED => {
  18. "changed": true,
  19. "comment": "",
  20. "create_home": true,
  21. "group": 994,
  22. "home": "/home/mariadb",
  23. "name": "mariadb",
  24. "shell": "/bin/bash",
  25. "state": "present",
  26. "system": true,
  27. "uid": 997
  28. }
  29. [root@localhost ~] ansible db -m user -a 'name=mysql state=absent' ##absent代表移除,删除用户名为mysql的用户
  30. 192.168.1.21 | CHANGED => {
  31. "changed": true,
  32. "force": false,
  33. "name": "mysql",
  34. "remove": false,
  35. "state": "absent"
  36. }

3,group模块:在远程主机上创建用户组

  1. [root@localhost ~] ansible db -m group -a 'name=tomcat state=present' ##创建组和创建用户的方法差不多,只是用
  2. 的模块上有些差异,此命令为创建一个普通的用户组
  3. 192.168.1.21 | CHANGED => {
  4. "changed": true,
  5. "gid": 1000,
  6. "name": "tomcat",
  7. "state": "present",
  8. "system": false
  9. }
  10. [root@localhost ~] ansible db -m group -a 'name=tomcat state=absent' ##移除用户组
  11. 192.168.1.21 | CHANGED => {
  12. "changed": true,
  13. "name": "tomcat",
  14. "state": "absent"
  15. }

4,copy模块:拷贝文件到远程主机

  1. [root@localhost ~] ansible db -m copy -a 'src=/root/test dest=/root/' ##拷贝一个test文件到对方主机的root目录下,src
  2. 指定源文件,dest指定目标文件的存放目录
  3. 192.168.1.21 | CHANGED => {
  4. "changed": true,
  5. "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
  6. "dest": "/root/test",
  7. "gid": 0,
  8. "group": "root",
  9. "md5sum": "d41d8cd98f00b204e9800998ecf8427e",
  10. "mode": "0644",
  11. "owner": "root",
  12. "size": 0,
  13. "src": "/root/.ansible/tmp/ansible-tmp-1556108167.92-277769296604040/source",
  14. "state": "file",
  15. "uid": 0
  16. }

5,yum模块:在远程主机上安装软件(需要在远程主机上安装好yum源,才能够安装软件)

  1. [root@localhost ~] ansible db -m yum -a "name=vsftpd" ##安装vsftpd
  2. 192.168.1.21 | CHANGED => {
  3. "ansible_facts": {
  4. "pkg_mgr": "yum"
  5. },
  6. "changed": true,
  7. "msg": "Repository 'cdrom' is missing name in configuration, using id\n",
  8. "rc": 0, ##rc返回值为0代表执行成功
  9. ......
  10. [root@localhost ~] ansible db -m yum -a 'name=vsftpd state=absent' ##删除已安装的软件包
  11. 192.168.1.21 | CHANGED => {
  12. "ansible_facts": {
  13. "pkg_mgr": "yum"
  14. },
  15. "changed": true,
  16. "msg": "Repository 'cdrom' is missing name in configuration, using id\n",
  17. "rc": 0,
  18. "results": [
  19. ......

6,shell模块:可以在远程主机上执行shell命令

  1. [root@localhost ~] ansible db -m shell -a 'hostname' ##在远程主机上执行hostname命令
  2. 192.168.1.21 | CHANGED | rc=0 >>
  3. localhost.localdomain

7,script模块:在远程主机上执行shell脚本,不用把脚本传递到远程主机上即可执行

编写一个test脚本

  1. [root@localhost ~] vim test.sh
  2. #!/bin/bash
  3. wall hello word

不用给创建的脚本执行权限,就可以使远程主机执行脚本

  1. [root@localhost ~] ansible db -m script -a /root/test.sh ##让远程主机执行脚本
  2. 192.168.1.21 | CHANGED => {
  3. "changed": true,
  4. "rc": 0,
  5. "stderr": "Shared connection to 192.168.1.21 closed.\r\n",
  6. "stderr_lines": [
  7. "Shared connection to 192.168.1.21 closed."
  8. ],
  9. "stdout": "",
  10. "stdout_lines": []
  11. }

8,File:设置文件属性

  1. [root@localhost ~] ansible db -m file -a 'path=/root/test owner=mariadb mode=700' ##给远程主机的文件设置属主,
  2. 和权限
  3. 192.168.1.21 | CHANGED => {
  4. "changed": true,
  5. "gid": 0,
  6. "group": "root",
  7. "mode": "0700",
  8. "owner": "mariadb",
  9. "path": "/root/test",
  10. "size": 0,
  11. "state": "file",
  12. "uid": 997
  13. }
  14. [root@localhost ~] ansible db -m file -a 'src=/root/test dest=/root/test-link state=link'
  15. 192.168.1.21 | CHANGED => { ##给文件创建软链接,当然也可以创建名为test-link硬链接,需要把link改成hard
  16. "changed": true,
  17. "dest": "/root/test-link",
  18. "gid": 0,
  19. "group": "root",
  20. "mode": "0777",
  21. "owner": "root",
  22. "size": 10,
  23. "src": "/root/test",
  24. "state": "link",
  25. "uid": 0
  26. }

9,Cron:计划任务

  1. [root@localhost ~] ansible db -m shell -a 'rpm -qa | grep crontabs' ##查看被管理的主机是否安装crontabs软件
  2. [root@localhost ~] ansible db -m shell -a 'systemctl status crond' ##查看计划任务服务是否启动
  3. [root@localhost ~] ansible db -m cron -a 'minute=*/5 job="/usr/bin/wall hello word"' ##设置计划任务,每五分钟执行一
  4. hello word,还可以指定小时,天,月,星期,如果没指定,默认是*

在对方主机上执行查看是否有计划任务

  1. [root@localhost ~] crontab -l
  2. #Ansible: None
  3. */5 * * * * /usr/bin/wall hello word

10,service模块

  1. [root@localhost ~] ansible db -m service -a 'name=httpd state=started' #安装http服务
  2. 192.168.1.21 | CHANGED => {
  3. "changed": true,
  4. "name": "httpd",
  5. "state": "started",
  6. "status": {
  7. "ActiveEnterTimestampMonotonic": "0",
  8. "ActiveExitTimestampMonotonic": "0",
  9. ......
  10. [root@localhost ~] ansible db -a 'systemctl status httpd' #查看http服务是否启动
  11. 192.168.1.21 | CHANGED | rc=0 >>
  12. httpd.service - The Apache HTTP Server
  13. Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
  14. Active: active (running) since Wed 2019-04-24 21:54:56 EDT; 42s ago
  15. ......
  16. [root@localhost ~] ansible db -m service -a 'name=httpd state=stopped' #停止http服务
  17. 192.168.1.21 | CHANGED => {
  18. "changed": true,
  19. "name": "httpd",
  20. "state": "stopped",
  21. "status": {
  22. ......

转载于:https://blog.51cto.com/14163901/2384320

发表评论

表情:
评论列表 (有 0 条评论,462人围观)

还没有评论,来说两句吧...

相关阅读