37.Ansible循环+常用过滤器

发布于:2025-09-03 ⋅ 阅读:(15) ⋅ 点赞:(0)

Ansible循环

with_items迭代

安装httpd、samba软件包时,可以使用with_items迭代功能进行实现

---
- name: b
  hosts: node1
  tasks:
    - name: yum
      yum:
        name: "{{item}}"
        state: present
      with_items:
        - httpd
        - samba

with_dict迭代字典

item.key对应着是字典的键,item.value对应着字典的值

---
- name: b
  hosts: node1
  tasks:
    - name: de1
      debug:
        msg: "{{item.key}}&{{item.value}}"
      with_dict:
        - kuga: 1
        - agito: 2
        - ryuki: 3

with_fileglob迭代文件

拷贝多个文件到受控主机上时,可以使用

---
- name: b
  hosts: node1
  tasks:
    - name: cp
      copy:
        src: "{{item}}"
        dest: /tmp/
      with_fileglob:
        - /tmp/*.sh
        - /home/student/ansible/*.yml

with_lines迭代行

With_lines可以将命令行的输出结果按行迭代

---
- name: b
  hosts: node1
  tasks:
    - name: b1
      copy:
        src: "{{item}}"
        dest: /tmp/
      with_lines:
        - find /etc/ansible -name "*.yml"

with_nested嵌套迭代
---
- name: b
  hosts: node1
  tasks:
    - name: bb1
      debug:
        msg:"{{item[0]}}&{{item[1]}}"
      with_nested:
        - [a,b]
        - [1,2,3]

with_sequence排序

正序

---
- name: b
  hosts: node1
  tasks:
    - name: v
      debug:
        msg: "{{item}}"
      with_sequence:
        start=1
        end=10
        stride=1

倒序

---
- name: b
  hosts: node1
  tasks:
    - name: v
      debug:
        msg: "{{item}}"
      with_sequence:
        start=10
        end=1
        stride=-1

with_random_choice

随机获得列表中的一个值

---
- name: b
  hosts: node1
  tasks:
    - name: bb
      debug:
        msg: "{{item}}"
      with_random_choice:
        - 1
        - 2
        - 3
        - 4
        - 8
        - a
        - cc

loop

loop 是 Ansible 中用于创建循环的核心功能,它取代了旧的 with_* 语法,提供了更一致和易用的循环机制

---
- name: b
  hosts: node1
  tasks:
    - name: bb
      debug:
        msg: "{{item}}"
      loop:
        - abc
        - bbc
        - cca


Ansible常用字符串过滤器
  1. center 过滤器:如果指定宽度小于字符串长度,则返回原字符串
  2. count 过滤器:与 length 完全等效,可根据个人偏好选择使用
  3. shuffle 过滤器:每次执行结果都会不同,因为是随机打乱顺序
  4. listshuffle 通常结合使用,先将字符串转换为字符列表,然后打乱顺序
过滤器名称 功能描述 示例代码
upper 将字符串中的所有字符转换为大写 `{{ testvar
lower 将字符串中的所有字符转换为小写 `{{ testvar
trim 移除字符串首尾的空白字符 `{{ testvar1
length 返回字符串的长度 `{{ testvar
capitalize 将字符串首字母大写,其余字母转换为小写 `{{ testvar
first 返回字符串的第一个字符 `{{ testvar
last 返回字符串的最后一个字符 `{{ testvar
center 将字符串居中,并用空格填充至指定宽度 `{{ testvar1
count 返回字符串长度(与 length 等效) `{{ testvar2
list 将字符串转换为列表,每个字符作为一个元素 `{{ testvar3
shuffle 将字符串转换为列表后随机打乱顺序(洗牌) `{{ testvar3

Ansible 中常用数字相关过滤器
  1. 使用 intfloat 过滤器时,如果转换失败且未指定默认值,Ansible 会报错
  2. random 过滤器每次执行都会生成不同的随机值
  3. abs 过滤器适用于整数和浮点数
  4. round 过滤器不指定精度时默认为0(即取整)
  5. 对于 random 过滤器,步长参数定义了随机数的可能取值间隔
过滤器名称 功能描述 示例代码 示例输入值 示例结果/说明
int 将值转换为整数类型 `{{ ‘8’ int }}` "8"
int(default) 将值转换为整数,失败时返回默认值 `{{ ‘a’ int(default=6) }}` "a"
float 将值转换为浮点数类型 `{{ ‘8’ float }}` "8"
float(default) 将值转换为浮点数,失败时返回默认值 `{{ ‘a’ float(8.88) }}` "a"
abs 获取数值的绝对值 `{{ testvar4 abs }}` -5
round 对数值进行四舍五入 `{{ 12.5 round }}` 12.5
round 对数值进行四舍五入到指定小数位 `{{ 3.1415926 round(5) }}` 3.1415926
random 生成0到指定值之间的随机整数 `{{ 100 random }}` 100
random 生成指定起始值到上限值之间的随机整数 `{{ 10 random(start=5) }}` 10 (start=5)
random(start,step) 生成指定范围内按步长递增的随机整数 `{{ 15 random(start=5,step=3) }}` 15 (start=5, step=3)
random(step) 生成0到上限值之间按指定步长递增的随机整数 `{{ 15 random(step=5) }}` 15 (step=5)

Ansible中与加密有关的过滤器
过滤器名称 功能描述 示例代码
hash 使用指定算法对字符串进行哈希计算 `{{ ‘123456’
hash 使用MD5算法对字符串进行哈希计算 `{{ ‘123456’
checksum 获取字符串的校验和(与MD5哈希值一致) `{{ ‘123456’
password_hash(algorithm) 使用指定算法生成带随机盐的密码哈希 `{{ ‘123456’
password_hash(algorithm, salt) 使用指定算法和盐值生成密码哈希 `{{ ‘123456’
password_hash(algorithm) 使用SHA512算法生成带随机盐的密码哈希 `{{ ‘123123’
password_hash(algorithm, salt) 使用SHA512算法和指定盐值生成密码哈希 `{{ ‘123123’
  1. hash 过滤器支持多种算法,如 ‘md5’, ‘sha1’, ‘sha256’, ‘sha512’ 等
  2. checksum 过滤器实际上是使用MD5算法的简便方法
  3. password_hash 过滤器专为密码哈希设计,会自动添加盐值增强安全性
  4. 使用随机盐时,每次执行结果都会不同
  5. 指定盐值时,结果将保持一致,适用于需要可重复哈希的场景