委派任务和facts
- 委派任务,是将任务交给某台指定主机去完成,而不是play中的被管主机
[student@worktest myansible]$ cat delegate.yml
---
- name: delegate test
hosts: servera
tasks:
- name: get remote hostname # 获取远程主机的信息的任务
command: hostname
register: server # 将模块执行结果注册到名为server的变量
- name: display remote vars
debug:
msg: "{{server}}"
- name: display remote vars sub content
debug:
msg: "{{server['stdout']}}"
- name: get local hostname # 获取本地主机的信息的任务
command: hostname
register: local
delegate_to: localhost
- name: display local hostname
debug:
msg: "{{local}}"
- 委派facts
[student@worktest myansible]$ cat myfacts.yml
- hosts: localhost
gather_facts: no # 默认不收集facts变量
tasks:
- name: Set a fact in delegated task on servera
set_fact: # 设置自定义的facts变量
myfact: Where am I set?
delegate_to: servera # 委派给servera执行
delegate_facts: True # facts变量也来自于servera
- name: Display the facts from servera.lab.example.com
debug: # hostvars可以展示指定主机的相关变量
msg: "{{ hostvars['servera']['myfact'] }}"
滚动更新
-
如果某一服务由多个节点构成,为了实现业务零中断,可以采用滚动更新的办法。
-
默认情况下,ansible在所有的主机上执行任务,当所有的主机全部完成某一任务后,才会继续执行后续任务。
-
如果某一主机执行任务失败,它将停止执行后续任务。其他主机不受影响。
-
如果执行某一任务时,全部主机都失败了,整个play才会终止。
设置batch size
- 通过serial关键字设置某一任务最多多少台主机同时进行。
- 当某一batch中的所有主机执行任务失败后,将会停止play。
---
- name: Update Webservers
hosts: web_servers
serial: 2 # 每个任务最多2台机器同时进行
- batch size也可以使用百分数来表示
---
- name: Update Webservers
hosts: web_servers
serial: 25%
- batch size也可以设置为每次不是固定的值
---
- name: Update Webservers
hosts: web_servers
serial:
- 1 # 第1批是1台
- 10% # 第2批是全部数量的10%
- 100% # 第3批是剩余全部
- 如果明确写明的批次不能更新完成,后续的数量以最后一个值为准
# 如果是100台机器,则更新的批次为:1 + 10 + 25 + 25 + 25 + 14 = 100
---
- name: Update Webservers
hosts: web_servers
serial:
- 1
- 10%
- 25%
- 默认一个批次中所有的主机执行失败,才会终止play。也可以设置失败百分比,一旦达到该百分比,则终止。
---
- name: Update Webservers
hosts: web_servers
max_fail_percentage: 30% # 某一批次中有超过30%主机失败,则终止
serial:
- 2
- 10%
- 100%
tasks:
- name: Step One
shell: /usr/bin/some_command
- name: Step Two
shell: /usr/bin/some_other_command
只运行一次的任务
- 通过run_once定义某一任务,只要执行一次
[student@worktest update-delegation]$ cat once.yml
- name: run once tasks
hosts: web_servers
tasks:
- name: execute command
command: 'id root'
- name: touch file
shell: "echo hello >> /tmp/hello.txt"
delegate_to: localhost
[student@worktest update-delegation]$ cat /tmp/hello.txt
hello
hello
hello
hello
hello
hello
# 通过run_once,指定委派的任务只执行一次
[student@worktest update-delegation]$ cat once.yml
- name: run once tasks
hosts: web_servers
tasks:
- name: execute command
command: 'id root'
- name: touch file
shell: "echo hello >> /tmp/hi.txt"
delegate_to: localhost
run_once: yes
[student@worktest update-delegation]$ cat /tmp/hi.txt
hello
安装并使用Ansible Tower
- 安装Ansible Tower,需要下载QQ群中DO447目录下的rpm包。该包包括最新的License。
# 切换成root进行软件包安装,管理员密码是Asimov
[root@foundation0 ~]# su -
[root@foundation0 ~]# rpm -ihv --force foundation0-do447-2.8-2.r2021121504git9df07522.noarch.rpm
用户和组
用户
- RBAC(基于角色的该问控制):Tower是通过RBAC的方式进行管理的。
- 组织:组织是Team、Project、Inventory的集合体。
- 用户也必须属于某一个组织。
用户类型
- System Administrator:系统管理员。拥有全部权限。对所有组织中的所有对象都有权限。
- System Auditor:系统审计员。对Ansible所有资源都具有只读权限。
- Normal User:普通用户。必须赋予相应的role角色,才会拥有相关的权限。
组织的角色
- 可以在组织内为用户分配的权限有:
- Organizational Admin :某一组织的管理员。可以细分为 Project Admin , Inventory Admin , Credential Admin , Notification Admin , Workflow Admin , Job Template Admin 。
- Auditor:对组织所有的对象拥有只读权限。
- Member :用户可以登录Ansible,并读取自己的组织。
- Read:赋予某一用户对某一对象的Read角色,用户可以查看其内容。
- Execute:执行。可以执行Job。
组
-
Team:组就是用户的集合。
-
为了管理方便,可以将用户放到组中,然后对组授权。
-
Team role:为了管理Team,可以为用户设置相关的角色。这些角色控制用户是否可以成为Team成员,能不能管理它,以及列出成员。
- Member:允许用户从资源上继承权限。
- admin:允许用户管理组成员。
- read:允许用户列出组成员。