概要
最近Y社内的自己的项目开始转型,
由于chef(社内的)存在admin的管理问题,
考虑转移到别的PF上。
以前就用过ansible,现在重新买一节课温习一下。
FYI
- Terraform学习:https://qiita.com/yaoluffy/items/9db0cc7eeabbd321e9b5
- 教材Udemy: https://www.udemy.com/course/learn-ansible/learn/lecture/11060794
正文
Inventory
放host信息的文件,一般长成这样
# Sample Inventory File
# Web Servers
web1 ansible_host=server1.company.com ansible_connection=ssh ansible_user=root ansible_ssh_pass=Password123!
web2 ansible_host=server2.company.com ansible_connection=ssh ansible_user=root ansible_ssh_pass=Password123!
web3 ansible_host=server3.company.com ansible_connection=ssh ansible_user=root ansible_ssh_pass=Password123!
# Database Servers
db1 ansible_host=server4.company.com ansible_connection=winrm ansible_user=administrator ansible_password=Password123!
[web_servers]
web1
web2
web3
[db_servers]
db1
[all_servers:children]
web_servers
db_servers
Playbook
把一系列的操作(install,config,start,shutdown等等)写到一个yaml文件里面,长成这样
-
name: 'Execute two commands on localhost'
hosts: localhost
tasks:
-
name: 'Execute a date command'
command: date
-
name: 'Execute a command to display hosts file'
command: 'cat /etc/hosts'
- 相当于Chef的cookbook
- 用
ansible-playbook
启动(而非ansible
命令) - task的执行顺序很重要,所以用队列来写(不能用map)
Jinja2
playbook里面用了{{}}来做vars,这里用的就是jinja2
FYI: https://www.jianshu.com/p/f04dae701361
Conditional
ansible里面也有条件语句(when)和循环语句(loop)
loop里的值需要被调用时用item,例
tasks:
- user: name='{{ item }}' status=present
loop:
- joe
- george
还能通过register把结果暂存起来,例
-
name: 'Add name server entry if not already entered'
hosts: localhost
tasks:
-
shell: 'cat /etc/resolv.conf'
register: command_output
-
shell: 'echo "nameserver 10.0.250.10" >> /etc/resolv.conf'
when: 'command_output.stdout.find("10.0.250.10") == -1'
Role
Role说白了就是共享包,类似于import
方便引用别人的东西, 然后自己再customise一下具体用哪些功能或者改哪些功能。
感想
- 如果真的打算将postgresql移植到k8s上,ansible也没有必要使用了
- 想象一下,如果要用ansible又不想维护,那ansible在k8s的deployment将成为必要
- 直接写postgresql的deployment岂不是更加省事
- 因为容量之类的问题,导致无法在k8s上装postgres,那还是建议保持现状(因为ansible和chef的差别就不大了)
- ansible的应用场景是建立在架构需要从实力开始的情形,即IaaS
- 换句话说,PaaS,CaaS都不需要ansible这项工具
- IaaS的话,感觉和Terraform一起食用更佳
- Katacoda
- 一个免费的一次性建立环境的在线工具