ansible之基础篇(三)
2024-10-20 05:48:51
setup
ansible_all_ipv4_addresses # ipv4的所有地址
ansible_all_ipv6_addresses # ipv6的所有地址
ansible_date_time # 获取到控制节点时间
ansible_default_ipv4 # 默认的ipv4地址
ansible_distribution # 系统
ansible_distribution_major_version # 系统的大版本
ansible_distribution_version # 系统的版本号
ansible_domain #系统所在的域
ansible_env #系统的环境变量
ansible_hostname #系统的主机名
ansible_fqdn #系统的全名
ansible_machine #系统的架构
ansible_memory_mb #系统的内存信息
ansible_os_family # 系统的家族
ansible_pkg_mgr # 系统的包管理工具
ansible_processor_cores #系统的cpu的核数(每颗)
ansible_processor_count #系统cpu的颗数
ansible_processor_vcpus #系统cpu的总个数=cpu的颗数*CPU的核数
ansible_python # 系统上的python
ansible cache -m setup -a 'filter=*processor*' # 用来搜索
正则回顾:
* 匹配数量,表示0或者多次 ? 匹配数量,表示0或者1次 . 除换行符以外的所有字符 + 至少一次 [123abc] 匹配内容,or () 分组 {m} 次数,出现m次 {m,} 至少m次 {m,n}出现m-n次 a*.b 与 a.*b # 第一个是0个或多个a +任意 +b 第二个是 a + 任意的0到多个 + b
条件判断
不同的系统
不同的版本
不同的环境
不同的用户
- hosts: db
remote_user: root
tasks:
- name: createfile
copy: content="大弦嘈嘈如急雨" dest=/tmp/a.txt
when: a==""
- name: cratefile
copy: content="小弦切切如私语" dest=/tmp/a.txt
when: a==""
Ubuntu 安装包的方式是apt-get
tags
- hosts: db
tasks:
- name: wadong
tieqiao: wadong
- name: tk
dong: tk
tags: tk
- hosts: web
tasks:
- name: installnginx
yum: name=nginx
- name: copyfile
copy: src=/etc/nginx/nginx.conf dest=/etc/nginx/nginx.conf
tags: copyfile
- name: start
service: name=nginx state=started ansible-playbook -t copyfile p7.yml # 为任务打上标签只有标签中的任务会执行
循环 with_item
一次性创建多个
- hosts: gb
tasks:
- name: gbqc
dong: {{ item }}
with_items:
- qbqc
- cyf
- ssfj - hosts: web
tasks:
- name: crateuser
user: name={{item}}
with_items:
- alex20
- alex21
- alex22
~
- hosts: web
tasks:
- name: crateuser
user: name={{item}}
with_items:
- alex30
- alex31
- alex32
- name: crategroup
group: name={{item}}
with_items:
- wulaoshi20
- wulaoshi21
- wulaoshi22
~
嵌套循环
- hosts: web
tasks:
- name: crategroup
group: name={{item}}
with_items:
- wulaoshi30
- wulaoshi31
- wulaoshi32
- name: createuser
user: name={{item.name}} group={{item.group}}
with_items:
- {'name':alex40,'group':wulaoshi30}
- {'name':alex41,'group':wulaoshi31}
- {'name':alex42,'group':wulaoshi32}
template:
jinja2
- hosts: web
tasks:
- name: installredis
yum: name=redis
- name: copyfile
template: src=/etc/redis.conf dest=/etc/redis.conf
- name: start
service: name=redis state=started
配置文件: bind {{ ansible_default_ipv4.address }}
copy和tamplate的区别
copy模块不替代参数
template模块替代参数
- hosts: web
tasks:
- name: installredis
yum: name=redis
- name: copyfile
template: src=redis.conf dest=/etc/redis.conf
- name: start
service: name=redis state=started
ps:写相对路径: 在当前目录下新建一个templates目录,然后把文件放在templates目录里面
handlers
不会执行handlers 下的 name 如果需要执行需要加入参数notify: name
- hosts: web
tasks:
- name: installredis
yum: name=redis
- name: copyfile
template: src=redis.conf dest=/etc/redis.conf
tags: copyfile
notify: restart
- name: start
service: name=redis state=started
handlers:
- name: restart
service: name=redis state=restarted
roles
目录清晰
可以互相调用
roles文件夹
文件夹里面是要创建的每一个角色,每一个角色一个文件夹每一个角色里面都有tasks(必须的),templates,files,handlers,vars目录,每个目录都要有main.yml文件,通过import_tasks来调用
其中templates文件夹中的文件可以通过相对路径来调用,其中files文件夹中的文件是否可以通过相对路径来调用?
创建如上文件夹,我们设置的任务文件写在tasks文件夹下
需要住的是文件夹下必须要有主文件,main.yml
install.yml文件:
web.yml文件 可以创建在任意位置 格式如下:
服务配置:
setenforce 0 #用来临时关闭selinux
iptables -F # 临时关闭防火墙
/etc/selinux/config # 永久关闭
最新文章
- Linux入门之路
- delphi 中如何查看网页POSTDATA
- ViewPager+tab+Fragment的滑动
- HTML5优点
- Scrapy中的item是什么
- offsetLeft,Left,clientLeft的区别
- 迷宫问题python实现(靠右手摸墙)
- 【NO.3-1】Jmeter - 在Windows配置HOSTS的方法
- JS解析JSON 注意事项总结
- hyperscan在低版本系统应用问题
- Code First的实体继承模式
- 《深入浅出nodejs》读书笔记(2)
- 【Java】 剑指offer(42) 连续子数组的最大和
- Expo大作战(二十)--expo中的Release channels(不重要*)
- BZOJ 2440 完全平方数 莫比乌斯反演模板题
- Python科学计算库灬numpy
- visio扩大画布的大小
- linux配置Nginx启动,停止
- Linux 去重 先sort再uniq
- hihoCoder #1872 : Pythagorean triple
热门文章
- WebGL学习笔记(五):变换库
- Could not get JDBC Connection; nested exception is java.sql.SQLException: ${jdbc.driver}
- 【SpringBoot】SpringBoot快速入门(一)
- 【linux】CentOS: Sudo: unable to initialize policy plugin
- [LeetCode] 636. Exclusive Time of Functions 函数的独家时间
- Oracle current redo.log出现坏块后的不完全恢复案例一则
- 在 Mac 系统下安装 PyCharm 的方法
- Linux定时自动备份oracle数据库
- LeetCode 653. 两数之和 IV - 输入 BST(Two Sum IV - Input is a BST)
- Callable+Future