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 selinux
      command: '/sbin/setenforce 0'
      ignore_errors: true
# 如果在执行任务中报错,返回码非0,报错,task就会停止,ignore_errors:True就会忽略
错误,继续执行下一个任务
    - name: close firewalld
      service: name=firewalld state=stopped
#调用service模块,关闭防火墙
    - name: install httpd
      yum: name=httpd state=latest
#latest,安装当前库中的最新版本的软件
    - name: interview
      shell: echo "this is httpd" > /var/www/html/index.html
#指定shell模块。修改默认的访问页面
      notify: restart httpd
#ansible 在执行任务之后不会立即执行重启,通过notify指令对应的名称传给触发器,让触
发器在任务的最后执行重启,影响执行的效率
  handlers:
    - name: restart httpd
      service: 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 selinux
      command: '/sbin/setenforce 0'
      ignore_errors: true
# 如果在执行任务中报错,返回码非0,报错,task就会停止,ignore_errors:True就会忽略错误,继续执行下一个任务
    - name: close firewalld
      service: name=firewalld state=stopped
#调用service模块,关闭防火墙
    - name: install nginx
      yum: name=nginx state=latest
#latest,安装当前库中的最新版本的软件
    - name: interview
      shell: echo "this is nginx" > /usr/share/nginx/html/index.html
#指定shell模块。修改默认的访问页面
    - name: nginx.conf
      copy: 'src=/opt/nginx.conf dest=/etc/nginx/nginx.conf'
      notify: restart nginx
#ansible 在执行任务之后不会立即执行重启,通过notify指令对应的名称传给触发器,让触发器在任务的最后执行重启,影响执行的效率
  handlers:
    - name: restart nginx
      service: name=nginx state=restarted
      
[root@test1 opt]# ansible-playbook test2.yaml 

在这里插入图片描述

访问测试

在这里插入图片描述

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

[root@test1 opt]# vim test3.yaml 

#定义变量,引用变量:
#脚本当中定义,以及脚本外传参
- name: second play
  hosts: 192.168.168.22
  remote_user: root
  vars:
    groupname: mysql
    username: nginx1
#定义变量:
  tasks:
    - name: create group
      group:
        name: "{{ groupname }}"
        system: yes
        gid: 306
    - name: create user
      user:
        name: "{{ username }}"
        uid: 306
        group: "{{ groupname }}"
        
        
[root@test1 opt]# ansible-playbook test3.yaml 

在这里插入图片描述

在这里插入图片描述

[root@test1 opt]# vim test3.yaml 

#定义变量,引用变量:
#脚本当中定义,以及脚本外传参
- name: second play
  hosts: 192.168.168.22
  remote_user: root
  vars:
    groupname: mysql
    username: nginx1
#定义变量:
  tasks:
    - name: create group
      group:
        name: "{{ groupname }}"
        system: yes
        gid: 16
    - name: create user
      user:
        name: "{{ username }}"
        uid: 16
        group: "{{ groupname }}"

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

在这里插入图片描述

在这里插入图片描述

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

在这里插入图片描述

#定义变量,引用变量:
#脚本当中定义,以及脚本外传参
- name: second play
  hosts: 192.168.168.22
  remote_user: root
  become: yes
#先
  vars:
    groupname: mysql
    username: nginx
#定义变量:
  tasks:
    - name: create group
      group:
        name: "{{ groupname }}"
        system: yes
        gid: 306
    - name: create user
      user:
        name: "{{ username }}"
        uid: 306
        group: "{{ 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 play
  hosts: 192.168.168.22
  remote_user: root
  become: yes
#先
  vars:
    groupname: mysql
    username: nginx
#定义变量:
  tasks:
    - name: create group
      group:
        name: "{{ groupname }}"
        system: yes
        gid: 307
    - name: create user
      user:
        name: "{{ username }}"
        uid: 307
        group: "{{ 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 play
  hosts: 192.168.168.22
  remote_user: root
  become: yes
#先
  vars:
    groupname: mysql
    username: nginx
#定义变量:
  tasks:
    - name: create group
      group:
        name: "{{ groupname }}"
        system: yes
        gid: 307
    - name: create user
      user:
        name: "{{ username }}"
        uid: 308
        group: "{{ groupname }}"


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

在这里插入图片描述

5、切换用户

#定义变量,引用变量:
#脚本当中定义,以及脚本外传参
- name: second play
  hosts: 192.168.168.22
  remote_user: dn
  become: yes
#先用普通用户执行,但是需要切换到其他的用户,例如切换到管理员
  become_user: root
  vars:
    groupname: mysql
    username: nginx
#定义变量:
  tasks:
    - name: create group
      group:
        name: "{{ groupname }}"
        system: yes
        gid: 307
    - name: create user
      user:
        name: "{{ username }}"
        uid: 308
        group: "{{ groupname }}"

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

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

[root@test1 opt]# vim test5.yaml 

#如何在脚本中实现条件判断:
#when 满足条件的主机执行,不满足的跳过
- name: this is if
  hosts: all
  remote_user: root
  tasks:
    - name: test when
      debug: 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 if
  hosts: all
  remote_user: root
  tasks:
    - name: test when
      debug: 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 test
  hosts: 192.168.168.22
  remote_user: root
  gather_facts: false
  tasks:
    - 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 test
  hosts: 192.168.168.22
  remote_user: root
  gather_facts: false
  tasks:
    - 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 test
  hosts: 192.168.168.22
  remote_user: root
  gather_facts: false
  tasks:
    - 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 test
  hosts: 192.168.168.22
  remote_user: root
  gather_facts: false
  tasks:
    - 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 test
  hosts: 192.168.168.22
  remote_user: root
  gather_facts: false
  tasks:
    - 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 test
  hosts: 192.168.168.22
  remote_user: root
  gather_facts: false
  tasks:
    - 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 test
  hosts: 192.168.168.22
  remote_user: root
  gather_facts: false
  tasks:
    - 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 test
  hosts: 192.168.168.22
  remote_user: root
  gather_facts: false
  tasks:
    - 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 test
  hosts: all
  remote_user: root
  tasks:
    - name: file when
      file:
        path: "{{ item }}"
        state: touch
      with_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

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值