78、 ansible----playbook

熊熊出没  论坛元老 | 2024-8-27 02:55:35 | 来自手机 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 1020|帖子 1020|积分 3060

一、ansible模块

11、防火墙和网络模块:

  1. [root@test1 ~]# ansible 192.168.168.23 -m iptables -a 'chain=INPUT protocol=ICMP source=192.168.168.22 jump=REJECT' -b  ##-b后台,拒绝
复制代码

  1. [root@test3 ~]# yum -y install nginx
  2. [root@test3 ~]# systemctl restart nginx.service
  3. [root@test3 ~]# curl 192.168.168.23
  4. this is nginx!
  5. [root@test1 ~]# ansible 192.168.168.23 -m iptables -a 'chain=INPUT protocol=tcp destination_port=80 jump=REJECT' -b  ##80端口关闭,拒绝访问
复制代码
删除防火墙战略

  1. [root@test1 ~]# ansible 192.168.168.23 -m iptables -a 'chain=INPUT protocol=tcp destination_port=80 jump=REJECT state=absent' -b
复制代码

  1. [root@test1 ~]# ansible 192.168.168.23 -m iptables -a 'chain=INPUT protocol=tcp destination_port=80 jump=ACCEPT' -b  ##放开80端口
复制代码
firewalld模块

  1. [root@test3 ~]# systemctl start firewalld
  2. [root@test1 ~]# ansible 192.168.168.23 -m firewalld -a 'service=nginx zone=public permanent=true state=enabled immediate=true' -e
  3. [root@test3 ~]# firewall-cmd --get-services | grep nginx##查询不到nginx服务,不支持
  4. [root@test3 ~]# firewall-cmd --get-services | grep http##查询http服务,支持
  5. #### service不支持nginx,启动端口80/tcp
  6. [root@test1 ~]# ansible 192.168.168.23 -m firewalld -a 'port=80/tcp zone=public permanent=true state=enabled immediate=true' -b
复制代码

删除端口,防火墙战略

  1. [root@test1 ~]# ansible 192.168.168.23 -m firewalld -a 'port=80/tcp zone=public permanent=true state=disabled immediate=true' -b
复制代码

12、配置网卡

修改ip地址

  1. [root@test1 ~]# ansible 192.168.168.22 -m ansible.builtin.lineinfile -a "path=/etc/sysconfig/network-scripts/ifcfg-ens33 regexp='^IPADDR' line='IPADDR=192.168.168.25'"
复制代码
查看有没有改成功

  1. [root@test2 ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
  2. TYPE=Ethernet
  3. DEVICE=ens33
  4. ONBOOT=yes
  5. BOOTPROTO=static
  6. IPADDR=192.168.168.25  ##修改成功
  7. NETMASK=255.255.255.0
  8. GATEWAY=192.168.168.2
  9. DNS1=218.2.135.1
  10. DNS2=221.131.143.69
复制代码
重启网卡

  1. [root@test1 ~]# ansible 192.168.168.22 -a 'systemctl restart network'
复制代码
回终端查看


ip地址改回为192.168.168.22

  1. [root@test1 ~]# cd /etc/ansible/
  2. [root@test1 ansible]# ls
  3. ansible.cfg  hosts  roles  test2.sh
  4. [root@test1 ansible]# vim hosts
  5. 192.168.168.25
  6. [root@test1 ansible]# ansible 192.168.168.25 -m ansible.builtin.lineinfile -a "path=/etc/sysconfig/network-scripts/ifcfg-ens33 regexp='^IPADDR' line='IPADDR=192.168.168.22'"
  7. [root@test1 ansible]# ansible 192.168.168.25 -a 'systemctl restart network'
  8. 点击xshell,重新连接
  9. ifconfig
复制代码

13、script模块:

运行的本地的脚本,把脚本运行的效果输出到目标主机。
  1. [root@test1 ansible]# cd /opt/
  2. [root@test1 opt]# vim test.sh
  3. [root@test1 opt]# chmod 777 test.sh
  4. [root@test1 opt]# ansible 192.168.168.22 -m script -a '/opt/test.sh'[root@test1 ansible]# cd /opt/
  5. [root@test1 opt]# vim test.sh
  6. #!/bin/bash
  7. echo "hello world!" > /opt/test2.txt
  8. [root@test1 opt]# chmod 777 test.sh
  9. [root@test1 opt]# ansible 192.168.168.22 -m script -a '/opt/test.sh'
  10. [root@test2 ~]# cd /opt/
  11. [root@test2 opt]# ls
  12. jenkins-2.396-1.1.noarch.rpm  test  test2.txt
  13. [root@test2 opt]# cat test2.txt
  14. hello world!
复制代码
运行的本地的脚本,把脚本运行的效果输出到目标主机。
  1. [root@test1 opt]# cp test.sh test1.sh
  2. [root@test1 opt]# vim test1.sh
  3. #!/bin/bash
  4. ifconfig > /opt/test3.txt
  5. free -h > /opt/test4.txt
  6. df -h > /opt/test5.txt
  7. [root@test1 opt]# ansible 192.168.168.22 -m script -a '/opt/test1.sh'  ##脚本在本地,脚本里面的命令是在目标主机里面运行
复制代码

14、setup模块:

查看目标主机的信息:ip地址,cpu,内核,系统信息。
  1. [root@test1 opt]# ansible 192.168.168.22 -m setup
复制代码
查看目标主机的cpu
  1. [root@test1 opt]# ansible 192.168.168.22 -m setup
  2. -a 'filter=ansible_*processor*'
复制代码

  1. [root@test1 opt]# ansible 192.168.168.22 -m setup
  2. -a 'filter=ansible_processor'
复制代码
查看内核版本

  1. [root@test1 opt]# ansible 192.168.168.22 -m setup
  2. -a 'filter=ansible_proc_cmdline'
复制代码
查看内存

  1. [root@test1 opt]# ansible 192.168.168.22 -m setup
  2. -a 'filter=ansible_mem*'
复制代码
查看系统信息

  1. [root@test1 opt]# ansible 192.168.168.22 -m setup
  2. -a 'filter=ansible_system'
复制代码
总结:



  • command和shell
  • copy,yum,user
  • service服务,对服务管理
  • file模块,文件的属性进行修改
  • hostname 改主机名
  • ping模块
指定端口


这种情况就不再需要传输密钥对,对目标主机生效


对目标组内全部主机都生效


利用拼接对web添加多个主机

192.168.168.[1:5] [0:9]

组嵌套

二、ansible的脚本:

playbook:脚本

2.1、playbook的构成:

1、Tasks 任务,每个Tasks 就是一个模块
2、variables 变量,存储和传递数据,可以自定义,也可以是全局变量,也可以是脚本外传参。
3、Templates模块,用于生成配置文件和多任务的编排。
4、handlers 处理器,用于满足某些条件时,触发的利用,一样寻常用于重启等利用。
5、roles 角色 组织和封装脚本的过程,角色可以把任务、变量、模块、处理器,组合成一个可用单元。
2.1.1、安装httpd

  1. [root@test1 opt]# vim test1.yaml
  2. - name: first play
  3. #定义这个剧本的名称,可不写
  4.   gather_facts: false
  5. #在执行剧本之前是否收集目标主机的信息,false:不收集,可用加快执行速度,如果不写>,默认就是收集。
  6.   hosts: 192.168.168.22
  7. #指定目标主机,可以是组名,也可以是ip地址
  8.   remote_user: root
  9. #在目标主机的执行用户
  10.   tasks:
  11.     - name: test connection
  12. #定义一个任务的名称,可以自定义
  13.       ping:
  14. #ping就是模块的名称
  15.     - name: close selinux
  16.       command: '/sbin/setenforce 0'
  17.       ignore_errors: true
  18. # 如果在执行任务中报错,返回码非0,报错,task就会停止,ignore_errors:True就会忽略
  19. 错误,继续执行下一个任务
  20.     - name: close firewalld
  21.       service: name=firewalld state=stopped
  22. #调用service模块,关闭防火墙
  23.     - name: install httpd
  24.       yum: name=httpd state=latest
  25. #latest,安装当前库中的最新版本的软件
  26.     - name: interview
  27.       shell: echo "this is httpd" > /var/www/html/index.html
  28. #指定shell模块。修改默认的访问页面
  29.       notify: restart httpd
  30. #ansible 在执行任务之后不会立即执行重启,通过notify指令对应的名称传给触发器,让触
  31. 发器在任务的最后执行重启,影响执行的效率
  32.   handlers:
  33.     - name: restart httpd
  34.       service: name=httpd state=restarted
  35.       
  36. [root@test1 opt]# ansible-playbook test1.yaml
复制代码


2.1.2、安装nginx

#安装nginx,传一个配置文件到目标主机,修改默认端口为8080,访问页面的内容 this is nginx 安装方式yum
  1. [root@test1 opt]# yum -y install nginx ##提前下载nginx,传出nginx.conf------在/etc/nginx/nginx.conf
  2. [root@test1 /]# cp /etc/nginx/nginx.conf /opt/nginx.conf
  3. [root@test1 opt]# vim nginx.conf
  4. 39         listen       8080;
  5. 40         listen       [::]:8080;
  6. [root@test1 opt]# yum -y remove nginx
  7. [root@test1 opt]# vim test2.yaml
  8. - name: second play
  9. #定义这个剧本的名称,可不写
  10.   gather_facts: false
  11. #在执行剧本之前是否收集目标主机的信息,false:不收集,可用加快执行速度,如果不写,默认就是收集。
  12.   hosts: 192.168.168.22
  13. #指定目标主机,可以是组名,也可以是ip地址
  14.   remote_user: root
  15. #在目标主机的执行用户
  16.   tasks:
  17.     - name: test2 connection
  18. #定义一个任务的名称,可以自定义
  19.       ping:
  20. #ping就是模块的名称
  21.     - name: close selinux
  22.       command: '/sbin/setenforce 0'
  23.       ignore_errors: true
  24. # 如果在执行任务中报错,返回码非0,报错,task就会停止,ignore_errors:True就会忽略错误,继续执行下一个任务
  25.     - name: close firewalld
  26.       service: name=firewalld state=stopped
  27. #调用service模块,关闭防火墙
  28.     - name: install nginx
  29.       yum: name=nginx state=latest
  30. #latest,安装当前库中的最新版本的软件
  31.     - name: interview
  32.       shell: echo "this is nginx" > /usr/share/nginx/html/index.html
  33. #指定shell模块。修改默认的访问页面
  34.     - name: nginx.conf
  35.       copy: 'src=/opt/nginx.conf dest=/etc/nginx/nginx.conf'
  36.       notify: restart nginx
  37. #ansible 在执行任务之后不会立即执行重启,通过notify指令对应的名称传给触发器,让触发器在任务的最后执行重启,影响执行的效率
  38.   handlers:
  39.     - name: restart nginx
  40.       service: name=nginx state=restarted
  41.       
  42. [root@test1 opt]# ansible-playbook test2.yaml
复制代码

访问测试


2.2、#定义变量,引用变量:#脚本当中定义参量

  1. [root@test1 opt]# vim test3.yaml
  2. #定义变量,引用变量:
  3. #脚本当中定义,以及脚本外传参
  4. - name: second play
  5.   hosts: 192.168.168.22
  6.   remote_user: root
  7.   vars:
  8.     groupname: mysql
  9.     username: nginx1
  10. #定义变量:
  11.   tasks:
  12.     - name: create group
  13.       group:
  14.         name: "{{ groupname }}"
  15.         system: yes
  16.         gid: 306
  17.     - name: create user
  18.       user:
  19.         name: "{{ username }}"
  20.         uid: 306
  21.         group: "{{ groupname }}"
  22.         
  23.         
  24. [root@test1 opt]# ansible-playbook test3.yaml
复制代码


  1. [root@test1 opt]# vim test3.yaml
  2. #定义变量,引用变量:
  3. #脚本当中定义,以及脚本外传参
  4. - name: second play
  5.   hosts: 192.168.168.22
  6.   remote_user: root
  7.   vars:
  8.     groupname: mysql
  9.     username: nginx1
  10. #定义变量:
  11.   tasks:
  12.     - name: create group
  13.       group:
  14.         name: "{{ groupname }}"
  15.         system: yes
  16.         gid: 16
  17.     - name: create user
  18.       user:
  19.         name: "{{ username }}"
  20.         uid: 16
  21.         group: "{{ groupname }}"
  22. [root@test1 opt]# ansible-playbook test3.yaml
复制代码


[root@test2 opt]# yum -y remove nginx ##卸载软件,就可覆盖用户和组

  1. #定义变量,引用变量:
  2. #脚本当中定义,以及脚本外传参
  3. - name: second play
  4.   hosts: 192.168.168.22
  5.   remote_user: root
  6.   become: yes
  7. #先
  8.   vars:
  9.     groupname: mysql
  10.     username: nginx
  11. #定义变量:
  12.   tasks:
  13.     - name: create group
  14.       group:
  15.         name: "{{ groupname }}"
  16.         system: yes
  17.         gid: 306
  18.     - name: create user
  19.       user:
  20.         name: "{{ username }}"
  21.         uid: 306
  22.         group: "{{ groupname }}"
  23. [root@test1 opt]# ansible-playbook test3.yaml
复制代码


2.3、脚本外传参

[root@test1 opt]# ansible-playbook test4.yaml -e 'groupname=test1 username=test1'##脚本外面的优先级比里面高
  1. [root@test1 opt]# vim test4.yaml
  2. #定义变量,引用变量:
  3. #脚本当中定义,以及脚本外传参
  4. - name: second play
  5.   hosts: 192.168.168.22
  6.   remote_user: root
  7.   become: yes
  8. #先
  9.   vars:
  10.     groupname: mysql
  11.     username: nginx
  12. #定义变量:
  13.   tasks:
  14.     - name: create group
  15.       group:
  16.         name: "{{ groupname }}"
  17.         system: yes
  18.         gid: 307
  19.     - name: create user
  20.       user:
  21.         name: "{{ username }}"
  22.         uid: 307
  23.         group: "{{ groupname }}"
  24.         
  25.         
  26. [root@test1 opt]# ansible-playbook test4.yaml -e 'groupname=test1 username=test1'
复制代码


2.4、检查脚本的语法

1、检查脚本语法

  1. [root@test1 opt]# ansible-playbook test3.yaml --syntax-check
复制代码
2、检查脚本里面的任务个数

  1. [root@test1 opt]# ansible-playbook test3.yaml --list-task
复制代码

3、检查对哪些主机生效

  1. [root@test1 opt]# ansible-playbook test3.yaml --list-hosts
复制代码

4、指定位置运行

  1. [root@test1 opt]# ansible-playbook test3.yaml --start-at-task='create user' -e 'username=test3 groupname=mysql'  ##此脚本中,组需要存在
复制代码

cat /etc/passwd ##查看用户
cat /etc/group ##查看组
  1. [root@test1 opt]# vim test4.yaml
  2. #定义变量,引用变量:
  3. #脚本当中定义,以及脚本外传参
  4. - name: second play
  5.   hosts: 192.168.168.22
  6.   remote_user: root
  7.   become: yes
  8. #先
  9.   vars:
  10.     groupname: mysql
  11.     username: nginx
  12. #定义变量:
  13.   tasks:
  14.     - name: create group
  15.       group:
  16.         name: "{{ groupname }}"
  17.         system: yes
  18.         gid: 307
  19.     - name: create user
  20.       user:
  21.         name: "{{ username }}"
  22.         uid: 308
  23.         group: "{{ groupname }}"
  24. [root@test1 opt]# ansible-playbook test3.yaml --start-at-task='create user' -e 'username=test3 groupname=mysql'  ##此脚本中,组需要存在
复制代码

5、切换用户

  1. #定义变量,引用变量:
  2. #脚本当中定义,以及脚本外传参
  3. - name: second play
  4.   hosts: 192.168.168.22
  5.   remote_user: dn
  6.   become: yes
  7. #先用普通用户执行,但是需要切换到其他的用户,例如切换到管理员
  8.   become_user: root
  9.   vars:
  10.     groupname: mysql
  11.     username: nginx
  12. #定义变量:
  13.   tasks:
  14.     - name: create group
  15.       group:
  16.         name: "{{ groupname }}"
  17.         system: yes
  18.         gid: 307
  19.     - name: create user
  20.       user:
  21.         name: "{{ username }}"
  22.         uid: 308
  23.         group: "{{ groupname }}"
复制代码
2.5、#如安在脚本中实现条件判断:

#when 满足条件的主机实行,不满足的跳过
  1. [root@test1 opt]# vim test5.yaml
  2. #如何在脚本中实现条件判断:
  3. #when 满足条件的主机执行,不满足的跳过
  4. - name: this is if
  5.   hosts: all
  6.   remote_user: root
  7.   tasks:
  8.     - name: test when
  9.       debug: msg='条件满足'
  10. #debug相当于echo echo "条件满足"
  11.       when: ansible_default_ipv4.address == "192.168.168.22"
  12. [root@test1 opt]# ansible-playbook test5.yaml
  13. ##取反,除了192.168.168.22都执行
  14. [root@test1 opt]# vim test5.yaml
  15. #如何在脚本中实现条件判断:
  16. #when 满足条件的主机执行,不满足的跳过
  17. - name: this is if
  18.   hosts: all
  19.   remote_user: root
  20.   tasks:
  21.     - name: test when
  22.       debug: msg='条件满足'
  23. #debug相当于echo echo "条件满足"
  24.       when: ansible_default_ipv4.address != "192.168.168.22"   ##取反,除了192.168.168.22都执行
复制代码
2.6、循环结构

  1. [root@test1 opt]# vim test6.yaml
  2. #循环结构:absible有多种循环方式,一般都命名为with_items,定义循环的内容。
  3. #with_item 单循环输出:
  4. - name: item test
  5.   hosts: 192.168.168.22
  6.   remote_user: root
  7.   gather_facts: false
  8.   tasks:
  9.     - debug:
  10.         msg: "{{item}}"
  11.       with_items: [a,b,c,d]
  12. #输出item的值,with_items:a b c d 依次传入      
  13.       
  14. [root@test1 opt]# ansible-playbook test6.yaml
  15. #循环结构:absible有多种循环方式,一般都命名为with_items,定义循环的内容。
  16. #with_item 单循环输出:
  17. - name: item test
  18.   hosts: 192.168.168.22
  19.   remote_user: root
  20.   gather_facts: false
  21.   tasks:
  22.     - debug:
  23.         msg: "{{item}}"
  24.       with_items:
  25.         - [a,b,c,d]
  26.         - [1,2,3,4]
  27. #输出item的值,with_items:a b c d 依次传入
  28. [root@test1 opt]# ansible-playbook test6.yaml
  29. #循环结构:absible有多种循环方式,一般都命名为with_items,定义循环的内容。
  30. #with_item 单循环输出:
  31. - name: item test
  32.   hosts: 192.168.168.22
  33.   remote_user: root
  34.   gather_facts: false
  35.   tasks:
  36.     - debug:
  37.         msg: "{{item}}"
  38.       with_list:
  39.         - [a,b,c,d]
  40.         - [1,2,3,4]
  41. #输出item的值,with_items:a b c d 依次传入
  42. #with_list,整个列表作为一个整体,进行输出
  43. [root@test1 opt]# ansible-playbook test6.yaml
  44. #循环结构:absible有多种循环方式,一般都命名为with_items,定义循环的内容。
  45. #with_item 单循环输出:
  46. - name: item test
  47.   hosts: 192.168.168.22
  48.   remote_user: root
  49.   gather_facts: false
  50.   tasks:
  51.     - debug:
  52.         msg: "{{item}}"
  53.       with_together:
  54.         - [a,b,c,d]
  55.         - [1,2,3,4]
  56. #输出item的值,with_items:a b c d 依次传入
  57. #with_list,整个列表作为一个整体,进行输出
  58. [root@test1 opt]# ansible-playbook test6.yaml
  59. #循环结构:absible有多种循环方式,一般都命名为with_items,定义循环的内容。
  60. #with_item 单循环输出:
  61. - name: item test
  62.   hosts: 192.168.168.22
  63.   remote_user: root
  64.   gather_facts: false
  65.   tasks:
  66.     - debug:
  67.         msg: "{{item}}"
  68.       with_together:
  69.         - [a,b,c,d]
  70.         - [1,2,3,4]
  71.         - [A,B,C]
  72. #输出item的值,with_items:a b c d 依次传入
  73. #with_list,整个列表作为一个整体,进行输出
  74. [root@test1 opt]# ansible-playbook test6.yaml
  75. #循环结构:absible有多种循环方式,一般都命名为with_items,定义循环的内容。
  76. #with_item 单循环输出:
  77. - name: item test
  78.   hosts: 192.168.168.22
  79.   remote_user: root
  80.   gather_facts: false
  81.   tasks:
  82.     - debug:
  83.         msg: "{{item}}"
  84.       with_nested:
  85.         - [a,b,c,d]
  86.         - [1,2,3,4]
  87.         - [A,B,C]
  88. #输出item的值,with_items:a b c d 依次传入
  89. #with_list,整个列表作为一个整体,进行输出
  90. [root@test1 opt]# ansible-playbook test6.yaml
  91. #循环结构:absible有多种循环方式,一般都命名为with_items,定义循环的内容。
  92. #with_item 单循环输出:
  93. - name: item test
  94.   hosts: 192.168.168.22
  95.   remote_user: root
  96.   gather_facts: false
  97.   tasks:
  98.     - debug:
  99.         msg: "{{item}}"
  100.       with_nested:
  101.         - [a,b,c,d]
  102.         - [1,2,3,4]
  103. #输出item的值,with_items:a b c d 依次传入
  104. #with_list,整个列表作为一个整体,进行输出
  105. [root@test1 opt]# ansible-playbook test6.yaml
  106. #循环结构:absible有多种循环方式,一般都命名为with_items,定义循环的内容。
  107. #with_item 单循环输出:
  108. - name: item test
  109.   hosts: 192.168.168.22
  110.   remote_user: root
  111.   gather_facts: false
  112.   tasks:
  113.     - debug:
  114.         msg: "{{item}}"
  115.       with_nested:
  116.         - [a,b,c,d]
  117.         - [1,2,3,4]
  118. #输出item的值,with_items:a b c d 依次传入
  119. #with_list,整个列表作为一个整体,进行输出
  120. #with_together,作为整体,以列两两配对输出
  121. #with_nested:每一层都是遍历执行一遍,输出结束
  122. #条件判断,主机的ip
  123. #才会执行,一次性创建4个文件,/opt/a /opt/b /opt/c /opt/d 循环 with_items
  124. [root@test1 opt]# ansible-playbook test6.yaml
复制代码
#条件判断,主机的ip
#才会实行,一次性创建4个文件,/opt/a /opt/b /opt/c
  1. [root@test1 opt]# vim test11.yaml
  2. - name: file test
  3.   hosts: all
  4.   remote_user: root
  5.   tasks:
  6.     - name: file when
  7.       file:
  8.         path: "{{ item }}"
  9.         state: touch
  10.       with_items: [/opt/a,/opt/b,/opt/c,/opt/d]
  11.       when: ansible_default_ipv4.address == "192.168.168.22"
  12. [root@test1 opt]# ansible-playbook test11.yaml
  13. [root@test2 opt]# ls
  14. a  c  jenkins-2.396-1.1.noarch.rpm  test2.txt  test4.txt
  15. b  d  test                          test3.txt  test5.txt
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

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

本版积分规则

熊熊出没

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表