不求甚解--详解ansible-playbook中roles的用法

前言

本文将详细介绍ansible-playbook中roles的各种用法,它允许你将相关的任务、变量、处理器、文件和模板等集合在一起,以便于在不同的项目中复用

环境准备

组件版本
操作系统Ubuntu 22.04.4 LTS
ansible2.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执行是串行的

小结

  • 联系我,做深入的交流


至此,本文结束
在下才疏学浅,有撒汤漏水的,请各位不吝赐教...

作者:it排球君原文地址:https://www.cnblogs.com/MrVolleyball/p/18591185

%s 个评论

要回复文章请先登录注册