当前位置: 首页 > news >正文

Ansible --- playbook 脚本+inventory 主机清单

一 inventory 主机清单

Inventory支持对主机进行分组,每个组内可以定义多个主机,每个主机都可以定义在任何一个或

多个主机组内。

如果是名称类似的主机,可以使用列表的方式标识各个主机。vim /etc/ansible/hosts[webservers]192.168.10.14:2222        #冒号后定义远程连接端口,默认是 ssh 的 22 端口192.168.10.1[2:5][dbservers]db-[a:f].example.org    #支持匹配 a~f

1 inventory 中变量

Inventory变量名                  含义
ansible_host                  ansible连接节点时的IP地址
ansible_port                  连接对方的端口号,ssh连接时默认为22
ansible_user                  连接对方主机时使用的主机名。不指定时,将使用执行ansible或ansible-playbook命令的用户
ansible_password              连接时的用户的ssh密码,仅在未使用密钥对验证的情况下有效
ansible_ssh_private_key_file  指定密钥认证ssh连接时的私钥文件
ansible_ssh_common_args       提供给ssh、sftp、scp命令的额外参数
ansible_become                允许进行权限提升
ansible_become_method         指定提升权限的方式,例如可使用sudo/su/runas等方式
ansible_become_user           提升为哪个用户的权限,默认提升为root
ansible_become_password       提升为指定用户权限时的密码

 环境准备:

192.168.11.6
192.168.11.12
192.168.11.13
192.168.11.7

关闭 防火墙 防护 

 

实验 1 :修改端口

连接对方端口号 

[root@11-6 ansible]# vim hosts

[root@mcb-11-7 ~]# vim  /etc/ssh/sshd_config 

验证一下:

实验 2 :连接端口另一个写法 
[webservers]192.168.11.16 ansible_port=22 ansible_user=root ansible_password=abc1234#加端口  加用户  加密码

[root@11-6 ansible]# vim hosts 

检测一下:

2 组变量 

[webservers:vars]			#表示为 webservers 组内所有主机定义变量
ansible_user=root
ansible_password=abc1234[all:vars]					#表示为所有组内的所有主机定义变量
ansible_port=22

[root@11-6 ansible]# vim hosts 

修改被控制端口1116 

ansible主机检测 

3 组嵌套

[root@11-6 ansible]# vim hosts

[root@11-6 ansible]# ansible onlys -m ping 

检测错误:

 二 Ansible 的脚本 --- playbook 剧本

1 playbook格式

playbook由YMAL语言编写。YMAL格式是类似于JSON的文件格式,便于人理解和阅读,同时便于

书写。一个剧本里面可以有多个play,每个play只能有一个tasks,每个tasks可以有多个name。

yaml三板斧
1、缩进:yaml使用一个固定的缩进风格表示层级结构,每个缩进由两个空格组成,不能使用tab键2、冒号: 以冒号结尾的除外,其他所有冒号后面所有必须有空格。3、短横线:表示列表项,使用一个短横线加一个空格作为一个列表项,多个项使用同样的缩进级别作为同一列表。

2 playbooks 各部分组成

(1)Tasks:任务,即通过 task 调用 ansible 的模板将多个操作组织在一个 playbook 中运行(2)Variables:变量(3)Templates:模板(4)Handlers:处理器,当changed状态条件满足时,(notify)触发执行的操作(5)Roles:角色

3 示例诠释:

常用属性: 
vim test1.yaml --- #yaml文件以---开头,以表明这是一个yaml文件,可省略name: first play #定义一个play的名称,可省略gather_facts: false #设置不进行facts信息收集,这可以加快执行速度,可省略hosts: webservers #指定要执行任务的被管理主机组,如多个主机组用冒号分隔remote_user: root #指定被管理主机上执行任务的用户tasks: #定义任务列表,任务列表中的各任务按次序逐个在hosts中指定的主机上执行
编辑剧本: 
name: test connection #自定义任务名称ping:  #使用 module: [options] 格式来定义一个任务name: disable selinuxcommand: '/sbin/setenforce 0' #command模块和shell模块无需使用key=value格式ignore_errors: True #如执行命令的返回值不为0,就会报错,tasks停止,可使用ignore_errors忽略失败的任务name: disable firewalld service: name=firewalld state=stopped #使用 module: options 格式来定义任务,option使用key=value格式name: install httpd yum: name=httpd state=latestname: install configuration file for httpd copy: src=/opt/httpd.conf dest=/etc/httpd/conf/httpd.conf#这里需要一个事先准备好的/opt/httpd.conf文件 notify: "restart httpd" #如以上操作后为changed的状态时,会通过notify指定的名称触发对应名称的handlers操作name: start httpd service service: enabled=true name=httpd state=started handlers: #handlers中定义的就是任务,此处handlers中的任务使用的是service模块name: restart httpd #notify和handlers中任务的名称必须一致 service: name=httpd state=restarted##Ansible在执行完某个任务之后并不会立即去执行对应的handler,而是在当前play中所有普通任务都执行完后再去执行handler,这样的好处是可以多次触发notify,但最后只执行一次对应的handler,从而避免多次重启。

注意:

每一个名字就是一个模块

4 实验:自动安装apache

[root@11-6 opt]# vim mcb1.yaml

---
- name: install httpdgather_facts: allhosts: mcbweb01remote_user: roottasks:- name: mcb1 connectionping:- name: disable firewalldservice: name=firewalld state=stopped- name: install apacheyum: name=httpd state=latest- name: install config filecopy: src=/opt/httpd.conf dest=/etc/httpd/conf/httpd.confnotify: "restart httpd"- name: start httpd serviceservice: enabled=true name=httpd state=startedhandlers:- name: "restart httpd"service:  name=httpd state=restarted
运行playbook

ansible-playbook test1.yaml

错误分析:

补充参数:
-k(–ask-pass):用来交互输入ssh密码-K(-ask-become-pass):用来交互输入sudo密码-u:指定用户 
ansible-playbook test1.yaml --syntax-check#检查yaml文件的语法是否正确

②ansible-playbook test1.yaml --list-task#检查tasks任务


 

ansible-playbook test1.yaml --list-hosts检查生效的主机

 

ansible-playbook mcb1.yaml --start-at-task='install apache'指定从某个task开始运行

主机检测一下:

三 playbook定义、引用变量

[root@11-6 opt]# vim mcb2.yaml
---
- name: yin yong bianlianghosts: mcb02remote_user: rootvars:- groupname: ky38- username: nginxtasks:- name: create groupgroup: name={{groupname}} system=yes gid=305- name: create useruser: name={{username}} uid=305 group={{groupname}}- name: copy filecopy: content="{{ansible_default_ipv4}}" dest=/opt/ky38.txt

在命令行里定义变量  

[root@11-6 opt]# ansible-playbook mcb2.yaml -e "mcb" 

检测:

报错信息:

四 指定远程主机sudo切换用户

 hosts: dbservers

remote_user: zhangsan

become: yes #2.6版本以后的参数,之前是sudo,意思为切换用户运行

become_user: root #指定sudo用户为root

执行playbook时:ansible-playbook test1.yml -K <密码>

1 当ssh不允许root用户登录时 打开普通用户sudo提权

编写脚本

2 修改sudoers加入配置文件

 

 给主机11.3 mcb做免密登录

五 when条件判断

在Ansible中,提供的唯一一个通用的条件判断是when指令,当when指令的值为

true时,则该任务执行,否则不执行该任务。

when一个比较常见的应用场景是实现跳过某个主机不执行任务或者只有满足条件的主机执行任务

1 调用command模块进行shutdown服务  

---
- name: restart host hosts: mcbweb01remote_user: roottasks:- name: shutdown hostcommand: /sbin/shutdown -r nowwhen: ansible_default_ipv4.address == "192.168.11.12"
 查看结果:

2 调用service模块关闭httpd服务 

六 迭代

Ansible提供了很多种循环结构,一般都命名为with_items,作用等同于 loop 循环。  

实验 1:批量创建目录 

[root@11-6 opt]# vim mcb4.yaml 

---
- name: mcb4hosts: mcbweb01tasks:- name: create dirfile: path={{item}} state=directorywith_items:          - /opt/heze- /opt/dingtao- /opt/shandong
 检测一下:

实验 2: 批量创建文件

---
- name: play1hosts: mcbweb01gather_facts: falsetasks: - name: create directoriesfile:path: "{{item}}"state: directorywith_items:          - /tmp/test1- /tmp/test2- name: add usersuser: name={{item.name}} state=present groups={{item.groups}}with_items:- name: test1groups: wheel- name: test2groups: root
 检测一下:

http://www.lryc.cn/news/346977.html

相关文章:

  • 【hive】transform脚本
  • 5款可用于LLMs的爬虫工具/方案
  • 投影、选择转SQL语言
  • 系统加固-自用
  • Java面试题:阐述Java中的自动装箱与拆箱机制,以及使用它们时可能遇到的性能问题
  • 初识sql注入--手工注入
  • OceanBase 缺少 dbms_obfuscation_toolkit.md5 包函数的解决方案
  • Java---类和对象第一节
  • Zeller公式的应用:给定日期,确定周几
  • 程序链接和运行 - 笔记
  • pyqt 按钮常用格式Qss设置
  • websevere服务器从零搭建到上线(一)|阻塞、非阻塞、同步、异步
  • 【C++】引用传递 常量引用
  • Docker停止不了
  • 【网络】为什么TCP需要四次挥手?
  • 2024自动化测试市场分析
  • 什么是机器视觉应用解决方案?
  • 使用 scrapyd 部署 scrapy
  • Python计算器程序代码
  • 图像分割各种算子算法-可直接使用(Canny、Roberts、Sobel)
  • Spring Boot进阶 - 实现自动装配原理
  • 面向电商家居行业3D室内场景合成中的空间感知
  • ERROR 1045 (28000) Access denied for user ‘root‘@‘IP‘(using password YES/NO)
  • verilog $test$plusargs和$value$plusargs
  • Linux设置open files
  • Linux下安装JDK并配置环境变量
  • 擎天科技与禅道合作,打造统一的项目管理平台
  • ansible报错解决:Failed to import the required Python library (netaddr)
  • 盛邦安全拟战略收购卫星通信加密厂商天御云安
  • iOS MRC那句话