水果网站建设方案,广州做礼物的网站,wordpress反应,淄博怎么做网站一 inventory 主机清单
Inventory支持对主机进行分组#xff0c;每个组内可以定义多个主机#xff0c;每个主机都可以定义在任何一个或
多个主机组内。
如果是名称类似的主机#xff0c;可以使用列表的方式标识各个主机。vim /etc/ansible/hosts[webservers]192.168.10.1…一 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.6192.168.11.12192.168.11.13192.168.11.7
关闭 防火墙 防护 实验 1 修改端口
连接对方端口号 [root11-6 ansible]# vim hosts [rootmcb-11-7 ~]# vim /etc/ssh/sshd_config 验证一下 实验 2 连接端口另一个写法
[webservers]192.168.11.16 ansible_port22 ansible_userroot ansible_passwordabc1234#加端口 加用户 加密码 [root11-6 ansible]# vim hosts 检测一下 2 组变量
[webservers:vars] #表示为 webservers 组内所有主机定义变量
ansible_userroot
ansible_passwordabc1234[all:vars] #表示为所有组内的所有主机定义变量
ansible_port22 [root11-6 ansible]# vim hosts 修改被控制端口1116 ansible主机检测 3 组嵌套 [root11-6 ansible]# vim hosts [root11-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 各部分组成
1Tasks任务即通过 task 调用 ansible 的模板将多个操作组织在一个 playbook 中运行2Variables变量3Templates模板4Handlers处理器当changed状态条件满足时notify触发执行的操作5Roles角色
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模块无需使用keyvalue格式ignore_errors: True #如执行命令的返回值不为0就会报错tasks停止可使用ignore_errors忽略失败的任务name: disable firewalld service: namefirewalld statestopped #使用 module: options 格式来定义任务option使用keyvalue格式name: install httpd yum: namehttpd statelatestname: 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: enabledtrue namehttpd statestarted handlers: #handlers中定义的就是任务此处handlers中的任务使用的是service模块name: restart httpd #notify和handlers中任务的名称必须一致 service: namehttpd staterestarted##Ansible在执行完某个任务之后并不会立即去执行对应的handler而是在当前play中所有普通任务都执行完后再去执行handler这样的好处是可以多次触发notify但最后只执行一次对应的handler从而避免多次重启。
注意
每一个名字就是一个模块
4 实验自动安装apache [root11-6 opt]# vim mcb1.yaml ---
- name: install httpdgather_facts: allhosts: mcbweb01remote_user: roottasks:- name: mcb1 connectionping:- name: disable firewalldservice: namefirewalld statestopped- name: install apacheyum: namehttpd statelatest- name: install config filecopy: src/opt/httpd.conf dest/etc/httpd/conf/httpd.confnotify: restart httpd- name: start httpd serviceservice: enabledtrue namehttpd statestartedhandlers:- name: restart httpdservice: namehttpd staterestarted运行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-taskinstall apache指定从某个task开始运行 主机检测一下 三 playbook定义、引用变量
[root11-6 opt]# vim mcb2.yaml
---
- name: yin yong bianlianghosts: mcb02remote_user: rootvars:- groupname: ky38- username: nginxtasks:- name: create groupgroup: name{{groupname}} systemyes gid305- name: create useruser: name{{username}} uid305 group{{groupname}}- name: copy filecopy: content{{ansible_default_ipv4}} dest/opt/ky38.txt在命令行里定义变量 [root11-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批量创建目录 [root11-6 opt]# vim mcb4.yaml ---
- name: mcb4hosts: mcbweb01tasks:- name: create dirfile: path{{item}} statedirectorywith_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}} statepresent groups{{item.groups}}with_items:- name: test1groups: wheel- name: test2groups: root检测一下