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

2024-08-20 15:52

本文主要是介绍8.19-ansible中模块的使用+playbook的应用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、ansible

1.scripts模块

script模块⽤于在远程机器上执⾏本地脚本。

[root@m0 ~]# vim test000.sh
#!/bin/bash
mkdir /tmp/three
touch /tmp/three/test
echo 'i am echo,at mttt' > /tmp/three/test
echo 'well done'
[root@m0 ~]# source test000.sh 
well done
[root@m0 ~]# ansible group02 -m script -a './test000.sh'
​
# 验证
​
[root@s0 ~]# ls /tmp/
111                                                                      three
a.txt                                                                    xxx
a.txt.4331.2024-08-16@17:23:26~                                          xxx2
systemd-private-18e460b4dc5b47458e28ad6b292e1a98-chronyd.service-ZPvmft

2.用ansible搭建nfs服务

[root@m0 ~]# ansible group02 -m file -a 'path=/static state=directory'
[root@m0 ~]# ansible group02 -m file -a 'path=/static/test state=touch'
​
[root@m0 ~]# ansible group02 -m command -a 'yum -y install nfs-utils'
​
[root@s0 ~]# rpm -qa | grep nfs
libnfsidmap-0.25-19.el7.x86_64
nfs-utils-1.3.0-0.68.el7.2.x86_64
​
[root@m0 ~]# ansible group02 -m yum -a 'name=rpcbind state=latest'
​
[root@s0 ~]# rpm -qa | grep rpcbind
rpcbind-0.2.0-49.el7.x86_64
​
[root@m0 ~]# vim /etc/exports
​
/static *(ro,rsync)
​
[root@m0 ~]# ansible group02 -m copy -a 'src=/etc/exports dest=/etc/exports'
​
[root@m0 ~]# ansible group02 -m service -a 'name=rpcbind state=started enabled=yes'
[root@m0 ~]# ansible group02 -m service -a 'name=nfs state=started enabled=yes'
[root@m0 ~]# yum -y install nfs-utils
[root@m0 ~]# mkdir /nfs
​
[root@m0 ~]# mount -t nfs 192.168.2.112:/static /nfs/
[root@m0 ~]# mount -t nfs 192.168.2.111:/static /nfs/
[root@m0 ~]# mount -t nfs 192.168.2.110:/static /nfs/
mount.nfs: Operation not permitted
[root@m0 ~]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root   17G  4.3G   13G   26% /
devtmpfs                 476M     0  476M    0% /dev
tmpfs                    488M     0  488M    0% /dev/shm
tmpfs                    488M  7.7M  480M    2% /run
tmpfs                    488M     0  488M    0% /sys/fs/cgroup
/dev/sr0                 8.8G  8.8G     0  100% /mnt
/dev/sda1               1014M  130M  885M   13% /boot
tmpfs                     98M     0   98M    0% /run/user/0
192.168.2.110:/static     17G  2.1G   15G   13% /nfs
192.168.2.112:/static     17G  2.1G   15G   13% /nfs
192.168.2.111:/static     17G  2.1G   15G   13% /nfs
​
[root@s0 ~]# ls /static/
test
​
[root@s1 ~]# ls /static/
test
​
[root@s2 ~]# ls /static/
test

二、playbook

playbook剧本是保存在控制机的yml文件

1.Playbook常⻅语法

hosts: ⽤于指定要执⾏任务的主机,其可以是⼀个或多个由冒号分隔主机组。

remote_user: ⽤于指定远程主机上的执⾏任务的⽤户 。

- hosts: group1 
remote_user: root

tasks: 任务列表, 按顺序执⾏任务.

如果⼀个host执⾏task失败, 整个tasks都会回滚, 修正playbook中的错误, 然后重新执⾏即可

tasks:
- name: ensure apache is at the latest version yum: name=httpd,httpd-devel state=latest
- name: write the apache config file copy: src=/etc/httpd/conf/httpd.confdest=/etc/httpd/conf/httpd.conf

handlers: 类似task,但需要使⽤notify通知调⽤。

不管有多少个通知者进⾏了notify,等到play中的所有task执⾏完 成之后,handlers也只会被执⾏⼀次。

handlers最佳的应⽤场景是⽤来重启服务,或者触发系统重启操作,除此以外很少⽤到了。

notify: 
- restart apache- name: ensure apache is running (and enable it at boot)service: name=httpd state=started enabled=yes
handlers:- name: restart apacheservice: name=httpd state=restarted

2.用剧本安装vsftpd

[root@m0 ~]# vim test001.yml
​
---
-       hosts: group02remote_user:  roottasks:-       name: 安装vsftpdyum: name=vsftpd state=latest[root@m0 ~]# ansible-playbook ./test001.yml
​
PLAY [group02] *********************************************************************
​
TASK [Gathering Facts] *************************************************************
ok: [192.168.2.111]
ok: [192.168.2.110]
ok: [other]
​
TASK [安装vsftpd] ********************************************************************
ok: [other]
ok: [192.168.2.111]
ok: [192.168.2.110]
​
PLAY RECAP *************************************************************************
192.168.2.110              : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.2.111              : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
other                      : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
​

3.用剧本卸载和安装vsftpd,且启动服务

[root@m0 ~]# vim test001.yml
---
-       hosts: group02remote_user:  roottasks:-       name: 卸载vsftpdyum: name=vsftpd        state=absent
​-       name: 安装vsftpdyum: name=vsftpd state=latest
​-       name: 启动服务service:        name=vsftpd state=started enabled=yes
​
[root@m0 ~]# ansible-playbook ./test001.yml
​
PLAY [group02] *********************************************************************
​
TASK [Gathering Facts] *************************************************************
ok: [192.168.2.111]
ok: [other]
ok: [192.168.2.110]
​
TASK [卸载vsftpd] ********************************************************************
changed: [other]
changed: [192.168.2.111]
changed: [192.168.2.110]
​
TASK [安装vsftpd] ********************************************************************
changed: [other]
changed: [192.168.2.111]
changed: [192.168.2.110]
​
TASK [启动服务] ************************************************************************
changed: [192.168.2.111]
changed: [192.168.2.110]
changed: [other]
​
PLAY RECAP *************************************************************************
192.168.2.110              : ok=4    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.2.111              : ok=4    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
other                      : ok=4    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
​

4.用剧本修改vsftpd文件,不允许匿名访问

修改配置文件后,要重新启动服务

# 可以访问到数据
​
[root@m0 ~]# lftp 192.168.2.110
lftp 192.168.2.110:~> ls
drwxr-xr-x    2 0        0               6 Jun 09  2021 pub
lftp 192.168.2.110:/> exit
[root@m0 ~]# lftp 192.168.2.111
lftp 192.168.2.111:~> ls
drwxr-xr-x    2 0        0               6 Jun 09  2021 pub
lftp 192.168.2.111:/> exit
​
# 修改配置文件,不允许匿名用户登录
[root@m0 ~]# vim test001.yml
---
-       hosts: group02remote_user:  roottasks:-       name: 卸载vsftpdyum: name=vsftpd        state=absent
​-       name: 安装vsftpdyum: name=vsftpd state=latest
​-       name: 启动服务service:        name=vsftpd state=started enabled=yes
​-       name: 修改配置文件command: sed -i '/^anonymous_enable=YES/s/YES/NO/g' /etc/vsftpd/vsftpd.confnotify:-       abcdefghandlers:-      name: abcdefgservice: name=vsftpd state=restarted
​
[root@m0 ~]# ansible-playbook ./test001.yml
​
PLAY [group02] *********************************************************************
​
TASK [Gathering Facts] *************************************************************
ok: [other]
ok: [192.168.2.110]
ok: [192.168.2.111]
​
TASK [卸载vsftpd] ********************************************************************
changed: [192.168.2.111]
changed: [other]
changed: [192.168.2.110]
​
TASK [安装vsftpd] ********************************************************************
changed: [192.168.2.111]
changed: [other]
changed: [192.168.2.110]
​
TASK [启动服务] ************************************************************************
changed: [other]
changed: [192.168.2.111]
changed: [192.168.2.110]
​
TASK [修改配置文件] **********************************************************************
[WARNING]: Consider using the replace, lineinfile or template module rather than
running 'sed'.  If you need to use command because replace, lineinfile or template
is insufficient you can add 'warn: false' to this command task or set
'command_warnings=False' in ansible.cfg to get rid of this message.
changed: [192.168.2.111]
changed: [192.168.2.110]
changed: [other]
​
RUNNING HANDLER [abcdefg] **********************************************************
changed: [192.168.2.111]
changed: [192.168.2.110]
changed: [other]
​
PLAY RECAP *************************************************************************
192.168.2.110              : ok=6    changed=5    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.2.111              : ok=6    changed=5    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
other                      : ok=6    changed=5    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
​
​
# 访问不到数据了
[root@m0 ~]# vim test001.yml
[root@m0 ~]# lftp 192.168.2.111
lftp 192.168.2.111:~> ls
​

5.剧本的格式:

---
-  hosts:   组名/别名/ip/域名remote_user:   roottasks:- name: 任务说明模块: key0=value0service: name=vfstpd state=started  enabled=yes- name:  修改配置文件command: sed ....notify:-  abcdefghandler:- name: abcdefgservice: name=vfstpd state=restarted

6.将httpd的端口号80改为8080

[root@m0 ~]# vim test002.yml
---
-       hosts:       group01remote_user: roottasks:-       name: 将控制主机的repo文件复制到被控制主机copy:  src=/etc/yum.repos.d  dest=/etc/
​-       name: 安装httpdyum:  name=httpd  state=present
​-       name: 修改配置文件command:  sed -i '/^Listen/s/80/8080/g' /etc/httpd/conf/httpd.conf
​-       name: 修改默认的资源文件command: echo 'xxxxxxx' > /var/www/html/index.html
​-       name: 启动httpd服务service: name=httpd state=started
​
[root@m0 ~]# ansible-playbook ./test002.yml
​
PLAY [group01] *********************************************************************
​
TASK [Gathering Facts] *************************************************************
ok: [192.168.2.111]
ok: [192.168.2.110]
​
TASK [将控制主机的repo文件复制到被控制主机] ********************************************************
ok: [192.168.2.110]
ok: [192.168.2.111]
​
TASK [安装httpd] *********************************************************************
changed: [192.168.2.111]
changed: [192.168.2.110]
​
TASK [修改配置文件] **********************************************************************
[WARNING]: Consider using the replace, lineinfile or template module rather than
running 'sed'.  If you need to use command because replace, lineinfile or template
is insufficient you can add 'warn: false' to this command task or set
'command_warnings=False' in ansible.cfg to get rid of this message.
changed: [192.168.2.111]
changed: [192.168.2.110]
​
TASK [修改默认的资源文件] *******************************************************************
changed: [192.168.2.110]
changed: [192.168.2.111]
​
TASK [启动httpd服务] *******************************************************************
changed: [192.168.2.110]
changed: [192.168.2.111]
​
PLAY RECAP *************************************************************************
192.168.2.110              : ok=6    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.2.111              : ok=6    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
​

7.使用剧本在不同主机上同时创建不同的文件

[root@m0 ~]# vim /etc/ansible/hosts
​
s0 ansible_ssh_host=192.168.2.110 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=1
​
s1 ansible_ssh_host=192.168.2.111 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=1
​
s2 ansible_ssh_host=192.168.2.112 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=1
​
[s]
s0
s1
s2
​
---
-       hosts: s1remote_user: roottasks:-       name: 创建一个文件file: path=/tmp/xxxxxx.txt state=touch
​
-       hosts: s2remote_user: roottasks:-       name: 也创建一个文件file: path=/tmp/yyyyy.txt state=touch
​
...
​
[root@m0 ~]# ansible-playbook ./test003.yml
​
PLAY [s1] **************************************************************************
​
TASK [Gathering Facts] *************************************************************
ok: [s1]
​
TASK [创建一个文件] **********************************************************************
changed: [s1]
​
PLAY [s2] **************************************************************************
​
TASK [Gathering Facts] *************************************************************
ok: [s2]
​
TASK [也创建一个文件] *********************************************************************
changed: [s2]
​
PLAY RECAP *************************************************************************
s1                         : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
s2                         : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

8.使用剧本搭建nfs服务

s1为nfs的服务器

s2为nfs的客户端

[root@m0 ~]# vim test004.yml
​
---
-       hosts: s1remote_user: roottasks:-       name: 按装nfs-utilsyum: name=nfs-utils state=present
​-       name: 安装rpcbindyum: name=rpcbind  state=present
​-       name: 创建共享目录file: path=/static state=directory
​-       name: 配置文件shell: echo '/static *(ro,sync)' > /etc/exports
​-       name: 启动服务nfsservice:  name=nfs state=started enabled=yes
​-       name: 启动服务rpcbindservice: name=rpcbind state=started  enabled=yes
​
-       hosts: s2remote_user: roottasks:-       name: 安装nfs-utilsyum: name=nfs-utils state=latest
​-       name: 创建挂载目录file: path=/nfs state=directory
​-       name: 挂载nfs文件command:  mount -t nfs 192.168.2.111:/static /nfs
...
​
# 验证
[root@s2 ~]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root   17G  2.1G   15G   13% /
devtmpfs                 476M     0  476M    0% /dev
tmpfs                    488M     0  488M    0% /dev/shm
tmpfs                    488M  7.7M  480M    2% /run
tmpfs                    488M     0  488M    0% /sys/fs/cgroup
/dev/sda1               1014M  130M  885M   13% /boot
/dev/sr0                 8.8G  8.8G     0  100% /mnt
tmpfs                     98M     0   98M    0% /run/user/0
192.168.2.111:/static     17G  2.1G   15G   13% /nfs
​
[root@s1 ~]# touch /static/haha
​
[root@s2 ~]# ls /nfs
haha  test

这篇关于8.19-ansible中模块的使用+playbook的应用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1090504

相关文章

使用Navicat工具比对两个数据库所有表结构的差异案例详解

《使用Navicat工具比对两个数据库所有表结构的差异案例详解》:本文主要介绍如何使用Navicat工具对比两个数据库test_old和test_new,并生成相应的DDLSQL语句,以便将te... 目录概要案例一、如图两个数据库test_old和test_new进行比较:二、开始比较总结概要公司存在多

CSS3中使用flex和grid实现等高元素布局的示例代码

《CSS3中使用flex和grid实现等高元素布局的示例代码》:本文主要介绍了使用CSS3中的Flexbox和Grid布局实现等高元素布局的方法,通过简单的两列实现、每行放置3列以及全部代码的展示,展示了这两种布局方式的实现细节和效果,详细内容请阅读本文,希望能对你有所帮助... 过往的实现方法是使用浮动加

如何使用Spring boot的@Transactional进行事务管理

《如何使用Springboot的@Transactional进行事务管理》这篇文章介绍了SpringBoot中使用@Transactional注解进行声明式事务管理的详细信息,包括基本用法、核心配置... 目录一、前置条件二、基本用法1. 在方法上添加注解2. 在类上添加注解三、核心配置参数1. 传播行为(

在Java中使用ModelMapper简化Shapefile属性转JavaBean实战过程

《在Java中使用ModelMapper简化Shapefile属性转JavaBean实战过程》本文介绍了在Java中使用ModelMapper库简化Shapefile属性转JavaBean的过程,对比... 目录前言一、原始的处理办法1、使用Set方法来转换2、使用构造方法转换二、基于ModelMapper

c++中std::placeholders的使用方法

《c++中std::placeholders的使用方法》std::placeholders是C++标准库中的一个工具,用于在函数对象绑定时创建占位符,本文就来详细的介绍一下,具有一定的参考价值,感兴... 目录1. 基本概念2. 使用场景3. 示例示例 1:部分参数绑定示例 2:参数重排序4. 注意事项5.

使用C++将处理后的信号保存为PNG和TIFF格式

《使用C++将处理后的信号保存为PNG和TIFF格式》在信号处理领域,我们常常需要将处理结果以图像的形式保存下来,方便后续分析和展示,C++提供了多种库来处理图像数据,本文将介绍如何使用stb_ima... 目录1. PNG格式保存使用stb_imagephp_write库1.1 安装和包含库1.2 代码解

一文教你使用Python实现本地分页

《一文教你使用Python实现本地分页》这篇文章主要为大家详细介绍了Python如何实现本地分页的算法,主要针对二级数据结构,文中的示例代码简洁易懂,有需要的小伙伴可以了解下... 在项目开发的过程中,遇到分页的第一页就展示大量的数据,导致前端列表加载展示的速度慢,所以需要在本地加入分页处理,把所有数据先放

Spring Boot Actuator使用说明

《SpringBootActuator使用说明》SpringBootActuator是一个用于监控和管理SpringBoot应用程序的强大工具,通过引入依赖并配置,可以启用默认的监控接口,... 目录项目里引入下面这个依赖使用场景总结说明:本文介绍Spring Boot Actuator的使用,关于Spri

Java中基于注解的代码生成工具MapStruct映射使用详解

《Java中基于注解的代码生成工具MapStruct映射使用详解》MapStruct作为一个基于注解的代码生成工具,为我们提供了一种更加优雅、高效的解决方案,本文主要为大家介绍了它的具体使用,感兴趣... 目录介绍优缺点优点缺点核心注解及详细使用语法说明@Mapper@Mapping@Mappings@Co

使用C++实现单链表的操作与实践

《使用C++实现单链表的操作与实践》在程序设计中,链表是一种常见的数据结构,特别是在动态数据管理、频繁插入和删除元素的场景中,链表相比于数组,具有更高的灵活性和高效性,尤其是在需要频繁修改数据结构的应... 目录一、单链表的基本概念二、单链表类的设计1. 节点的定义2. 链表的类定义三、单链表的操作实现四、