ansible批量搞机之ansible-playbook 概述;远程发布;远程执行(二),
ansible批量搞机之ansible-playbook 概述;远程发布;远程执行(二),
序:接上一篇模块后,这一篇主要介绍playbook的用法
一、ansible七种武器
• 第一种武器
– ansible 命令,用于执行临时性的工作,也是我们之前
主要学习的功能,必须掌握
• 第二种武器
– ansible-doc 是 Ansible模块文档说明,针对每个模块
都有详细的用法说明及应用案例介终,功能和Linux系
统man命令类似,必须掌握
• 第三种武器
– ansible-console 是 Ansible 为用户提供的一款交互
式工具,用户可以在 ansible-console 虚拟出来的终
端上像 Shell 一样使用 Ansible 内置的各种命令,
这为习惯亍使用 Shell 交互方式的用户提供了良好的
使用体验。
• 第四种武器
– ansible-galaxy 从 github 上下载管理 Roles 的一款
工具,不 python 的 pip 类似。ansible七种武器
• 第五种武器
– ansible-playbook 是日常应用中使用频率最高的命令,
其工作机制是:通过读叏预先编写好的 playbook 文
件实现批量管理。要实现的功能不命令 ansible 一样,
可以理解为按一定条件组成的 ansible 任务集,必须掌握
• 第六种武器
– ansible-vault 主要用亍配置文件加密,如编写的
Playbook 配置文件中包含敏感信息,丌希望其他人随
意查看, ansible-vault 可加密/解密这个配置文件ansible七种武器
• 第七种武器
– ansible-pull
– Ansible 有两种工作模式 pull/push ,默认使用 push
模式工作,pull 模式和通常使用的 push 模式工作机理刚好相反
– 适用场景:有数量巨大的机器需要配置,即使使用高
幵収线程依旧要花费很多时间;
– 通常在配置大批量机器的场景下会使用,灵活性稍有
欠缺,但效率几乎可以无限提升,对运维人员的技术
水平和前瞻性规划有较高要求。
json简介
• json 是什么?
– json 是 JavaScript 对象表示法,它是一种基于文本,
独立于诧言的轻量级数据交换格式。
– JSON中的分隔符限于单引号 ' 、小括号 ()、中括号
[ ]、大括号 { } 、冒号 : 和逗号 ,
• json 特性
– JSON 是纯文本
– JSON 具有"自我描述性"(人类可读)
– JSON 具有层级结构(值中存在值)
– JSON 可通过 JavaScript 进行解析
{ "讲师":
[ {"牛犇":"小逗逼", "负责阶段":"1"},
{"丁丁":"老逗逼", "负责阶段":"2"},
{"静静":"漂亮姐", "负责阶段":"3"},
{"李欣":"老司机", "负责阶段":"4"}
]
}
yaml简介
• yaml 基础语法
– YAML的结构通过空格杢展示
– 数组使用"- "来表示
– 键值对使用": "来表示
– YAML使用一个固定的缩进风格表示数据层级结构关系
– 一般每个缩进级别由两个以上空格组成
– # 表示注释
• 注意:
– 不要使用tab,缩迚是初学者容易出错的地方之一
– 同一层级缩迚必须对齐
playbook语法基础
• playbook 诧法格式
– playbook由 YAML 语言编写,遵循 YAML 标准
– 在同一行中,#之后的内容表示注释
– 同一个列表中的元素应该保持相同的缩迚
– playbook 由一个或多个 play 组成
– play 中 hosts,variables,roles,tasks 等对象的表示
方法都是键值中间以 ": " 分隔表示
– YAML 还有一个小的怪癖. 所有的 YAML 文件开始行都
应该是 ---. 这是 YAML 格式的一部分, 表明一个文件的开始
二、具体的实例
1.编写一个ping检查ssh连通性
[root@ansible1 ansible]# vim ping.yml
---
- hosts: web1,db1
remote_user: root
tasks:
- ping:
[root@ansible1 ansible]# ansible-playbook --syntax-check ping.yml //可以检查语法是否有错误
[root@ansible1 ansible]# ansible-playbook --syntax-check ping.yml //执行yml
2.编写一个playbook要求创建mj用户并在第一次登陆时修改密码
[root@ansible1 ansible]# vim user01.yml
---
- hosts: web
remote_user: root
tasks:
- name: create user mj
shell: useradd mj
- shell: echo 123456 | passwd --stdin mj
- shell: chage -d 0 mj
[root@ansible1 ansible]# ansible-playbook user01.yml
3.编写一httpd的playbook(要求修改默认的端口和ServerName以及修改默认主页)
在当前目录下面准备一个index.html文件
playbook httpd.yml
---
- hosts: web
remote_user: root
tasks:
- name: install the latest version of Apache
yum:
name: httpd
state: installed
- lineinfile:
path: /etc/httpd/conf/httpd.conf
regexp: '^Listen '
insertafter: '^#Listen '
line: 'Listen 8080'
- lineinfile:
path: /etc/httpd/conf/httpd.conf
regexp: '^#ServerName'
line: 'ServerName localhost'
- copy:
src: /root/index.html
dest: /var/www/html/index.html
owner: apache
group: apache
mode: 0644
- service:
name: httpd
state: started
enabled: yes
4. user02.yml 变量
---
- hosts: db1,web1
remote_user: root
vars:
username: "jj"
tasks:
- user:
name: "{{username}}"
- shell: echo 123456 |passwd --stdin "{{username}}"
- shell: chage -d 0 "{{username}}"
5. user03.yml 过滤器
---
- hosts: cache
remote_user: root
vars:
username: "jj"
tasks:
- user:
name: "{{username}}"
password: "{{'123456'|password_hash('sha512')}}"
- shell: chage -d 0 "{{username}}"
6. user04.yml 忽略错误
---
- hosts: web1
remote_user: root
vars:
username: "zhang3"
tasks:
- shell: adduser "{{username}}"
ignore_errors: True
- shell: echo 123123 |passwd --stdin "{{username}}"
- shell: chage -d 0 "{{username}}"
7. http02.yml handlers 使用
---
- hosts: cache
remote_user: root
tasks:
- name: install the latest version of Apache
yum:
name: httpd
state: installed
notify:
- boot start
- restart httpd
- lineinfile:
path: /etc/httpd/conf/httpd.conf
regexp: '^Listen '
insertafter: '^#Listen '
line: 'Listen 8080'
notify:
- restart httpd
- lineinfile:
path: /etc/httpd/conf/httpd.conf
regexp: '^#ServerName'
line: 'ServerName localhost'
notify:
- restart httpd
- copy:
src: index.html
dest: /var/www/html/index.html
owner: apache
group: apache
mode: 0644
handlers:
- name: restart httpd
service:
name: httpd
state: restarted
- name: boot start
service:
name: httpd
enabled: yes
8. 把负载高的web服务器停止
---
- hosts: web
remote_user: root
tasks:
- shell: uptime |awk '{printf("%.2f",$(NF-2))}'
register: result
- service: name=httpd state=stopped
when: result.stdout|float > 0.7
追加 debug 调试信息
- name: Show debug info
debug: var=ooxx
测试命令 awk 'BEGIN{while(1){}}'
9. 循环添加多用户
---
- hosts: db2
remote_user: root
tasks:
- name: create user
user:
name: "{{item.name}}"
password: "{{item.pwd|password_hash('sha512')}}"
group: "{{item.group}}"
with_items:
- { name: "nb", pwd: "aa", group: "users" }
- { name: "dd", pwd: "bb", group: "mail" }
- { name: "jj", pwd: "cc", group: "wheel" }
- { name: "xx", pwd: "dd", group: "root" }
10. 嵌套循环
---
- hosts: cache
remote_user: root
vars:
un: [a, b, c]
id: [1, 2, 3]
tasks:
- name: add users
shell: echo {{item}}
with_nested:
- "{{un}}"
- "{{id}}"
评论暂时关闭