Ansible——lookup,过滤器

news/2024/9/24 21:17:29/

文章目录

  • Ansible——lookup,过滤器
    • lookup读取文件
    • lookup生成随机密码
    • lookup读取环境变量
    • lookup读取Linux命令的执行结果
    • lookup读取template变量替换后的文件
    • lookup读取配置文件
    • lookup读取DNS解析的值
  • 过滤器
    • 过滤器使用的位置
    • 过滤器对普通变量的操作
    • 过滤器对文件路径的操作
    • 过滤器对字符串变量的操作
    • 过滤器对JSON的操作
    • 过滤器对数据结构的操作
    • 过滤器的链式/连续使用
  • 常见插件类型

Ansible——lookup,过滤器

Ansible Playbook允许用户使用自定义变量,不过当变量过大,或者太复杂时,无论是在Playbook中通过vars定义,还是在单独的变量文件中定义,可读性都比较差

lookup能够读取Ansible管理节点上文件系统的文件内容到Ansible变量中,也可以读取配置的数据库中的内容

lookup读取文件

下面是lookup基本使用方法,将Ansible管理节点上的文件data/plain.txt的内容读取出来,并赋值给变量"contents"。"file"告诉lookup读取对象的类型是File,直接读取文件内容,无须做特别的处理

[root@csq ~]# mkdir data
[root@csq ~]# echo "csqcsqcsq" > data/plain.txt
[root@csq ~]# vim test.yml 
- hosts: allremote_user: rootgather_facts: falsevars:contents: "{{ lookup('file','/root/data/plain.txt') }}"tasks:- debug: msg="the value of /root/data/plain.txt is {{ contents }}"# 执行结果
TASK [debug] **********************************************************************************
ok: [192.168.200.20] => {"msg": "the value of /root/data/plain.txt is csqcsqcsq"
}
ok: [192.168.200.30] => {"msg": "the value of /root/data/plain.txt is csqcsqcsq"
}

lookup生成随机密码

如果密码文件/tmp/password/kitty不存在,lookup会生成长度为5的随机密码,存储在文件中。如果密码文件存在,那么直接读取该文件中的内容作为密码

[root@csq ~]# vim a.yml 
- hosts: allremote_user: rootvars:password: "{{ lookup('password','/tmp/password/kitty length=5') }}"tasks:- debug: var=password# 执行结果
TASK [debug] **********************************************************************************
ok: [192.168.200.20] => {"password": "TWLDK"
}
ok: [192.168.200.30] => {"password": "TWLDK"
}

lookup读取环境变量

env类型的lookup可以读取Linux上的环境变量

[root@csq ~]# vim c.yml              tasks:
- hosts: testremote_user: roottasks:- name: envdebug: msg=" {{lookup('env','HOME') }} 是一个环境变量"# 执行结果
[root@csq ~]# ansible-playbook c.yml PLAY [test] *************************************************************************************************************************TASK [Gathering Facts] **************************************************************************************************************
ok: [192.168.200.20]TASK [env] **************************************************************************************************************************
ok: [192.168.200.20] => {"msg": " /root 是一个环境变量"
}PLAY RECAP **************************************************************************************************************************
192.168.200.20             : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

lookup读取Linux命令的执行结果

pipe类型的lookup可以将Linux上命令的执行结果读取到Ansible中

[root@csq ~]# vim d.yml         
- hosts: allremote_user: roottasks:- name: 读取管理节点的Linux命令执行结果debug: msg="{{ lookup('pipe','date') }} 这是该命令执行的结果"# 执行结果
[root@csq ~]# ansible-playbook d.yml PLAY [all] **************************************************************************************************************************TASK [Gathering Facts] **************************************************************************************************************
ok: [192.168.200.30]
ok: [192.168.200.20]TASK [读取管理节点的Linux命令执行结果] **********************************************************************************************
ok: [192.168.200.20] => {"msg": "2024年 05月 02日 星期四 11:42:35 CST 这是该命令执行的结果"
}
ok: [192.168.200.30] => {"msg": "2024年 05月 02日 星期四 11:42:35 CST 这是该命令执行的结果"
}PLAY RECAP **************************************************************************************************************************
192.168.200.20             : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.200.30             : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

lookup读取template变量替换后的文件

template类型的lookup可以将一个template文件经过变量替换后的内容读取到Ansible中。如果在template文件中有未定义的变量,则会报错

# template文件
[root@csq ~]# vim template1.j2            
hostname: {{ansible_hostname}}# playbook文件
[root@csq ~]# vim e.yml              
- hosts: allremote_user: roottasks:- name: 读取templatedebug: msg="{{ lookup('template','/root/template1.j2') }} 这是template.j2的变量"# 执行结果
[root@csq ~]# ansible-playbook e.yml PLAY [all] **************************************************************************************************************************TASK [Gathering Facts] **************************************************************************************************************
ok: [192.168.200.30]
ok: [192.168.200.20]TASK [读取template] *****************************************************************************************************************
ok: [192.168.200.20] => {"msg": "hostname: podman\n 这是template.j2的变量"
}
ok: [192.168.200.30] => {"msg": "hostname: localhost\n 这是template.j2的变量"
}PLAY RECAP **************************************************************************************************************************
192.168.200.20             : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.200.30             : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

lookup读取配置文件

lookup支持读取两种类型的配置文件;ini和java的properties

ini类型的lookup默认读取配置文件类型是ini

# ini文件
[root@csq ~]# vim data/users.ini 
[test1]
user=csq
password=111
[test2]
user=zhw
password=000# playbook文件
[root@csq ~]# vim f.yml- name:
- hosts: testremote_user: roottasks:- debug:msg: "test1 user = {{ lookup('ini','user section=test1 file=/root/data/users.ini') }}"- debug:msg: "test2 user = {{ lookup('ini','user section=test2 file=/root/data/users.ini') }}"
# 执行结果
[root@csq ~]# ansible-playbook  f.yml PLAY [test] *************************************************************************************************************************TASK [Gathering Facts] **************************************************************************************************************
ok: [192.168.200.20]TASK [debug] ************************************************************************************************************************
ok: [192.168.200.20] => {"msg": "test1 user = csq"
}TASK [debug] ************************************************************************************************************************
ok: [192.168.200.20] => {"msg": "test2 user = zhw"
}PLAY RECAP **************************************************************************************************************************
192.168.200.20             : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

读取properties类型文件时,需要加一个额外的参数来告诉lookup,这是properties类型的文件

# user.properties文件
[root@csq ~]# vim data/user.properties
user.name=csq
user.pass=111# playbook文件
[root@csq ~]# vim g.yml- name: dd
- hosts: testremote_user: roottasks:- debug:msg: "user.name is {{ lookup('ini','user.name type=properties file=/root/data/user.properties') }}"# 执行结果
[root@csq ~]# ansible-playbook g.yml PLAY [test] *************************************************************************************************************************TASK [Gathering Facts] **************************************************************************************************************
ok: [192.168.200.20]TASK [debug] ************************************************************************************************************************
ok: [192.168.200.20] => {"msg": "user.name is csq"
}PLAY RECAP **************************************************************************************************************************
192.168.200.20             : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

ini类型参数格式:

lookup('ini','key [type=<properties|ini>] [section=section] [file=file.ini] [re=true] [default=<defaultvalue>]')
# 每个参数都是可选的,没有传入的参数会使用默认值
参数名默认值参数含义
typeini文件的类型
fileansible.ini加载文件的名字
sectionglobal默认的在哪个section里面查找key
reFalsekey的正则表达式
defaultempty stringkey不存在时的返回值

lookup读取DNS解析的值

dig类型的lookup可以向DNS服务器查询指定域名的DNS记录。它可以查询任何DNS记录,包括正向反向查询

使用dig类型的lookup查询DNS,使用正向反向查询DNS解析,还可以指定查询的DNS服务器

# 首先安装dnspython因为需要用到这个Python库
[root@localhost ansible]# pip3.11 install dnspython
# 剧本文件
[root@localhost ansible]# vim test7.yml              
- hosts: testremote_user: rootgather_facts: falsetasks:- debug: msg="这是baidu.com的ipv4地址:{{lookup('dig','baidu.com')}}"- debug: msg="这是baidu.com的TXT记录:{{lookup('dig','baidu.com','qtype=TXT')}}"- debug: msg="这是baidu.com的TXT记录:{{lookup('dig','baidu.com./TXT')}}"- debug: msg="163.com的mx记录之一是:{{item}}"with_items: "{{lookup('dig','163.com./MX','wantlist=True')}}"
# 执行剧本
TASK [debug] ************************************************************************************************************************
ok: [192.168.200.20] => {"msg": "这是baidu.com的ipv4地址:110.242.68.66,39.156.66.10"
}TASK [debug] ************************************************************************************************************************
ok: [192.168.200.20] => {"msg": "这是baidu.com的TXT记录:9279nznttl321bxp1j464rd9vpps246v,v=spf1 include:spf1.baidu.com include:spf2.baidu.com include:spf3.baidu.com include:spf4.baidu.com mx ptr -all,google-site-verification=GHb98-6msqyx_qqjGl5eRatD3QTHyVB6-xQ3gJB5UwM,_globalsign-domain-verification=qjb28W2jJSrWj04NHpB0CvgK9tle5JkOq-EcyWBgnE"
}TASK [debug] ************************************************************************************************************************
ok: [192.168.200.20] => {"msg": "这是baidu.com的TXT记录:9279nznttl321bxp1j464rd9vpps246v,v=spf1 include:spf1.baidu.com include:spf2.baidu.com include:spf3.baidu.com include:spf4.baidu.com mx ptr -all,google-site-verification=GHb98-6msqyx_qqjGl5eRatD3QTHyVB6-xQ3gJB5UwM,_globalsign-domain-verification=qjb28W2jJSrWj04NHpB0CvgK9tle5JkOq-EcyWBgnE"
}TASK [debug] ************************************************************************************************************************
ok: [192.168.200.20] => (item=10 163mx02.mxmail.netease.com.,10 163mx03.mxmail.netease.com.,50 163mx00.mxmail.netease.com.,10 163mx01.mxmail.netease.com.) => {"msg": "163.com的mx记录之一是:10 163mx02.mxmail.netease.com.,10 163mx03.mxmail.netease.com.,50 163mx00.mxmail.netease.com.,10 163mx01.mxmail.netease.com."
}

反向查询DNS

# 剧本文件
[root@localhost ansible]# vim test8.yml              
- hosts: testremote_user: rootgather_facts: falsetasks:- debug: msg="114.114.114.114反向查询DNS是:{{lookup('dig','114.114.114.114/PTR')}}"    
# 执行结果
TASK [debug] ************************************************************************************************************************
ok: [192.168.200.20] => {"msg": "114.114.114.114反向查询DNS是:public1.114dns.com."
}

指定查询的DNS服务器

    - debug: msg="使用8.8.8.8查询baidu.com的ipv4地址:{{lookup('dig','baidu.com','@8.8.8.8')}}"
# 执行结果
TASK [debug] ************************************************************************************************************************
ok: [192.168.200.20] => {"msg": "使用8.8.8.8查询baidu.com的ipv4地址:39.156.66.10,110.242.68.66"
}

过滤器

在 Ansible 中,过滤器(Filters)是一种功能强大的工具,用于在模板中处理变量的值。过滤器可以对变量进行转换、格式化、筛选和操作

过滤器使用的位置

quote过滤器的功能是给字符串加引号

[root@csq ~]# vim b.yml
- hosts: test2remote_user: rootgather_facts: falsevars:my_test_string: "This is the test string"tasks:- name: "quote {{ my_test_string }}"debug: msg="echo {{ my_test_string | quote }}"# 执行结果
TASK [quote This is the test string] **********************************************************
ok: [192.168.200.30] => {"msg": "echo 'This is the test string'"
}

过滤器对普通变量的操作

default:为没有定义的变量提供默认值

因为变量some_undefined_varible表示没有定义,所以下面的任务会输出NONE

[root@csq ~]# vim c.yml   
- hosts: testremote_user: rootgather_facts: falsetasks:- name: output1debug: msg="{{ some_undefined_varible | default('NONE') }}"
# 执行结果
TASK [output1] ********************************************************************************
ok: [192.168.200.20] => {"msg": "NONE"
}

如果是远程主机fact里面的变量,当远程主机的fact变量为空一般都是显示的[]或者"",那么我不想让他显示这个空的

true:这是 default 过滤器的第二个参数,用于指示 Ansible 在判断变量是否为空时将空字符串、空列表、空字典等视为真实的空值。如果设置为 true,则 Ansible 会将这些空值视为真实的空值,否则会将其视为非空值。

[root@csq ~]# vim c.yml              
- hosts: testremote_user: roottasks:- name: output1debug: msg="{{ ansible_fibre_channel_wwn | default('NONE',true) }}"# 执行结果TASK [output1] ********************************************************************************
ok: [192.168.200.20] => {"msg": "NONE"
}

omit:忽略变量的占位符

与default一起使用时,如果某个变量没有定义,那么使用omit占位符,Ansible就会把这个对应的参数按照没用传这个参数的值来处理

[root@csq ~]# vim r.yml
- hosts: testremote_user: roottasks:- name: touch filefile: dest={{item.path}} state=touch mode={{item.mode|default(omit)}}with_items:- path: /tmp/csq1- path: /tmp/csq2mode: "0444"
[root@csq ~]# ansible test -m shell -a "ls /tmp/csq* -la"     
192.168.200.20 | CHANGED | rc=0 >>
-rw-r--r--. 1 root root 0  52 13:58 /tmp/csq1
-r--r--r--. 1 root root 0  52 13:58 /tmp/csq2
# 文件/tmp/csq1没用定义参数mode,所以default(omit)会在没有定义mode时忽略mode变量
# ansible的file模块会按照没有传入mode这个参数来创建文件/tmp/csq1,/tmp/csq2定义了mode为0444
# 所以文件的权限为0444

mandatory:强制变量必须定义,否则报错

ansible默认的配置中,如果变量没有定义,那么直接使用未定义的变量{{aaa-bbb-ccc}}会报错

如果ansible配置文件中使用了下面的配置,那么遇到未定义的变量时,ansible就不会报错

error_on_undefined_vars=False

如果你想要某一个变量必须定义,就可使用mandatory

[root@csq ~]# vim l.yml                    
- hosts: testremote_user: rootvars:aaabbbccc: abcgather_facts: falsetasks:- name: 定义了变量debug:msg: "{{aaabbbccc}}"- name: 未定义变量报错debug:msg: "{{aaa-bbb-ccc | mandatory}}"# 执行结果
[root@csq ~]# ansible-playbook l.yml       PLAY [test] *************************************************************************************************************************TASK [定义了变量] *******************************************************************************************************************
ok: [192.168.200.20] => {"msg": "abc"
}TASK [未定义变量报错] ***************************************************************************************************************
fatal: [192.168.200.20]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'aaa' is undefined. 'aaa' is undefined\n\nThe error appears to be in '/root/l.yml': line 10, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n        msg: \"{{aaabbbccc}}\"\n    - name: 未定义变量报错\n      ^ here\n"}PLAY RECAP **************************************************************************************************************************
192.168.200.20             : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

bool:判断变量是否为布尔类型

bool的过滤器是playbook中判断条件bool类型的过滤器,其中bool类型的过滤器用来判断变量是否为布尔类型

[root@csq ~]# vim i.yml              
- hosts: testremote_user: rootgather_facts: falsevars:var1: "Test"var2: Truevar3: "True"tasks:- debug: msg=testwhen: var1 | bool- debug: msg=testwhen: var2 | bool- debug: msg=testwhen: var3 | bool
#  执行结果
TASK [debug] **************************************************************************************************************************
skipping: [192.168.200.20]TASK [debug] **************************************************************************************************************************
ok: [192.168.200.20] => {"msg": "test"
}TASK [debug] **************************************************************************************************************************
ok: [192.168.200.20] => {"msg": "test"
}

ternary:Playbook的条件表达式

ternary类似于编程语言中的类型表达式,(“A?B:C”)当条件为真时,返回一个值;当条件为假时,返回另一个值

[root@csq ~]# vim j.yml              
- hosts: testremote_user: rootgather_facts: falsevars:hostname: "csq"tasks:- name: if hostname = csq Yes if hostname = other Nodebug: msg="{{ (hostname == "csq") | ternary('Yes','No') }}"
# 执行结果
TASK [if hostname = csq Yes if hostname = other No] ***********************************************************************************
ok: [192.168.200.20] => {"msg": "Yes"
}

过滤器对文件路径的操作

Ansible为了方便文件及其路径进行操作,提供了一系列关于文件目录的操作,包括获取文件名,路径名,等等。

Linux文件路径的操作的过滤器如下:

  • basename:获取路径中的文件名
  • dirname:获取文件的目录
  • expanduser:扩展~为实际的目录
  • realpath:获取链接文件所指文件的真实路径
  • relpath:获得相对某一根目录的相对路径
  • splitext:把文件名用点号(.)分割成多个部分
[root@csq ~]# vim test1.yml              
- hosts: testremote_user: rootgather_facts: falsevars:linux_path: "/etc/csq/a.txt"linux_user_path: "~/data/my_test/file.txt"link_to_ansible_cfg: "/bin"tasks:- name: "获取Linux路径的文件名 {{linux_path}}"debug: msg="{{linux_path | basename}}"- name: "获取Linux路径的目录名 {{linux_path}}"debug: msg="{{linux_path | dirname}}"- name: "获取展开文件路径中的用户目录 {{linux_user_path}}"debug: msg="{{linux_user_path | expanduser}}"- name: "获取文件路径的真实路径 {{link_to_ansible_cfg}}"debug: msg="{{link_to_ansible_cfg | realpath}}"- name: "获取相对某一根目录的相对路径 {{linux_path}}"debug: msg="{{linux_path | relpath('/etc')}}"- name: "把文件名用点号(.)分隔成多个部分{{linux_path}}"debug: msg="{{linux_path | splitext}}"
# 执行结果
TASK [获取Linux路径的文件名 /etc/csq/a.txt] *******************************************************************************************
ok: [192.168.200.20] => {"msg": "a.txt"
}TASK [获取Linux路径的目录名 /etc/csq/a.txt] *******************************************************************************************
ok: [192.168.200.20] => {"msg": "/etc/csq"
}TASK [获取展开文件路径中的用户目录 ~/data/my_test/file.txt] ***************************************************************************
ok: [192.168.200.20] => {"msg": "/root/data/my_test/file.txt"
}TASK [获取文件路径的真实路径 /bin] ****************************************************************************************************
ok: [192.168.200.20] => {"msg": "/usr/bin"
}TASK [获取相对某一根目录的相对路径 /etc/csq/a.txt] ************************************************************************************
ok: [192.168.200.20] => {"msg": "csq/a.txt"
}TASK [把文件名用点号(.)分隔成多个部分/etc/csq/a.txt] **********************************************************************************
ok: [192.168.200.20] => {"msg": "('/etc/csq/a', '.txt')"
}

过滤器对字符串变量的操作

quote:给字符串加引号

[root@csq ~]# vim b.yml
- hosts: test2remote_user: rootgather_facts: falsevars:my_test_string: "This is the test string"tasks:- name: "quote {{ my_test_string }}"debug: msg="echo {{ my_test_string | quote }}"# 执行结果
TASK [quote This is the test string] **********************************************************
ok: [192.168.200.30] => {"msg": "echo 'This is the test string'"
}

base64:得到字符串Base64编码

[root@csq ~]# vim test2.yml              
- hosts: testremote_user: rootgather_facts: falsevars:my_comment: "hello world"my_comment_base64: "aGVsbG8gd29ybGQ="tasks:- name: "获取base64编码{{my_comment}}"debug: msg="{{my_comment | b64encode}}"- name: "解码base64 {{my_comment_base64}}"debug: msg="{{my_comment_base64 | b64decode}}"- name: "获取uuid {{my_comment}}"debug: msg="{{my_comment | to_uuid}}"
# 执行结果
TASK [获取base64编码hello world] ******************************************************************************************************
ok: [192.168.200.20] => {"msg": "aGVsbG8gd29ybGQ="
}TASK [解码base64 aGVsbG8gd29ybGQ=] ****************************************************************************************************
ok: [192.168.200.20] => {"msg": "hello world"
}TASK [获取uuid hello world] ***********************************************************************************************************
ok: [192.168.200.20] => {"msg": "9a129f19-657c-5ca0-80f4-31b29d10569c"
}

hash:获取字符串的哈希值

计算哈希值的算法有很多,如果sha1、md5、checksum等

[root@csq ~]# vim test3.yml              
- hosts: testremote_user: rootgather_facts: falsevars:my_password: "mypassword"tasks:- name: "获取字符串{{my_password}}的sha1 hash"debug: msg="{{my_password | hash('sha1')}}"- name: "获取字符串{{my_password}}的MD5 hash"debug: msg="{{my_password | hash('md5')}}"- name: "获取字符串{{my_password}}的checksum"debug: msg="{{my_password | checksum}}"- name: "获取字符串{{my_password}}的sha512 hash"debug: msg="{{my_password | password_hash('sha512')}}"- name: "获取字符串{{my_password}}的sha256 hash带有特定字符串"debug: msg="{{my_password | password_hash('sha256','mysecretsalt')}}"
# 执行结果
TASK [获取字符串mypassword的sha1 hash] ************************************************************************************************
ok: [192.168.200.20] => {"msg": "91dfd9ddb4198affc5c194cd8ce6d338fde470e2"
}TASK [获取字符串mypassword的MD5 hash] *************************************************************************************************
ok: [192.168.200.20] => {"msg": "34819d7beeabb9260a5c854bc85b3e44"
}TASK [获取字符串mypassword的checksum] *************************************************************************************************
ok: [192.168.200.20] => {"msg": "91dfd9ddb4198affc5c194cd8ce6d338fde470e2"
}TASK [获取字符串mypassword的sha512 hash] **********************************************************************************************ok: [192.168.200.20] => {"msg": "$6$kM35/CEoWTM1xoIX$0HjNRHNwS01H2uH4Rik7h06E3DzDZpI9uHvg0NN0QE2kQOMKYwkN28kPKOuB9.0aShHv6CsFJAbAPnOCv1PZO0"
}TASK [获取字符串mypassword的sha256 hash带有特定字符串] ********************************************************************************ok: [192.168.200.20] => {"msg": "$5$mysecretsalt$HhobCuvPwbLCsdua9UNYk8C.EPib1CkzWvN23KBHYV2"
}

comment:把字符串变成代码注释的一部分

comment展示了将字符串转化为不同风格和格式注释的使用方法,最后一个是用户自定义的注释风格

[root@csq ~]# vim test4.yml              - name: "erlang 
- hosts: testremote_user: rootgather_facts: falsevars:my_comment: "hello world"tasks:- name: "Simple comment"debug: msg="{{my_comment | comment}}"- name: "C  comment"debug: msg="{{my_comment | comment('c')}}"- name: "cblock  comment"debug: msg="{{my_comment | comment('cblock')}}"- name: "erlang  comment"debug: msg="{{my_comment | comment('erlang')}}"- name: "xml  comment"debug: msg="{{my_comment | comment('xml')}}"- name: "customize comment"debug: msg="{{my_comment | comment('plain',prefix='######\n#',postfix='#\n######\n###\n#')}}"
# 执行结果
TASK [Simple comment] *****************************************************************************************************************
ok: [192.168.200.20] => {"msg": "#\n# hello world\n#"
}TASK [C  comment] *********************************************************************************************************************
ok: [192.168.200.20] => {"msg": "//\n// hello world\n//"
}TASK [cblock  comment] ****************************************************************************************************************
ok: [192.168.200.20] => {"msg": "/*\n *\n * hello world\n *\n */"
}TASK [erlang  comment] ****************************************************************************************************************
ok: [192.168.200.20] => {"msg": "%\n% hello world\n%"
}TASK [xml  comment] *******************************************************************************************************************
ok: [192.168.200.20] => {"msg": "<!--\n -\n - hello world\n -\n-->"
}TASK [customize comment] **************************************************************************************************************
ok: [192.168.200.20] => {"msg": "######\n#\n# hello world\n#\n######\n###\n#"
}

regex:利用正则表达式对字符串进行替换

[root@csq ~]# vim test5.yml              
- hosts: testremote_user: rootvars:my_comment: "hello world"gather_facts: falsetasks:- name: "转换 ansible 为 able"debug: msg="{{'ansible' | regex_replace('^a.*i(.*)$','a\\1')}}"- name: "转换 foobar 为 bar"debug: msg="{{'foobar' | regex_replace('^f.*o(.*)$','\\1')}}"- name: "转换 localhost:80 为 localhost,80"debug: msg="{{'localhost:80' | regex_replace('^(?P<host>.+):(?P<port>\\d+)$','\\g<host>,\\g<port>') }}"
# 执行结果
TASK [转换 ansible 为 able] ***********************************************************************************************************
ok: [192.168.200.20] => {"msg": "able"
}TASK [转换 foobar 为 bar] *************************************************************************************************************
ok: [192.168.200.20] => {"msg": "bar"
}TASK [转换 localhost:80 为 localhost,80] **********************************************************************************************
ok: [192.168.200.20] => {"msg": "localhost,80"
}

ip:判断字符串是否是合法的IP地址

[root@localhost ansible]# vim test6.yml              
- hosts: testremote_user: rootgather_facts: falsevars:ip_address1: "192.168.2455.1"ip_address2: "192.168.200.20"ip_addr_ipv6: "fe80::20c:29ff:fe4e:d6a9"tasks:- name: "判断{{ip_address1}}是否为合法ip"debug: msg="{{ ip_address1 | ipaddr}}"- name: "判断{{ip_address2}}是否为合法ip"debug: msg="{{ ip_address2 | ipaddr}}"- name: "判断{{ip_address2}}是否为ipv4地址"debug: msg="{{ ip_address2 | ipv4}}"- name: "判断{{ip_addr_ipv6}}是否为ipv6地址"debug: msg="{{ ip_addr_ipv6 | ipv6}}"- name: "判断{{'192.0.2.1/24' | ipaddr('address')}}是否为合法IP"debug: msg="{{'192.0.2.1/24' | ipaddr('address')}}"
# 执行结果
[root@localhost ansible]# ansible-playbook test6.yml TASK [判断192.168.2455.1是否为合法ip] ***********************************************************************************************ok: [192.168.200.20] => {"msg": false
}TASK [判断192.168.200.20是否为合法ip] ***********************************************************************************************ok: [192.168.200.20] => {"msg": "192.168.200.20"
}TASK [判断192.168.200.20是否为ipv4地址] *********************************************************************************************ok: [192.168.200.20] => {"msg": "192.168.200.20"
}TASK [判断fe80::20c:29ff:fe4e:d6a9是否为ipv6地址] ***********************************************************************************ok: [192.168.200.20] => {"msg": "fe80::20c:29ff:fe4e:d6a9"
}TASK [判断192.0.2.1是否为合法IP] ****************************************************************************************************
ok: [192.168.200.20] => {"msg": "192.0.2.1"
}

过滤器对JSON的操作

format:将变量的值按照JSON/YAML格式输出

# 剧本文件
[root@localhost ansible]# vim test1.yml   
- hosts: testgather_facts: falsevars:my_variable:key1: value1key2: value2tasks:- name: Print variable as JSONdebug:msg: "{{ my_variable | to_json }}"- name: Print variable as YAMLdebug:msg: "{{ my_variable | to_yaml }}"- name: Print variable as Nice JSONdebug:msg: "{{ my_variable | to_nice_json }}"- name: Print variable as Nice YAMLdebug:msg: "{{ my_variable | to_nice_yaml }}"
# 执行结果TASK [Print variable as JSON] *******************************************************************************************************
ok: [192.168.200.20] => {"msg": "{\"key1\": \"value1\", \"key2\": \"value2\"}"
}TASK [Print variable as YAML] *******************************************************************************************************
ok: [192.168.200.20] => {"msg": "{key1: value1, key2: value2}\n"
}TASK [Print variable as Nice JSON] **************************************************************************************************
ok: [192.168.200.20] => {"msg": "{\n    \"key1\": \"value1\",\n    \"key2\": \"value2\"\n}"
}TASK [Print variable as Nice YAML] **************************************************************************************************
ok: [192.168.200.20] => {"msg": "key1: value1\nkey2: value2\n"
}

query:在一个JSON对象里,搜索符合条件的属性,返回符合条件的属性数组

[root@localhost ansible]# vim test2.yml              
- name: Search for properties matching a condition in a JSON objecthosts: localhostgather_facts: falsevars:my_json:users:- name: Johnage: 30- name: Aliceage: 25- name: Bobage: 35tasks:- name: Query JSON objectdebug:msg: "{{ my_json.users | json_query('[].name') }}"
# 执行结果
ok: [localhost] => {"msg": ["John","Alice","Bob"]
}

过滤器对数据结构的操作

Ansible中的过滤器支持以下几种数据结构的操作

random:取随机数

[root@localhost ansible]# vim test3.yml              
- hosts: testremote_user: rootgather_facts: falsetasks:- name: 列表随机debug: msg="{{ ['a','b','c'] | random}}"- name: 随机数debug: msg="{{ 59 | random}} * * * * echo hello world"- name: random with stepdebug: msg="{{ 100 | random(step=10)}}"- name: random with start and stepdebug: msg="{{ 100 | random(2,10)}}"- name: random with start and stepdebug: msg="{{ 100|random(start=2,step=10)}}"
# 执行结果TASK [列表随机] *********************************************************************************************************************
ok: [192.168.200.20] => {"msg": "b"
}TASK [随机数] ***********************************************************************************************************************
ok: [192.168.200.20] => {"msg": "33 * * * * echo hello world"
}TASK [random with step] *************************************************************************************************************
ok: [192.168.200.20] => {"msg": "10"
}TASK [random with start and step] ***************************************************************************************************
ok: [192.168.200.20] => {"msg": "32"
}TASK [random with start and step] ***************************************************************************************************
ok: [192.168.200.20] => {"msg": "72"
}

过滤器的链式/连续使用

ansible的过滤器是支持链式使用的,就是在一个{{}}中使用多个过滤器

[root@localhost ansible]# vim test4.yml              
- hosts: testremote_user: rootvars:aaabbbccc: abcgather_facts: falsetasks:- name: 判断变量是否存在,存在就使用base64加密该变量debug:msg: "{{aaabbbccc | mandatory | b64encode}}"
# 执行结果TASK [判断变量是否存在,存在就使用base64加密该变量] *********************************************************************************
ok: [192.168.200.20] => {"msg": "YWJj"
}

常见插件类型

modules插件

模块插件是Ansible的核心成分,用于执行具体的任务。它们可以通过在任务中调用来提供丰富的功能,例如文件操作、软件包管理、配置管理等。

Inventory插件

主机清单插件用于动态生成Ansible的主机清单。它们可以从各种源(如INI文件、YAML文件、云服务提供商)读取主机信息,并动态生成可供Ansible使用的清单。

Action插件

和模块使用方法类似,只不过执行目标不是远程主机,而是在Ansible的控制节点(管理节点)本机上

Cache插件

为Facts(主机变量)提供缓存,以避免多次执行Playbook时搜集Facts

Callback插件

Ansible执行Playbook后,提供额外的行为,例如,将执行结果发送到E-mail中,或者将执行结果写入log中等等

connection插件

用于在Ansible连接远程主机时进行身份验证。认证插件允许使用不同的认证机制,如用户名/密码、私钥等

filters插件

过滤器插件用于对变量进行转换、过滤和操作。

lookup插件

文件查找插件用于在Ansible中查找文件和数据。它们可以从文件系统、远程主机或其它的数据源中查找所需的文件或数据

test插件

ansible Jinja2 test提供更多功能


http://www.ppmy.cn/news/1456660.html

相关文章

复现SMO算法:序列最小优化的启发式方法【三、算法原理揭秘-2】

接下来的内容将转向SMO算法的第二个核心组成部分——选择要优化的乘数的启发式方法。在这篇博客中&#xff0c;我们将探讨算法如何通过启发式选择策略高效地识别和更新拉格朗日乘数。通过对比直接优化的分析方法和启发式方法的策略选择&#xff0c;我们能够更全面地理解SMO算法…

详解MySQL常用的数据类型

前言 MySQL是一个流行的关系型数据库管理系统&#xff0c;它支持多种数据类型&#xff0c;以满足不同数据处理和存储的需求。理解并正确使用这些数据类型对于提高数据库性能、确保数据完整性和准确性至关重要。本文将详细介绍MySQL中的数据类型&#xff0c;包括数值类型、字符…

口感与风味的完善结合:精酿啤酒的多样风格

啤酒的世界是丰富多彩的&#xff0c;不同的啤酒有着各自与众不同的口感和风味。而Fendi club啤酒&#xff0c;作为精酿啤酒的代表&#xff0c;以其多样化的风格和卓着的口感&#xff0c;吸引了无数啤酒爱好者的目光。 Fendi club啤酒的多样风格&#xff0c;首先体现在其原料的选…

如何迁移Windows PC数据到统信UOS 1070

原文链接&#xff1a;如何迁移Windows PC数据到统信UOS 1070 Hello&#xff0c;大家好啊&#xff01;随着统信UOS 1070的推出&#xff0c;越来越多的用户和企业选择迁移到这个基于Linux的操作系统&#xff0c;以享受其安全性和稳定性的优势。今天&#xff0c;我们将探讨如何使用…

【开发工具】pythontutor——在线内存可视化工具

笔者在学习RISC-V时&#xff0c;希望找到一款可视化的内存工具&#xff0c;遗憾目前还未找到。发现了pythontutor这个网站&#xff0c;可以对C、python等多种语言进行内存可视化。结果似乎是x86架构的&#xff0c;符合小端存储。 贴一下网址&#xff0c;原准备依据开源版本进行…

文本转图表的AI工具-Chart-GPT

Chart-GPT Chart-GPT一款基于 GPT 实现的开源工具&#xff0c;可在几秒内&#xff0c;将文本快速转换为各种图表。用户只需在输入字段中输入数据说明和所需的图表类型&#xff0c;Chart-GPT的后台生成器即可建出多种类型的图表&#xff0c;包括条形图、折线图、组合图、散点图、…

AI实景自动无人直播软件:引领直播行业智能化革命;提升直播效果,无人直播软件助力智能讲解

随着科技的快速发展&#xff0c;AI实景自动无人直播软件正在引领直播行业迈向智能化革命。它通过智能讲解、一键开播和智能回复等功能&#xff0c;为商家提供了更高效、便捷的直播体验。此外&#xff0c;软件还支持手机拍摄真实场景或搭建虚拟场景&#xff0c;使直播画面更好看…

网络安全之交换基础

交换属于二层技术。路由器&#xff08;router&#xff09;是三层设备&#xff0c;可以基于IP地址转发&#xff0c;但需要路由表来记录。 交换机&#xff08;switch&#xff09;是二层设备&#xff0c;网桥&#xff08;switch&#xff09;也是二层设备&#xff0c;这两个都是基…