3.Ansible varialbes实战
varialbes
1.什么是变量?
以一个固定的字符串,表示一个不固定的值 version: 1.12
2.定义变量?
1.在playbook中定义变量?
>1.vars 关键字
[root@manager project1]# cat f2.yml
- hosts: webservers
vars:
- file_name: playbook_vars
tasks:
- name: Create New File
file:
path: /tmp/{{ file_name }}
state: touch
>2.vars_file 属于一种共享的方式
1.提前准备变量
[root@manager project1]# cat vars_file.yml
web_packages: httpd
ftp_packages: vsftpd
2.编写剧本
[root@manager project1]# cat f2.yml
- hosts: webservers
vars:
- file_name: playbook_vars
#调用共享vars_file文件,只不过刚好文件名叫vars_file
vars_files: ./vars_file.yml //提前创建好的
tasks:
- name: Create New File
file:
path: /tmp/{{ file_name }}
state: touch
- name: Installed Packages {{ web_packages }}
yum:
name: "{{ web_packages }}" //web_packages: httpd 等于安装httpd
state: present
2.在inventory主机清单中定义变量
>1.清单文件中直接定义 hosts文件定义
[webservers]
172.16.1.7
172.16.1.8
[webservers:vars]
file_name=hostsfile_group_vars
> 2.创建hosts_vars group_vars 目录
[root@manager project1]# mkdir hosts_vars #单个主机
[root@manager project1]# mkdir group_vars #主机组
>>>1.单个主机定义和使用方式 (host_vars能分别对不同的主机定义变量)
[root@manager project1]# cat host_vars/172.16.1.7
host_vars_name: 172.16.1.7
[root@manager project1]# cat host_vars/172.16.1.8
host_vars_name: 172.16.1.8
[root@manager project1]# cat f4.yml
- hosts: webservers
tasks:
- name: Create New File
file:
path: /opt/{{ host_vars_name }}
state: touch
>>>2.针对主机组定义的方式
给指定的webserver组设定变量.其他组主机无法使用该变量
[root@manager project1]# cat group_vars/webservers
group_host_vars: webservers //设定group_host_vars的值为webservers
[root@manager project1]# cat f5.yml
- hosts: webservers
tasks:
- name: Create New File {{ group_host_vars }}
file:
path: /opt/{{ group_host_vars }} //调用变量
state: touch
>>>3.针对主机组定义的方式 (给所有的主机和主机组设定变量)
[root@manager project1]# cat group_vars/all
group_host_vars: all
[root@manager project1]# cat f5.yml
- hosts: webservers
tasks:
- name: Create New File {{ group_host_vars }}
file:
path: /opt/{{ group_host_vars }}
state: touch
>3.通过外置传参定义变量? -e
[root@manager project1]# ansible-playbook -i hosts test5.yml
PLAY [webservers] **************************************************************
TASK [Gathering Facts] *********************************************************
ok: [172.16.1.7]
ok: [172.16.1.8]
TASK [Create New File] *********************************************************
fatal: [172.16.1.7]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'g_vars' is undefined\n\nThe error appears to be in '/project1/test5.yml': line 3, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - name: Create New File\n ^ here\n"}
fatal: [172.16.1.8]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'g_vars' is undefined\n\nThe error appears to be in '/project1/test5.yml': line 3, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - name: Create New File\n ^ here\n"}
PLAY RECAP *********************************************************************
172.16.1.7 : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
172.16.1.8 : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
系统提示找不到定义的变量:这时候使用-e传参
[root@manager project1]# ansible-playbook -i hosts test5.yml -e "web_vars=123"
3.变量冲突,优先级?
1.定义相同的变量不同的值,来测试变量的优先级。操作步骤如下 file_name:
1)在plabook中定义vars变量
2)在playbook中定义vars_files变量
3)在inventory主机定义变量
4)在inventory主机组定义变量
5)在host_vars中定义变量
6)在group_vars中定义变量 组 all组
7)通过执行命令传递变量
优先级测试结果:
外置传入参数优先级最高 ---> playbook ( vars_files(共享)--->vars(私有) )
---> host_vars --> group_vars/group_name ---> group_vars/all
4.变量注册
[root@manager project1]# cat f8.yml
- hosts: webservers
tasks:
# System_Status=$(netstat -lntp) //这里是设定变量的意思
- name: Get Network Status
shell: netstat -lntp | grep "nginx"
register: System_Status //这儿定义的名字必须和下边的msg一样
# echo "$System_Status" //这里是输出结果的意思
- name: Debug output Variables
debug:
msg: "{{ System_Status.stdout_lines }}"
5.facts变量
[root@manager project1]# ansible localhost -m setup -i hosts 查看存放所采集到的信息变量
gather_facts: no 关闭facts变量
1.根据主机的cpu信息,生成不同的配置.
A: 1核心 work_process 1;
B: 2核心 work_process 2;
2.根据主机名称设定不同配置文件
zabbix_agent
Server: ===> 指向172.16.1.61
Hostname: web01 web02
[root@manager project1]# cat ./file/zabbix_agent.conf.j2
Server={{ zabbix_server_ip }}
ServerActive={{ zabbix_server_ip }}
Hostname={{ ansible_hostname }}
[root@manager project1]# cat f11.yml
- hosts: webservers
vars:
- zabbix_server_ip: 172.16.1.61
tasks:
- name: Configure zabbix-agent.conf
template:
src: ./file/zabbix_agent.conf.j2
dest: /tmp/zabbix-agent.conf
3.根据主机的内存生成不同的配置文件,memcached
[root@manager project1]# cat f12.yml
- hosts: webservers
tasks:
- name: Installed Memcached Server
yum:
name: memcached
state: present
- name: Configure Memcached Server
template:
src: ./file/memcached.j2
dest: /etc/sysconfig/memcached
notify: Restart Memcached Server //这儿定义的名称必须要和下边的handlers里的name一致
- name: System Memcached Server
systemd:
name: memcached
state: started
enabled: yes
handlers:
- name: Restart Memcached Server
systemd:
name: memcached
state: restarted
[root@manager project1]# cat file/memcached.j2
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="{{ ansible_memtotal_mb //2 }}" //使用物理内存的2分之一
OPTIONS=""
案例需求:
1.根据cpu
2.根据内存
3.根据主机名
4.Redis配置文件 bind本地地址
5.操作系统不统一
《 变量可以进行运算 + - * // 》
>>>1.定义变量
playbook
vars 私有
vars_files 共享
inventory
host_vars
group_vars
group_vars/group_name
group_vars/all
外置传参
-e //优先级最高
>>>2.测试优先级
在不改变playbook变量的情况下,使用新的值测试.
>>>3.变量注册register
1.将任务执行的结果存储至特定的变量中
2.可以使用debug模块将变量进行打印输出
python: 字典
json 格式化数据
{
k1: v1
k2: v2
}
4.facts ---->>>重点
1.每次都会获取随机数【random】
[root@manager project1]# cat f13.yml
- hosts: webservers
tasks:
- name: RANDOM
shell: echo "$RANDOM"
register: System_SJ
- name: Debug
debug:
msg: "web_{{ System_SJ.stdout }}"
2.提取facts变量中的IP地址 mac地址 UUID 等等 只要唯一
ansible_default_ipv4.address
[root@manager project1]# cat f14.yml
- hosts: webservers
tasks:
- name: Debug
debug:
msg: "web_{{ ansible_default_ipv4.address }}"
6.Ansible 流程控制
1.判断语句
- 1.centos和ubuntu系统都需要安装httpd, 判断系统.
- 2.安装软件仓库,只有web组的安装webtatic其他的主机全部跳过.
- 3.TASK任务, TASK1任务执行成功,才会执行TASK2
1.根据不同的系统,安装不同的服务
- hosts: webservers
tasks:
- name: CentOS Installed Httpd Server
yum:
name: httpd
state: present
when: ( ansible_distribution == "CentOS" )
- name: Ubuntu Installed Httpd Server
yum:
name: httpd2
state: present
when: ( ansible_distribution == "Ubuntu" )
2.根据主机组名安装服务
[root@manager project1]# cat f16.yml
- hosts: all
tasks:
- name: Add Nginx Yum Repository
yum_repository:
name: nginx
description: Nginx Repository
baseurl: http://nginx.org/packages/centos/7/$basearch/
when: ( ansible_hostname is match ("web*"))
3.判断http的状态来重启服务
[root@manager project1]# cat f17.yml
- hosts: webservers
tasks:
- name: Check Httpd Server
command: systemctl is-active httpd
register: Check_Httpd
ignore_errors: yes
#判断Check_Httpd.rc是否等于0,如果为0则执行任务,否则不执行
- name: Restart Httpd Server
systemd:
name: httpd
state: restarted
when: ( Check_Httpd.rc == 0 )
2.循环语句
1.一次启动多个服务
[root@manager project1]# cat f18.yml
- hosts: webservers
tasks:
- name: Systemd Nginx Status
systemd:
name: "{{ item }}" #调用的变量也不变,也是固定
state: started
--->>固定的语法格式:
with_items:
- nginx
- php-fpm
2.一次拷贝多个文件
[root@manager project1]# cat f19.yml
- hosts: webservers
tasks:
- name: Configure nginx.conf
copy:
src: '{{ item.src }}'
dest: '{{ item.dest }}'
mode: '{{ item.mode }}'
with_items:
- { src: ./file/nginx.conf.j2, dest: /etc/nginx/nginx.conf, mode: '0644' }
- { src: ./file/kold.oldxu.com.conf.j2, dest: /etc/nginx/conf.d/kold.oldxu.com.conf, mode: '0600' }
3.创建多个用户,一次创建多个? 3个用户 TASK
方式一:标准循环
[root@manager project1]# cat f20.yml
- hosts: webservers
tasks:
- name: Create User
user:
name: "{{ item }}"
with_items:
- test1
- test2
- test3
- test4
1.创建tt1 --> bin tt2 -->root tt3 --->adm 附加组
[root@manager project1]# cat f20.yml
- hosts: webservers
tasks:
- name: Create User
user:
name: "{{ item.name }}"
groups: "{{ item.groups }}"
with_items:
- { name: tt1, groups: bin }
- { name: tt2, groups: root }
- { name: tt3, groups: adm }
1.标准循环 --->居多
item
with_items:
- test
2.字典循环: --->居多
itme.name
with_items:
- { name: test }
3.变量循环
- hosts: webservers
tasks:
- name: ensure a list of packages installed
yum: name={{ packages }} state=present
vars:
packages:
- httpd
- httpd-tools
3.handlers
--->>handlers实践:
[root@manager project1]# cat f22.yml
- hosts: webservers
tasks:
- name: Installed Nginx and PHP Packages
yum:
name: nginx
state: present
- name: Configure nginx.conf
template:
src: ./file/nginx.conf.j2
dest: /etc/nginx/nginx.conf
#监控-->changed状态-->通知-->handlers--->name-->Restart Nginx Server
notify: Restart Nginx Server
#notify:
# - Restart Nginx Server
# - Restart php Server
- name: Systemd Nginx Server
systemd:
name: nginx
state: started
enabled: yes
当nginx或php配置文件发生变更才会触发此操作
handlers:
- name: Restart Nginx Server
systemd:
name: nginx
state: restarted
3.handlers注意事项
1.无论多少个task通知了相同的handlers,handlers仅会在所有tasks结束后运行一次。
2.只有task发生改变了才会通知handlers,没有改变则不会触发handlers.
3.不能使用handlers替代tasks、因为handlers是一个特殊的tasks。
4.varialbes实战
变量->facts-->判断-->循环
1.安装Rsyncd服务 (循环)
[root@manager project1]# cat rsync.yml
- hosts: backupservers
tasks:
- name: Installed Rsync Server
yum:
name: rsync
state: present
- name: Configure Rsync Server
copy:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
mode: "{{ item.mode }}"
with_items:
- { src: ./file/rsyncd.conf.j2, dest: /etc/rsyncd.conf, mode: '0644' }
- { src: ./file/rsync.passwd.j2, dest: /etc/rsync.passwd, mode: '0600' }
notify:
- Restart Rsync Server
- name: Create Group
group:
name: www
gid: 666
- name: Create User
user:
name: www
uid: 666
group: www
create_home: no
- name: Create Directory
file:
path: "/{{ item }}"
state: directory
owner: www
group: www
recurse: yes
with_items:
- backup
- name: Systemd Rsync Server
systemd:
name: rsyncd
state: started
enabled: yes
- name: check_rsync_status
shell: netstat -lntp |grep 873
register: rsync_status
ignore_errors: yes
- name: rsync_status server
debug:
msg: "{{ rsync_status.stdout_lines }}"
handlers:
- name: Restart Rsync Server
systemd:
name: rsyncd
state: restarted
2.安装Redis (bind 本地IP地址) facts
[root@manager project1]# cat redis.yml
- hosts: redis
tasks:
- name: Installed Redis Server
yum:
name: redis
state: present
- name: Configure redis.conf
template:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
mode: "{{ item.mode }}"
with_items:
- { src: ./file/redis.conf, dest: /etc/redis.conf, mode: '0640' }
notify:
- Restart redis server
- name: Systemd start redis
systemd:
name: redis
state: started
enabled: yes
- name: check_redis_status
shell: netstat -lntp |grep redis
register: systemctl_status_redis
- name: redis_status server
debug:
msg: "{{ systemctl_status_redis.stdout_lines }}"
handlers:
- name: Restart redis server
systemd:
name: redis
state: restarted
3.安装NFS (配置文件,创建目录,客户端挂载) 变量
服务端剧本:
[root@manager project1]# cat nfs_server.yml
- hosts: nfs
vars_files: ./vars_file.yml
tasks:
- name: Installed NFS Server
yum:
name: nfs-utils
state: present
- name: Configure NFS Server
template:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
mode: "{{ item.mode }}"
with_items:
- { src: ./file/exports.j2, dest: /etc/exports, mode: '0644' }
notify:
- Restart NFS Server
- name: Configure Group
group:
name: www
gid: 666
- name: Configure User
user:
name: www
uid: 666
group: www
create_home: no
shell: /sbin/nologin
- name: Create NFS shard directory
file:
path: "{{ mount }}"
state: directory
owner: www
group: www
recurse: yes
- name: Systemd NFS Server
systemd:
name: nfs
state: started
enabled: yes
handlers:
- name: Restart NFS Server
systemd:
name: nfs
state: restarted
enabled: yes
客户端剧本:
[root@manager project1]# cat nfs_client.yml
- hosts: backup
vars_files: ./vars_file.yml
tasks:
- name: Installed nfs server
yum:
name: nfs-utils
state: present
- name: Mount nfs zh
mount:
src: "{{ nfs_server_ip }}:{{ mount }}"
path: /mnt
fstype: nfs
opts: defaults
state: mounted
总结:
- 1.变量
- 2.facts
- 3.register
- 4.when 判断语句 ( facts )
- 5.with_items 循环
- 1.标准循环
- 2.字典循环
- 3.变量循环(忽略)
3.Ansible varialbes实战的更多相关文章
- Ansible 批处理实战
软件简介 Ansible 是一款自动化运维工具,基于 Python 开发,集合了众多运维工具(puppet.chef.func.fabric)的优点,实现了批量系统配置.批量程序部署.批量运行命令等功 ...
- Ansible学习实战手记-你想要知道的可能都在这里了
最近接触了ansible工具,查找了一些资料,也做了一些总结.希望能给刚接触的新手带来一些帮助. 此总结有实际例子,大部分也是从实践中用到才逐一总结的. 当然可能肯定一定会存在一些错误和纰漏,还望大家 ...
- ansible最佳实战部署nginx
1.先看下整体目录架构 [root@bogon ~]# cd /etc/ansible/ [root@bogon ansible]# tree . ├── ansible.cfg ├── group_ ...
- Ansible快速实战指南----多机自动化执行命令、部署神器
1.需求: 需要在多台主机上,发送文件.执行命令,进行快速部署 2.ansible 远程复制文件 例子:在当前节点(20.88.14 ...
- Ansible varialbes
1.什么是变量? 以一个固定的字符串,表示一个不固定的值 version: 1.12 2.定义变量? 1.在playbook中定义变量? vars 关键字 [root@manager projec ...
- Ansible性能调优
Ansible企业实战环境中,如果管理的服务器越来越多,Ansibe执行效率会变得比较慢,可以通过优化Ansible提供工作效率,由于Ansible基于SSH协议通信,SSH连接慢会导致整个基于Ans ...
- ansible的优化
Ansible企业实战环境中,如果管理的服务器越来越多,Ansibe执行效率会变得比较慢,可以通过优化Ansible提供工作效率,由于Ansible基于SSH协议通信,SSH连接慢会导致整个基于Ans ...
- Ansible--项目实战
Ansible项目实战lnmp 项目规划 通过ansible roles配置lnmp环境,nginx通过源码编译安装,php通过源码编译安装,mysql通过yum安装(mysql源码编译超级慢)支持系 ...
- ansible自动化部署之场景应用
ansible自动化配置管理 官方网站: https://docs.ansible.com 一.安装 配置 启动 (ansible由红帽收购) (1)什么是ansible ansible是IT自动化配 ...
随机推荐
- #华为云·寻找黑马程序员#微服务-你真的懂 Yaml 吗?
在Java 的世界里,配置的事情都交给了 Properties,要追溯起来这个模块还是从古老的JDK1.0 就开始了的. "天哪,这可是20年前的东西了,我居然还在用 Properties. ...
- MongoDB一次节点宕机引发的思考(源码剖析)【华为云分享】
目录 简介 日志分析 副本集 如何实现 Failover 心跳的实现 electionTimeout 定时器 业务影响评估 参考链接 声明:本文同步发表于 MongoDB 中文社区,传送门:http: ...
- .NET Core简单使用RabbitMq
RabbitMQ简介 RabbitMQ是一个开源的,基于AMQP(Advanced Message Queuing Protocol)协议的完整的可复用的企业级消息队,RabbitMQ可以实现点对点, ...
- org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'ExtractAbstractServiceHandler' is defined
在利用 Spring 集成 thrift 时,报错如下: javax.servlet.ServletException: Servlet.init() for servlet search-nlp-s ...
- postman tests常用方法
postman常用方法集合: 1.设置环境变量 postman.setEnvironmentVariable("key", "value"); pm.envir ...
- jQuery中的属性选择器
先看代码,后面详细解释: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...
- 改变SecureCRT的背景颜色
1.在使用secureCRT客户端时,可以连接服务器,默认为白色底. 2.要进行对把底色的白色改为黑色的底色,右击的窗口的位置. 3.下拉菜单中点击 Session Options 4.点击Appea ...
- Navicat for mysql 免费破解工具+教程
转载自:https://blog.csdn.net/BigCabbageFy/article/details/79789833 navicat for mysql带给大家这款高性能数据库管理开发工具的 ...
- JVM系列三(垃圾收集器).
一.概述 1. 哪些内存需要回收 上篇文章 我们介绍了 Java 内存运行时区域的各个部分,其中程序计数器.虚拟机栈.本地方法栈三个区域随线程而生,随线程而灭,在这几个区域内就不需要过多考虑回收的问题 ...
- [从今天开始修炼数据结构]图的最小生成树 —— 最清楚易懂的Prim算法和kruskal算法讲解和实现
接上文,研究了一下算法之后,发现大话数据结构的代码风格更适合与前文中邻接矩阵的定义相关联,所以硬着头皮把大话中的最小生成树用自己的话整理了一下,希望大家能够看懂. 一.最小生成树 1,问题 最小生成树 ...