8.19-ansible中模块的使用+playbook的应用

打印 上一主题 下一主题

主题 517|帖子 517|积分 1551

一、ansible

1.scripts模块

script模块⽤于在长途机器上执⾏本地脚本。
  1. [root@m0 ~]# vim test000.sh
  2. #!/bin/bash
  3. mkdir /tmp/three
  4. touch /tmp/three/test
  5. echo 'i am echo,at mttt' > /tmp/three/test
  6. echo 'well done'
  7. [root@m0 ~]# source test000.sh
  8. well done
  9. [root@m0 ~]# ansible group02 -m script -a './test000.sh'
  10. # 验证
  11. [root@s0 ~]# ls /tmp/
  12. 111                                                                      three
  13. a.txt                                                                    xxx
  14. a.txt.4331.2024-08-16@17:23:26~                                          xxx2
  15. systemd-private-18e460b4dc5b47458e28ad6b292e1a98-chronyd.service-ZPvmft
复制代码
2.用ansible搭建nfs服务

  1. [root@m0 ~]# ansible group02 -m file -a 'path=/static state=directory'
  2. [root@m0 ~]# ansible group02 -m file -a 'path=/static/test state=touch'
  3. [root@m0 ~]# ansible group02 -m command -a 'yum -y install nfs-utils'
  4. [root@s0 ~]# rpm -qa | grep nfs
  5. libnfsidmap-0.25-19.el7.x86_64
  6. nfs-utils-1.3.0-0.68.el7.2.x86_64
  7. [root@m0 ~]# ansible group02 -m yum -a 'name=rpcbind state=latest'
  8. [root@s0 ~]# rpm -qa | grep rpcbind
  9. rpcbind-0.2.0-49.el7.x86_64
  10. [root@m0 ~]# vim /etc/exports
  11. /static *(ro,rsync)
  12. [root@m0 ~]# ansible group02 -m copy -a 'src=/etc/exports dest=/etc/exports'
  13. [root@m0 ~]# ansible group02 -m service -a 'name=rpcbind state=started enabled=yes'
  14. [root@m0 ~]# ansible group02 -m service -a 'name=nfs state=started enabled=yes'
  15. [root@m0 ~]# yum -y install nfs-utils
  16. [root@m0 ~]# mkdir /nfs
  17. [root@m0 ~]# mount -t nfs 192.168.2.112:/static /nfs/
  18. [root@m0 ~]# mount -t nfs 192.168.2.111:/static /nfs/
  19. [root@m0 ~]# mount -t nfs 192.168.2.110:/static /nfs/
  20. mount.nfs: Operation not permitted
  21. [root@m0 ~]# df -h
  22. 文件系统                 容量  已用  可用 已用% 挂载点
  23. /dev/mapper/centos-root   17G  4.3G   13G   26% /
  24. devtmpfs                 476M     0  476M    0% /dev
  25. tmpfs                    488M     0  488M    0% /dev/shm
  26. tmpfs                    488M  7.7M  480M    2% /run
  27. tmpfs                    488M     0  488M    0% /sys/fs/cgroup
  28. /dev/sr0                 8.8G  8.8G     0  100% /mnt
  29. /dev/sda1               1014M  130M  885M   13% /boot
  30. tmpfs                     98M     0   98M    0% /run/user/0
  31. 192.168.2.110:/static     17G  2.1G   15G   13% /nfs
  32. 192.168.2.112:/static     17G  2.1G   15G   13% /nfs
  33. 192.168.2.111:/static     17G  2.1G   15G   13% /nfs
  34. [root@s0 ~]# ls /static/
  35. test
  36. [root@s1 ~]# ls /static/
  37. test
  38. [root@s2 ~]# ls /static/
  39. test
复制代码

二、playbook

playbook剧本是生存在控制机的yml文件
1.Playbook常⻅语法

hosts: ⽤于指定要执⾏使命的主机,其可以是⼀个或多个由冒号分隔主机组。
remote_user: ⽤于指定长途主机上的执⾏使命的⽤户 。
  1. - hosts: group1
  2. remote_user: root
复制代码
tasks: 使命列表, 按顺序执⾏使命.
如果⼀个host执⾏task失败, 整个tasks都会回滚, 修正playbook中的错误, 然后重新执⾏即可
  1. tasks:
  2. - name: ensure apache is at the latest version
  3.    yum: name=httpd,httpd-devel state=latest
  4. - name: write the apache config file
  5.    copy: src=/etc/httpd/conf/httpd.conf
  6.    dest=/etc/httpd/conf/httpd.conf
复制代码
handlers: 类似task,但必要使⽤notify关照调⽤。
不管有多少个关照者进⾏了notify,等到play中的全部task执⾏完 成之后,handlers也只会被执⾏⼀次。
handlers最佳的应⽤场景是⽤来重启服务,或者触发体系重启操纵,除此以外很少⽤到了。
  1. notify:
  2. - restart apache
  3.    - name: ensure apache is running (and enable it at boot)
  4.       service: name=httpd state=started enabled=yes
  5. handlers:
  6.    - name: restart apache
  7.       service: name=httpd state=restarted
复制代码

2.用剧本安装vsftpd

  1. [root@m0 ~]# vim test001.yml
  2. ---
  3. -       hosts: group02
  4.        remote_user:  root
  5.        tasks:
  6.        -       name: 安装vsftpd
  7.                yum: name=vsftpd state=latest
  8.                
  9. [root@m0 ~]# ansible-playbook ./test001.yml
  10. PLAY [group02] *********************************************************************
  11. TASK [Gathering Facts] *************************************************************
  12. ok: [192.168.2.111]
  13. ok: [192.168.2.110]
  14. ok: [other]
  15. TASK [安装vsftpd] ********************************************************************
  16. ok: [other]
  17. ok: [192.168.2.111]
  18. ok: [192.168.2.110]
  19. PLAY RECAP *************************************************************************
  20. 192.168.2.110              : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  
  21. 192.168.2.111              : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  
  22. other                      : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  
复制代码
3.用剧本卸载和安装vsftpd,且启动服务

  1. [root@m0 ~]# vim test001.yml
  2. ---
  3. -       hosts: group02
  4.        remote_user:  root
  5.        tasks:
  6.        -       name: 卸载vsftpd
  7.                yum: name=vsftpd        state=absent
  8.        -       name: 安装vsftpd
  9.                yum: name=vsftpd state=latest
  10.        -       name: 启动服务
  11.                service:        name=vsftpd state=started enabled=yes
  12. [root@m0 ~]# ansible-playbook ./test001.yml
  13. PLAY [group02] *********************************************************************
  14. TASK [Gathering Facts] *************************************************************
  15. ok: [192.168.2.111]
  16. ok: [other]
  17. ok: [192.168.2.110]
  18. TASK [卸载vsftpd] ********************************************************************
  19. changed: [other]
  20. changed: [192.168.2.111]
  21. changed: [192.168.2.110]
  22. TASK [安装vsftpd] ********************************************************************
  23. changed: [other]
  24. changed: [192.168.2.111]
  25. changed: [192.168.2.110]
  26. TASK [启动服务] ************************************************************************
  27. changed: [192.168.2.111]
  28. changed: [192.168.2.110]
  29. changed: [other]
  30. PLAY RECAP *************************************************************************
  31. 192.168.2.110              : ok=4    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  
  32. 192.168.2.111              : ok=4    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  
  33. other                      : ok=4    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  
复制代码
4.用剧本修改vsftpd文件,不允许匿名访问

修改配置文件后,要重新启动服务
  1. # 可以访问到数据
  2. [root@m0 ~]# lftp 192.168.2.110
  3. lftp 192.168.2.110:~> ls
  4. drwxr-xr-x    2 0        0               6 Jun 09  2021 pub
  5. lftp 192.168.2.110:/> exit
  6. [root@m0 ~]# lftp 192.168.2.111
  7. lftp 192.168.2.111:~> ls
  8. drwxr-xr-x    2 0        0               6 Jun 09  2021 pub
  9. lftp 192.168.2.111:/> exit
  10. # 修改配置文件,不允许匿名用户登录
  11. [root@m0 ~]# vim test001.yml
  12. ---
  13. -       hosts: group02
  14.        remote_user:  root
  15.        tasks:
  16.        -       name: 卸载vsftpd
  17.                yum: name=vsftpd        state=absent
  18.        -       name: 安装vsftpd
  19.                yum: name=vsftpd state=latest
  20.        -       name: 启动服务
  21.                service:        name=vsftpd state=started enabled=yes
  22.        -       name: 修改配置文件
  23.                command: sed -i '/^anonymous_enable=YES/s/YES/NO/g' /etc/vsftpd/vsftpd.conf
  24.                notify:
  25.                 -       abcdefg
  26.        handlers:
  27.                -      name: abcdefg
  28.                       service: name=vsftpd state=restarted
  29. [root@m0 ~]# ansible-playbook ./test001.yml
  30. PLAY [group02] *********************************************************************
  31. TASK [Gathering Facts] *************************************************************
  32. ok: [other]
  33. ok: [192.168.2.110]
  34. ok: [192.168.2.111]
  35. TASK [卸载vsftpd] ********************************************************************
  36. changed: [192.168.2.111]
  37. changed: [other]
  38. changed: [192.168.2.110]
  39. TASK [安装vsftpd] ********************************************************************
  40. changed: [192.168.2.111]
  41. changed: [other]
  42. changed: [192.168.2.110]
  43. TASK [启动服务] ************************************************************************
  44. changed: [other]
  45. changed: [192.168.2.111]
  46. changed: [192.168.2.110]
  47. TASK [修改配置文件] **********************************************************************
  48. [WARNING]: Consider using the replace, lineinfile or template module rather than
  49. running 'sed'.  If you need to use command because replace, lineinfile or template
  50. is insufficient you can add 'warn: false' to this command task or set
  51. 'command_warnings=False' in ansible.cfg to get rid of this message.
  52. changed: [192.168.2.111]
  53. changed: [192.168.2.110]
  54. changed: [other]
  55. RUNNING HANDLER [abcdefg] **********************************************************
  56. changed: [192.168.2.111]
  57. changed: [192.168.2.110]
  58. changed: [other]
  59. PLAY RECAP *************************************************************************
  60. 192.168.2.110              : ok=6    changed=5    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  
  61. 192.168.2.111              : ok=6    changed=5    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  
  62. other                      : ok=6    changed=5    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  
  63. # 访问不到数据了
  64. [root@m0 ~]# vim test001.yml
  65. [root@m0 ~]# lftp 192.168.2.111
  66. lftp 192.168.2.111:~> ls
复制代码
5.剧本的格式:

  1. ---
  2. -  hosts:   组名/别名/ip/域名
  3.    remote_user:   root
  4.    tasks:
  5.    - name: 任务说明
  6.    模块: key0=value0
  7.    service: name=vfstpd state=started  enabled=yes
  8.    - name:  修改配置文件
  9.    command: sed ....
  10.    notify:
  11.    -  abcdefg
  12.    
  13.    
  14.    handler:
  15.    - name: abcdefg
  16.    service: name=vfstpd state=restarted
复制代码
6.将httpd的端标语80改为8080

  1. [root@m0 ~]# vim test002.yml
  2. ---
  3. -       hosts:       group01
  4.        remote_user: root
  5.        tasks:
  6.        -       name: 将控制主机的repo文件复制到被控制主机
  7.                copy:  src=/etc/yum.repos.d  dest=/etc/
  8.        -       name: 安装httpd
  9.                yum:  name=httpd  state=present
  10.        -       name: 修改配置文件
  11.                command:  sed -i '/^Listen/s/80/8080/g' /etc/httpd/conf/httpd.conf
  12.        -       name: 修改默认的资源文件
  13.                command: echo 'xxxxxxx' > /var/www/html/index.html
  14.        -       name: 启动httpd服务
  15.                service: name=httpd state=started
  16. [root@m0 ~]# ansible-playbook ./test002.yml
  17. PLAY [group01] *********************************************************************
  18. TASK [Gathering Facts] *************************************************************
  19. ok: [192.168.2.111]
  20. ok: [192.168.2.110]
  21. TASK [将控制主机的repo文件复制到被控制主机] ********************************************************
  22. ok: [192.168.2.110]
  23. ok: [192.168.2.111]
  24. TASK [安装httpd] *********************************************************************
  25. changed: [192.168.2.111]
  26. changed: [192.168.2.110]
  27. TASK [修改配置文件] **********************************************************************
  28. [WARNING]: Consider using the replace, lineinfile or template module rather than
  29. running 'sed'.  If you need to use command because replace, lineinfile or template
  30. is insufficient you can add 'warn: false' to this command task or set
  31. 'command_warnings=False' in ansible.cfg to get rid of this message.
  32. changed: [192.168.2.111]
  33. changed: [192.168.2.110]
  34. TASK [修改默认的资源文件] *******************************************************************
  35. changed: [192.168.2.110]
  36. changed: [192.168.2.111]
  37. TASK [启动httpd服务] *******************************************************************
  38. changed: [192.168.2.110]
  39. changed: [192.168.2.111]
  40. PLAY RECAP *************************************************************************
  41. 192.168.2.110              : ok=6    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  
  42. 192.168.2.111              : ok=6    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  
复制代码
7.使用剧本在不同主机上同时创建不同的文件

  1. [root@m0 ~]# vim /etc/ansible/hosts
  2. s0 ansible_ssh_host=192.168.2.110 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=1
  3. s1 ansible_ssh_host=192.168.2.111 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=1
  4. s2 ansible_ssh_host=192.168.2.112 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=1
  5. [s]
  6. s0
  7. s1
  8. s2
  9. ---
  10. -       hosts: s1
  11.        remote_user: root
  12.        tasks:
  13.        -       name: 创建一个文件
  14.                file: path=/tmp/xxxxxx.txt state=touch
  15. -       hosts: s2
  16.        remote_user: root
  17.        tasks:
  18.        -       name: 也创建一个文件
  19.                file: path=/tmp/yyyyy.txt state=touch
  20. ...
  21. [root@m0 ~]# ansible-playbook ./test003.yml
  22. PLAY [s1] **************************************************************************
  23. TASK [Gathering Facts] *************************************************************
  24. ok: [s1]
  25. TASK [创建一个文件] **********************************************************************
  26. changed: [s1]
  27. PLAY [s2] **************************************************************************
  28. TASK [Gathering Facts] *************************************************************
  29. ok: [s2]
  30. TASK [也创建一个文件] *********************************************************************
  31. changed: [s2]
  32. PLAY RECAP *************************************************************************
  33. s1                         : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  
  34. s2                         : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  
复制代码
8.使用剧本搭建nfs服务

s1为nfs的服务器
s2为nfs的客户端
  1. [root@m0 ~]# vim test004.yml
  2. ---
  3. -       hosts: s1
  4.        remote_user: root
  5.        tasks:
  6.        -       name: 按装nfs-utils
  7.                yum: name=nfs-utils state=present
  8.        -       name: 安装rpcbind
  9.                yum: name=rpcbind  state=present
  10.        -       name: 创建共享目录
  11.                file: path=/static state=directory
  12.        -       name: 配置文件
  13.                shell: echo '/static *(ro,sync)' > /etc/exports
  14.        -       name: 启动服务nfs
  15.                service:  name=nfs state=started enabled=yes
  16.        -       name: 启动服务rpcbind
  17.                service: name=rpcbind state=started  enabled=yes
  18. -       hosts: s2
  19.        remote_user: root
  20.        tasks:
  21.        -       name: 安装nfs-utils
  22.                yum: name=nfs-utils state=latest
  23.        -       name: 创建挂载目录
  24.                file: path=/nfs state=directory
  25.        -       name: 挂载nfs文件
  26.                command:  mount -t nfs 192.168.2.111:/static /nfs
  27. ...
  28. # 验证
  29. [root@s2 ~]# df -h
  30. 文件系统                 容量  已用  可用 已用% 挂载点
  31. /dev/mapper/centos-root   17G  2.1G   15G   13% /
  32. devtmpfs                 476M     0  476M    0% /dev
  33. tmpfs                    488M     0  488M    0% /dev/shm
  34. tmpfs                    488M  7.7M  480M    2% /run
  35. tmpfs                    488M     0  488M    0% /sys/fs/cgroup
  36. /dev/sda1               1014M  130M  885M   13% /boot
  37. /dev/sr0                 8.8G  8.8G     0  100% /mnt
  38. tmpfs                     98M     0   98M    0% /run/user/0
  39. 192.168.2.111:/static     17G  2.1G   15G   13% /nfs
  40. [root@s1 ~]# touch /static/haha
  41. [root@s2 ~]# ls /nfs
  42. haha  test
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

南飓风

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表