欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 养生 > 78、 ansible----playbook

78、 ansible----playbook

2024/10/23 19:21:11 来源:https://blog.csdn.net/m0_74149099/article/details/141432954  浏览:    关键词:78、 ansible----playbook

一、ansible模块

11、防火墙和网络模块:

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

在这里插入图片描述

[root@test3 ~]# yum -y install nginx[root@test3 ~]# systemctl restart nginx.service [root@test3 ~]# curl 192.168.168.23
this is nginx!
[root@test1 ~]# ansible 192.168.168.23 -m iptables -a 'chain=INPUT protocol=tcp destination_port=80 jump=REJECT' -b  ##80端口关闭,拒绝访问

删除防火墙策略

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

在这里插入图片描述

[root@test1 ~]# ansible 192.168.168.23 -m iptables -a 'chain=INPUT protocol=tcp destination_port=80 jump=ACCEPT' -b  ##放开80端口

firewalld模块

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

在这里插入图片描述

删除端口,防火墙策略

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

在这里插入图片描述

12、配置网卡

修改ip地址
[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'"
查看有没有改成功
[root@test2 ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
DEVICE=ens33
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.168.25  ##修改成功
NETMASK=255.255.255.0
GATEWAY=192.168.168.2
DNS1=218.2.135.1
DNS2=221.131.143.69
重启网卡
[root@test1 ~]# ansible 192.168.168.22 -a 'systemctl restart network'
回终端查看

在这里插入图片描述

ip地址改回为192.168.168.22
[root@test1 ~]# cd /etc/ansible/
[root@test1 ansible]# ls
ansible.cfg  hosts  roles  test2.sh
[root@test1 ansible]# vim hosts 192.168.168.25[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'"[root@test1 ansible]# ansible 192.168.168.25 -a 'systemctl restart network'点击xshell,重新连接ifconfig

在这里插入图片描述

13、script模块:

运行的本地的脚本,把脚本运行的结果输出到目标主机。

[root@test1 ansible]# cd /opt/
[root@test1 opt]# vim test.sh
[root@test1 opt]# chmod 777 test.sh 
[root@test1 opt]# ansible 192.168.168.22 -m script -a '/opt/test.sh'[root@test1 ansible]# cd /opt/
[root@test1 opt]# vim test.sh#!/bin/bash
echo "hello world!" > /opt/test2.txt[root@test1 opt]# chmod 777 test.sh 
[root@test1 opt]# ansible 192.168.168.22 -m script -a '/opt/test.sh'[root@test2 ~]# cd /opt/
[root@test2 opt]# ls
jenkins-2.396-1.1.noarch.rpm  test  test2.txt
[root@test2 opt]# cat test2.txt
hello world!

运行的本地的脚本,把脚本运行的结果输出到目标主机。

[root@test1 opt]# cp test.sh test1.sh
[root@test1 opt]# vim test1.sh#!/bin/bash
ifconfig > /opt/test3.txt
free -h > /opt/test4.txt
df -h > /opt/test5.txt[root@test1 opt]# ansible 192.168.168.22 -m script -a '/opt/test1.sh'  ##脚本在本地,脚本里面的命令是在目标主机里面运行

在这里插入图片描述

14、setup模块:

查看目标主机的信息:ip地址,cpu,内核,系统信息。

[root@test1 opt]# ansible 192.168.168.22 -m setup

查看目标主机的cpu

[root@test1 opt]# ansible 192.168.168.22 -m setup -a 'filter=ansible_*processor*'

在这里插入图片描述

[root@test1 opt]# ansible 192.168.168.22 -m setup -a 'filter=ansible_processor'
查看内核版本
[root@test1 opt]# ansible 192.168.168.22 -m setup -a 'filter=ansible_proc_cmdline'
查看内存
[root@test1 opt]# ansible 192.168.168.22 -m setup -a 'filter=ansible_mem*'
查看系统信息
[root@test1 opt]# ansible 192.168.168.22 -m setup -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

[root@test1 opt]# vim test1.yaml- name: first play
#定义这个剧本的名称,可不写gather_facts: false
#在执行剧本之前是否收集目标主机的信息,false:不收集,可用加快执行速度,如果不写>,默认就是收集。hosts: 192.168.168.22
#指定目标主机,可以是组名,也可以是ip地址remote_user: root
#在目标主机的执行用户tasks:- name: test connection
#定义一个任务的名称,可以自定义ping:
#ping就是模块的名称- name: close selinuxcommand: '/sbin/setenforce 0'ignore_errors: true
# 如果在执行任务中报错,返回码非0,报错,task就会停止,ignore_errors:True就会忽略
错误,继续执行下一个任务- name: close firewalldservice: name=firewalld state=stopped
#调用service模块,关闭防火墙- name: install httpdyum: name=httpd state=latest
#latest,安装当前库中的最新版本的软件- name: interviewshell: echo "this is httpd" > /var/www/html/index.html
#指定shell模块。修改默认的访问页面notify: restart httpd
#ansible 在执行任务之后不会立即执行重启,通过notify指令对应的名称传给触发器,让触
发器在任务的最后执行重启,影响执行的效率handlers:- name: restart httpdservice: name=httpd state=restarted[root@test1 opt]# ansible-playbook test1.yaml 

在这里插入图片描述

在这里插入图片描述

2.1.2、安装nginx

#安装nginx,传一个配置文件到目标主机,修改默认端口为8080,访问页面的内容 this is nginx 安装方式yum

[root@test1 opt]# yum -y install nginx ##提前下载nginx,传出nginx.conf------在/etc/nginx/nginx.conf[root@test1 /]# cp /etc/nginx/nginx.conf /opt/nginx.conf[root@test1 opt]# vim nginx.conf 39         listen       8080;40         listen       [::]:8080;[root@test1 opt]# yum -y remove nginx[root@test1 opt]# vim test2.yaml- name: second play
#定义这个剧本的名称,可不写gather_facts: false
#在执行剧本之前是否收集目标主机的信息,false:不收集,可用加快执行速度,如果不写,默认就是收集。hosts: 192.168.168.22
#指定目标主机,可以是组名,也可以是ip地址remote_user: root
#在目标主机的执行用户tasks:- name: test2 connection
#定义一个任务的名称,可以自定义ping:
#ping就是模块的名称- name: close selinuxcommand: '/sbin/setenforce 0'ignore_errors: true
# 如果在执行任务中报错,返回码非0,报错,task就会停止,ignore_errors:True就会忽略错误,继续执行下一个任务- name: close firewalldservice: name=firewalld state=stopped
#调用service模块,关闭防火墙- name: install nginxyum: name=nginx state=latest
#latest,安装当前库中的最新版本的软件- name: interviewshell: echo "this is nginx" > /usr/share/nginx/html/index.html
#指定shell模块。修改默认的访问页面- name: nginx.confcopy: 'src=/opt/nginx.conf dest=/etc/nginx/nginx.conf'notify: restart nginx
#ansible 在执行任务之后不会立即执行重启,通过notify指令对应的名称传给触发器,让触发器在任务的最后执行重启,影响执行的效率handlers:- name: restart nginxservice: name=nginx state=restarted[root@test1 opt]# ansible-playbook test2.yaml 

在这里插入图片描述

访问测试

在这里插入图片描述

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

[root@test1 opt]# vim test3.yaml #定义变量,引用变量:
#脚本当中定义,以及脚本外传参
- name: second playhosts: 192.168.168.22remote_user: rootvars:groupname: mysqlusername: nginx1
#定义变量:tasks:- name: create groupgroup:name: "{{ groupname }}"system: yesgid: 306- name: create useruser:name: "{{ username }}"uid: 306group: "{{ groupname }}"[root@test1 opt]# ansible-playbook test3.yaml 

在这里插入图片描述

在这里插入图片描述

[root@test1 opt]# vim test3.yaml #定义变量,引用变量:
#脚本当中定义,以及脚本外传参
- name: second playhosts: 192.168.168.22remote_user: rootvars:groupname: mysqlusername: nginx1
#定义变量:tasks:- name: create groupgroup:name: "{{ groupname }}"system: yesgid: 16- name: create useruser:name: "{{ username }}"uid: 16group: "{{ groupname }}"[root@test1 opt]# ansible-playbook test3.yaml

在这里插入图片描述

在这里插入图片描述

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

在这里插入图片描述

#定义变量,引用变量:
#脚本当中定义,以及脚本外传参
- name: second playhosts: 192.168.168.22remote_user: rootbecome: yes
#先vars:groupname: mysqlusername: nginx
#定义变量:tasks:- name: create groupgroup:name: "{{ groupname }}"system: yesgid: 306- name: create useruser:name: "{{ username }}"uid: 306group: "{{ groupname }}"[root@test1 opt]# ansible-playbook test3.yaml

在这里插入图片描述

在这里插入图片描述

2.3、脚本外传参

[root@test1 opt]# ansible-playbook test4.yaml -e 'groupname=test1 username=test1'##脚本外面的优先级比里面高

[root@test1 opt]# vim test4.yaml#定义变量,引用变量:
#脚本当中定义,以及脚本外传参
- name: second playhosts: 192.168.168.22remote_user: rootbecome: yes
#先vars:groupname: mysqlusername: nginx
#定义变量:tasks:- name: create groupgroup:name: "{{ groupname }}"system: yesgid: 307- name: create useruser:name: "{{ username }}"uid: 307group: "{{ groupname }}"[root@test1 opt]# ansible-playbook test4.yaml -e 'groupname=test1 username=test1'

在这里插入图片描述

在这里插入图片描述

2.4、检查脚本的语法

1、检查脚本语法

[root@test1 opt]# ansible-playbook test3.yaml --syntax-check

2、检查脚本里面的任务个数

[root@test1 opt]# ansible-playbook test3.yaml --list-task

在这里插入图片描述

3、检查对哪些主机生效

[root@test1 opt]# ansible-playbook test3.yaml --list-hosts

在这里插入图片描述

4、指定位置运行

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

在这里插入图片描述

cat /etc/passwd ##查看用户

cat /etc/group ##查看组

[root@test1 opt]# vim test4.yaml #定义变量,引用变量:
#脚本当中定义,以及脚本外传参
- name: second playhosts: 192.168.168.22remote_user: rootbecome: yes
#先vars:groupname: mysqlusername: nginx
#定义变量:tasks:- name: create groupgroup:name: "{{ groupname }}"system: yesgid: 307- name: create useruser:name: "{{ username }}"uid: 308group: "{{ groupname }}"[root@test1 opt]# ansible-playbook test3.yaml --start-at-task='create user' -e 'username=test3 groupname=mysql'  ##此脚本中,组需要存在

在这里插入图片描述

5、切换用户

#定义变量,引用变量:
#脚本当中定义,以及脚本外传参
- name: second playhosts: 192.168.168.22remote_user: dnbecome: yes
#先用普通用户执行,但是需要切换到其他的用户,例如切换到管理员become_user: rootvars:groupname: mysqlusername: nginx
#定义变量:tasks:- name: create groupgroup:name: "{{ groupname }}"system: yesgid: 307- name: create useruser:name: "{{ username }}"uid: 308group: "{{ groupname }}"

2.5、#如何在脚本中实现条件判断:

#when 满足条件的主机执行,不满足的跳过

[root@test1 opt]# vim test5.yaml #如何在脚本中实现条件判断:
#when 满足条件的主机执行,不满足的跳过
- name: this is ifhosts: allremote_user: roottasks:- name: test whendebug: msg='条件满足'
#debug相当于echo echo "条件满足"when: ansible_default_ipv4.address == "192.168.168.22"[root@test1 opt]# ansible-playbook test5.yaml##取反,除了192.168.168.22都执行[root@test1 opt]# vim test5.yaml #如何在脚本中实现条件判断:
#when 满足条件的主机执行,不满足的跳过
- name: this is ifhosts: allremote_user: roottasks:- name: test whendebug: msg='条件满足'
#debug相当于echo echo "条件满足"when: ansible_default_ipv4.address != "192.168.168.22"   ##取反,除了192.168.168.22都执行

2.6、循环结构

[root@test1 opt]# vim test6.yaml #循环结构:absible有多种循环方式,一般都命名为with_items,定义循环的内容。#with_item 单循环输出:- name: item testhosts: 192.168.168.22remote_user: rootgather_facts: falsetasks:- debug:msg: "{{item}}"with_items: [a,b,c,d]
#输出item的值,with_items:a b c d 依次传入      [root@test1 opt]# ansible-playbook test6.yaml#循环结构:absible有多种循环方式,一般都命名为with_items,定义循环的内容。#with_item 单循环输出:- name: item testhosts: 192.168.168.22remote_user: rootgather_facts: falsetasks:- debug:msg: "{{item}}"with_items:- [a,b,c,d]- [1,2,3,4]
#输出item的值,with_items:a b c d 依次传入
[root@test1 opt]# ansible-playbook test6.yaml#循环结构:absible有多种循环方式,一般都命名为with_items,定义循环的内容。#with_item 单循环输出:- name: item testhosts: 192.168.168.22remote_user: rootgather_facts: falsetasks:- debug:msg: "{{item}}"with_list:- [a,b,c,d]- [1,2,3,4]
#输出item的值,with_items:a b c d 依次传入
#with_list,整个列表作为一个整体,进行输出
[root@test1 opt]# ansible-playbook test6.yaml#循环结构:absible有多种循环方式,一般都命名为with_items,定义循环的内容。#with_item 单循环输出:- name: item testhosts: 192.168.168.22remote_user: rootgather_facts: falsetasks:- debug:msg: "{{item}}"with_together:- [a,b,c,d]- [1,2,3,4]
#输出item的值,with_items:a b c d 依次传入
#with_list,整个列表作为一个整体,进行输出
[root@test1 opt]# ansible-playbook test6.yaml#循环结构:absible有多种循环方式,一般都命名为with_items,定义循环的内容。#with_item 单循环输出:- name: item testhosts: 192.168.168.22remote_user: rootgather_facts: falsetasks:- debug:msg: "{{item}}"with_together:- [a,b,c,d]- [1,2,3,4]- [A,B,C]
#输出item的值,with_items:a b c d 依次传入
#with_list,整个列表作为一个整体,进行输出
[root@test1 opt]# ansible-playbook test6.yaml#循环结构:absible有多种循环方式,一般都命名为with_items,定义循环的内容。#with_item 单循环输出:- name: item testhosts: 192.168.168.22remote_user: rootgather_facts: falsetasks:- debug:msg: "{{item}}"with_nested:- [a,b,c,d]- [1,2,3,4]- [A,B,C]
#输出item的值,with_items:a b c d 依次传入
#with_list,整个列表作为一个整体,进行输出
[root@test1 opt]# ansible-playbook test6.yaml#循环结构:absible有多种循环方式,一般都命名为with_items,定义循环的内容。#with_item 单循环输出:- name: item testhosts: 192.168.168.22remote_user: rootgather_facts: falsetasks:- debug:msg: "{{item}}"with_nested:- [a,b,c,d]- [1,2,3,4]
#输出item的值,with_items:a b c d 依次传入
#with_list,整个列表作为一个整体,进行输出
[root@test1 opt]# ansible-playbook test6.yaml#循环结构:absible有多种循环方式,一般都命名为with_items,定义循环的内容。#with_item 单循环输出:- name: item testhosts: 192.168.168.22remote_user: rootgather_facts: falsetasks:- debug:msg: "{{item}}"with_nested:- [a,b,c,d]- [1,2,3,4]
#输出item的值,with_items:a b c d 依次传入
#with_list,整个列表作为一个整体,进行输出
#with_together,作为整体,以列两两配对输出
#with_nested:每一层都是遍历执行一遍,输出结束
#条件判断,主机的ip
#才会执行,一次性创建4个文件,/opt/a /opt/b /opt/c /opt/d 循环 with_items
[root@test1 opt]# ansible-playbook test6.yaml

#条件判断,主机的ip
#才会执行,一次性创建4个文件,/opt/a /opt/b /opt/c

[root@test1 opt]# vim test11.yaml - name: file testhosts: allremote_user: roottasks:- name: file whenfile:path: "{{ item }}"state: touchwith_items: [/opt/a,/opt/b,/opt/c,/opt/d]when: ansible_default_ipv4.address == "192.168.168.22"[root@test1 opt]# ansible-playbook test11.yaml
[root@test2 opt]# ls
a  c  jenkins-2.396-1.1.noarch.rpm  test2.txt  test4.txt
b  d  test                          test3.txt  test5.txt

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com