ToB企服应用市场:ToB评测及商务社交产业平台
标题:
不求甚解--详解ansible-playbook中roles的用法
[打印本页]
作者:
伤心客
时间:
2024-12-10 14:46
标题:
不求甚解--详解ansible-playbook中roles的用法
前言
本文将具体先容ansible-playbook中roles的各种用法,它允许你将相关的使命、变量、处置惩罚器、文件和模板等聚集在一起,以便于在不同的项目中复用
环境准备
组件版本操作系统Ubuntu 22.04.4 LTSansible2.17.6
基本用法
文件结构
.
├── deploy.hosts
├── deploy.yaml
└── roles
└── base
└── tasks
└── main.yaml
复制代码
deploy.hosts作为目标呆板的列表文件
▶ cat deploy.hosts
10.22.11.166
复制代码
deploy.yaml作为入口文件
▶ cat deploy.yaml
- name: deploy
hosts: all
remote_user: wilson
gather_facts: no
vars:
ansible_ssh_pass: '123456'
ansible_python_interpreter: /usr/bin/python3
roles:
- base
复制代码
name:指定作业的名字:deploy
hosts:all代表deploy.hosts文件中所有的呆板(也可以指定呆板的分类信息)
remote_user:目标呆板的登录用户
gather_facts: 是否需要收罗目标呆板的基本数据,默认收罗。脚本指定的是不收罗,为了进步执行速度
vars.ansible_ssh_oass:目标呆板登录用户的密码
vars.ansible_python_interpreter:目标呆板python3的路径
roles:指定本次作业所使用的角色 base
roles目录作为ansible-playbook中一系列作业使命的聚集,其中一个聚集的名字叫做base
tasks中的main.yaml作为base的入口文件
▶ cat roles/base/tasks/main.yaml
- name: first
command: echo 'hello world'
复制代码
base当前只有1个使命,就是登录到目标呆板,执行hello world
运行
▶ ansible-playbook -i deploy.hosts deploy.yaml
PLAY [deploy] ****************************************************************************************************
TASK [base : first] **********************************************************************************************
changed: [10.22.11.166]
PLAY RECAP *******************************************************************************************************
10.22.11.166 : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
复制代码
在屏幕上显示执行结果
需要改造roles/base/tasks/main.yaml
▶ cat roles/base/tasks/main.yaml
- name: first
command: echo 'hello world' register: display_result- name: display debug: msg: "{{ display_result }}"
复制代码
把结果放入变量display_result,然后通过模版语言打印出来,而且是json格式的
运行:
▶ ansible-playbook -i deploy.hosts deploy.yaml
PLAY [deploy] ****************************************************************************************************
TASK [base : first] **********************************************************************************************
changed: [10.22.11.166]
TASK [base : display] ********************************************************************************************
ok: [10.22.11.166] => {
"msg": {
"changed": true,
"cmd": [
"echo",
"hello world"
],
"delta": "0:00:00.002740",
"end": "2024-11-19 07:22:22.226036",
"failed": false,
"msg": "",
"rc": 0,
"start": "2024-11-19 07:22:22.223296",
"stderr": "",
"stderr_lines": [],
"stdout": "hello world",
"stdout_lines": [
"hello world"
]
}
}
PLAY RECAP *******************************************************************************************************
10.22.11.166 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
复制代码
引用环境变量
将操作系统的环境变量传入,可以通过lookup来引用,好比:要使用PATH,可以通过lookup('env', 'PATH')
改造roles/base/tasks/main.yaml
▶ cat roles/base/tasks/main.yaml
- name: first
debug:
msg: "{{ lookup('env', 'PATH') }}"
复制代码
运行:
▶ ansible-playbook -i deploy.hosts deploy.yaml
PLAY [deploy] ****************************************************************************************************
TASK [base : first] **********************************************************************************************
ok: [10.22.11.166] => {
"msg": "/home/wilson/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin:/usr/local/go/bin:/usr/local/go/bin"
}
PLAY RECAP *******************************************************************************************************
10.22.11.166 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
复制代码
使用ansible变量
这里的ansible变量,其中一部门ansible默认的变量,一部门是ansible运行的时候,会默认去收罗目标呆板的基本信息,好比操作系统、cpu、内存、磁盘等等的基本信息
部门内置变量
变量名描述inventory_hostname当前使命执行的主机名(来自 Inventory 文件)ansible_facts包罗所有收集到的主机事实(facts)hostvars所有主机变量的聚集,包罗当前和其他主机
收罗目标呆板的基本信息
需要把之前收罗ansible基本信息的开关打开gather_facts: yes,打开之后会牺牲运行速度
改造roles/base/tasks/main.yaml
- name: first
debug:
msg: "{{ hostvars }}"
复制代码
运行:
由于数据量太大,只展示部门,而且json格式,可以直接拿想要的值
▶ ansible-playbook -i deploy.hosts deploy.yaml
PLAY [deploy] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [10.22.11.166]
TASK [base : first] ************************************************************
ok: [10.22.11.166] => {
"msg": {
"10.22.11.166": {
"ansible_all_ipv4_addresses": [
"10.22.11.166"
],
"ansible_all_ipv6_addresses": [
"fe80::a00:27ff:fef6:82c4"
],
"ansible_apparmor": {
"status": "enabled"
},
"ansible_architecture": "x86_64",
"ansible_bios_date": "12/01/2006",
"ansible_bios_vendor": "innotek GmbH",
"ansible_bios_version": "VirtualBox",
"ansible_board_asset_tag": "NA",
"ansible_board_name": "VirtualBox",
"ansible_board_serial": "NA",
"ansible_board_vendor": "Oracle Corporation",
...
"inventory_dir": "/home/wilson/workspace/ansible",
"inventory_file": "/home/wilson/workspace/ansible/deploy.hosts",
"inventory_hostname": "10.22.11.166",
"inventory_hostname_short": "10.22.11.166",
"module_setup": true,
"playbook_dir": "/home/wilson/workspace/ansible"
}
}
}
PLAY RECAP *********************************************************************
10.22.11.166 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
复制代码
是否收罗目标的基本信息
上面已经演示过,只需要gather_facts: yes即可
从命令行传入变量
ansible-playbook可以通过-e传入变量使用
▶ cat roles/base/tasks/main.yaml
- name: first
debug:
msg: "{{ app_name }} - {{ app_version }}"
复制代码
运行:
▶ ansible-playbook -i deploy.hosts -e "app_name=prom app_version=1.0" deploy.yaml
PLAY [deploy] ****************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************
ok: [10.22.11.166]
TASK [base : first] **********************************************************************************************
ok: [10.22.11.166] => {
"msg": "prom - 1.0"
}
PLAY RECAP *******************************************************************************************************
10.22.11.166 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
复制代码
动态选择host
1)将主机分组
修改deploy.hosts文件
▶ cat deploy.hosts
[ga]
10.22.11.166
[gb]
10.22.11.166
127.0.0.1
复制代码
2)改造入口文件
▶ cat deploy.yaml
- name: deploy
hosts: '{{h}}'
remote_user: wilson
gather_facts: no
vars:
ansible_ssh_pass: '123456'
ansible_python_interpreter: /usr/bin/python3
roles:
- base
复制代码
将hosts改在成为'{{h}}',通过传入h变量来动态定义
3)运行
指定h为ga
▶ ansible-playbook -i deploy.hosts -e "h=ga" deploy.yaml
PLAY [deploy] ****************************************************************************************************
TASK [base : first] **********************************************************************************************
ok: [10.22.11.166] => {
"msg": "hello world"
}
PLAY RECAP *******************************************************************************************************
10.22.11.166 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
复制代码
指定h为gb
▶ ansible-playbook -i deploy.hosts -e "h=gb" deploy.yaml
PLAY [deploy] ****************************************************************************************************
TASK [base : first] **********************************************************************************************
ok: [10.22.11.166] => {
"msg": "hello world"
}
ok: [127.0.0.1] => {
"msg": "hello world"
}
PLAY RECAP *******************************************************************************************************
10.22.11.166 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
127.0.0.1 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
复制代码
条件选择when
▶ cat roles/base/tasks/main.yaml
- name: first
debug:
msg: "version 1"
when: version == '1'
- name: second
debug:
msg: "version 2"
when: version == '2'
复制代码
定义暂时变量
通过-e传入的变量来产生暂时变量
- name: first
set_fact:
tag: "hello-{{ version }}"
- name: second
debug:
msg: "{{ tag }}"
复制代码
运行:
▶ ansible-playbook -i deploy.hosts -e "version=2" deploy.yaml
...
TASK [base : second] *********************************************************************************************
ok: [10.22.11.166] => {
"msg": "hello-2"
}
...
复制代码
调用多个role
1)新增role: advance
.
├── deploy.hosts
├── deploy.yaml
└── roles
├── advance
│ └── tasks
│ └── main.yaml
└── base
└── tasks
└── main.yaml
复制代码
2)直接在入口文件deploy.yaml引用
.
├── deploy.hosts
├── deploy.yaml
└── roles
├── advance
│ └── tasks
│ └── main.yaml
└── base
└── tasks
└── main.yaml
复制代码
运行:
▶ ansible-playbook -i deploy.hosts -e "version=2" deploy.yaml
PLAY [deploy] ****************************************************************************************************
TASK [base : first] **********************************************************************************************
ok: [10.22.11.166]
TASK [base : second] *********************************************************************************************
ok: [10.22.11.166] => {
"msg": "hello-2"
}
TASK [advance : new-first] ***************************************************************************************
ok: [10.22.11.166] => {
"msg": "new hello world"
}
PLAY RECAP *******************************************************************************************************
10.22.11.166 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
复制代码
3)在roles base中引用 advance
▶ cat deploy.yaml
- name: deploy
hosts: all
remote_user: wilson
gather_facts: no
vars:
ansible_ssh_pass: '123456'
ansible_python_interpreter: /usr/bin/python3
roles:
- base
复制代码
▶ cat roles/base/tasks/main.yaml
- name: base first
debug:
msg: "base {{ version }}"
- name: base second
include_role:
name: advance
vars:
role_pipe_from_base: "hello world from base"
- name: base third
debug:
msg: "{{ role_pipe_from_advance }}"
复制代码
▶ cat roles/advance/tasks/main.yaml
- name: advance first
debug:
msg: "advance {{ version }}"
- name: advance second
debug:
msg: "{{ role_pipe_from_base }}"
- name: advance third
set_fact:
role_pipe_from_advance: "hello world from advance"
复制代码
运行:
▶ ansible-playbook -i deploy.hosts -e "version=2" deploy.yaml
PLAY [deploy] ****************************************************************************************************
TASK [base : base first] *****************************************************************************************
ok: [10.22.11.166] => {
"msg": "base 2"
}
TASK [base second] ***********************************************************************************************
included: advance for 10.22.11.166
TASK [advance : advance first] ***********************************************************************************
ok: [10.22.11.166] => {
"msg": "advance 2"
}
TASK [advance : advance second] **********************************************************************************
ok: [10.22.11.166] => {
"msg": "hello world from base"
}
TASK [advance : advance third] ***********************************************************************************
ok: [10.22.11.166]
TASK [base : base third] *****************************************************************************************
ok: [10.22.11.166] => {
"msg": "hello world from advance"
}
PLAY RECAP *******************************************************************************************************
10.22.11.166 : ok=6 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
复制代码
这个例子呈现了:
传入变量version是共用的
如何在role通报参数
执行过程中定义的暂时变量role_pipe_from_advance可以跨role持续
多role执行是串行的
小结
联系我,做深入的交换
至此,本文结束
在下才疏学浅,有撒汤漏水的,请各位不吝见教...
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/)
Powered by Discuz! X3.4