当我尝试检查远程服务器上符号链接的目标是否与本地服务器上的目标相同时,我遇到了麻烦。
我的剧本有什么问题吗?
@localhost
$ ls -l /tmp/test /tmp/test2
lrwxrwxrwx 1 myuser users 11 Apr 23 15:55 /tmp/test -> test.target
lrwxrwxrwx 1 myuser users 12 Apr 23 15:56 /tmp/test2 -> test2.target
@target
$ ls -l /tmp/test /tmp/test2
lrwxrwxrwx 1 myuser users 11 4月 23 16:05 /tmp/test -> test.target
lrwxrwxrwx 1 myuser users 12 4月 23 16:05 /tmp/test2 -> test2.target
playbook(test.yml)
- hosts: all
gather_facts: no
vars:
files:
- /tmp/test
- /tmp/test2
tasks:
- stat:
path: '{{ item }}'
register: out_stat_source
delegate_to: localhost
with_items: '{{ files }}'
run_once: true
- stat:
path: '{{ item }}'
register: out_stat_remote
failed_when: out_stat_remote.stat.lnk_target != out_stat_source|json_query(get_lnk_target)
vars:
get_lnk_target: |
{{ "results[?item=='" + item + "'].stat.lnk_target" }}
with_items: '{{ files }}'
因为远程服务器和本地服务器上符号链接的目标相同,所以预期的行为是SUCCESS。 但是输出如下:
test.yml的输出
$ ansible-playbook -itarget, test.yml
PLAY [all] *******************************************************************************************************************************************************************************************************
TASK [stat] ******************************************************************************************************************************************************************************************************
ok: [target -> localhost] => (item=/tmp/test)
ok: [target -> localhost] => (item=/tmp/test2)
TASK [stat] ******************************************************************************************************************************************************************************************************
failed: [target] (item=/tmp/test) => {"changed": false, "failed_when_result": true, "item": "/tmp/test", "stat": {"atime": 1587625535.6842694, "attr_flags": "", "attributes": [], "block_size": 4096, "blocks": 0, "charset": "binary", "ctime": 1587625508.0491216, "dev": 64768, "device_type": 0, "executable": false, "exists": true, "gid": 100, "gr_name": "users", "inode": 27342789, "isblk": false, "ischr": false, "isdir": false, "isfifo": false, "isgid": false, "islnk": true, "isreg": false, "issock": false, "isuid": false, "lnk_source": "/tmp/test.target", "lnk_target": "test.target", "mimetype": "inode/symlink", "mode": "0777", "mtime": 1587625508.0491216, "nlink": 1, "path": "/tmp/test", "pw_name": "myuser", "readable": false, "rgrp": true, "roth": true, "rusr": true, "size": 11, "uid": 10004, "version": null, "wgrp": true, "woth": true, "writeable": false, "wusr": true, "xgrp": true, "xoth": true, "xusr": true}}
failed: [target] (item=/tmp/test2) => {"changed": false, "failed_when_result": true, "item": "/tmp/test2", "stat": {"atime": 1587625535.6842694, "attr_flags": "", "attributes": [], "block_size": 4096, "blocks": 0, "charset": "binary", "ctime": 1587625511.3281388, "dev": 64768, "device_type": 0, "executable": false, "exists": true, "gid": 100, "gr_name": "users", "inode": 27473317, "isblk": false, "ischr": false, "isdir": false, "isfifo": false, "isgid": false, "islnk": true, "isreg": false, "issock": false, "isuid": false, "lnk_source": "/tmp/test2.target", "lnk_target": "test2.target", "mimetype": "inode/symlink", "mode": "0777", "mtime": 1587625511.3281388, "nlink": 1, "path": "/tmp/test2", "pw_name": "myuser", "readable": false, "rgrp": true, "roth": true, "rusr": true, "size": 12, "uid": 10004, "version": null, "wgrp": true, "woth": true, "writeable": false, "wusr": true, "xgrp": true, "xoth": true, "xusr": true}}
to retry, use: --limit @/home/myuser/test.ansible/test.retry
PLAY RECAP *******************************************************************************************************************************************************************************************************
target : ok=1 changed=0 unreachable=0 failed=1
我创建了以下剧本进行调试。
我确认的是,results[0]
可以使用,但是当我将results[?item=='foo']
与fail_when一起使用时,在json_query中它不起作用。
playbook(test2.yml)
- hosts: all
gather_facts: no
vars:
files:
- /tmp/test
- /tmp/test2
tasks:
- stat:
path: '{{ item }}'
register: out_stat_source
delegate_to: localhost
with_items: '{{ files }}'
run_once: true
- debug:
msg: '{{ out_stat_source|json_query(get_lnk_target) }}'
vars:
get_lnk_target: |
{{ "results[?item=='" + item + "'].stat.lnk_target" }}
with_items: '{{ files }}'
- stat:
path: '{{ item }}'
register: out_stat_remote
failed_when: out_stat_remote.stat.lnk_target != out_stat_source|json_query(get_lnk_target)
vars:
get_lnk_target: |
{{ "results[0].stat.lnk_target" }}
with_items: '{{ files }}'
test2.yml的输出
$ ansible-playbook -itarget, test2.yml
PLAY [all] *******************************************************************************************************************************************************************************************************
TASK [stat] ******************************************************************************************************************************************************************************************************
ok: [target -> localhost] => (item=/tmp/test)
ok: [target -> localhost] => (item=/tmp/test2)
TASK [debug] *****************************************************************************************************************************************************************************************************
ok: [target] => (item=None) => {
"msg": [
"test.target"
]
}
ok: [target] => (item=None) => {
"msg": [
"test2.target"
]
}
TASK [stat] ******************************************************************************************************************************************************************************************************
ok: [target] => (item=/tmp/test)
failed: [target] (item=/tmp/test2) => {"changed": false, "failed_when_result": true, "item": "/tmp/test2", "stat": {"atime": 1587625535.6842694, "attr_flags": "", "attributes": [], "block_size": 4096, "blocks": 0, "charset": "binary", "ctime": 1587625511.3281388, "dev": 64768, "device_type": 0, "executable": false, "exists": true, "gid": 100, "gr_name": "users", "inode": 27473317, "isblk": false, "ischr": false, "isdir": false, "isfifo": false, "isgid": false, "islnk": true, "isreg": false, "issock": false, "isuid": false, "lnk_source": "/tmp/test2.target", "lnk_target": "test2.target", "mimetype": "inode/symlink", "mode": "0777", "mtime": 1587625511.3281388, "nlink": 1, "path": "/tmp/test2", "pw_name": "myuser", "readable": false, "rgrp": true, "roth": true, "rusr": true, "size": 12, "uid": 10004, "version": null, "wgrp": true, "woth": true, "writeable": false, "wusr": true, "xgrp": true, "xoth": true, "xusr": true}}
to retry, use: --limit @/home/myuser/test.ansible/test2.retry
PLAY RECAP *******************************************************************************************************************************************************************************************************
target : ok=2 changed=0 unreachable=0 failed=1
谢谢