在Ansible中的文件中搜索正则表达式



我正试图将一个小的bash代码片段转换为Ansible,但我发现它很难实现。基本上,它首先检查/etc/redhat-release是否存在。如果是,则它正在查找正则表达式模式*release 6.8*。如果找到该模式,则它正在检查另一个文件/bin/login.ori。如果它存在,那么它将执行几个操作。

#fixed RHEL6.8/CentOS6.8 rlogin issue
if [ -f /etc/redhat-release ]; then
case `cat /etc/redhat-release` in
*'release 6.8'*)
if [ ! -e /bin/login.ori ]; then
cp -f
/bin/login /bin/login.ori
cp -f $MDIR/login.bin.68 /bin/login
restorecon /bin/login
fi
;;
esac
fi

以下是我迄今为止所尝试的:

- name: Fix RHEL6.8/CentOS6.8 rlogin issue
stat:
path: /etc/redhat-release
register: redhat_file
- debug:
msg: "File exists: {{ redhat_file }}"
when: redhat_file.stat.exists

- name: Check whether /etc/redhat-release contains "*release 6.8*"
lineinfile:
path: /etc/redhat-release
line: '*release 7.3*'
# insertafter: [main]
register: checkmyconf
when: redhat_file.stat.exists
- name: Greet the world if /etc/redhat-release contains "*release 6.8*"
debug:
msg: "{{ checkmyconf }}"
when: checkmyconf.stdout | match('*release 7.3.1611*')

但我的错误越来越少。请帮忙。

TASK [qsc/hack/v1 : Check whether /etc/redhat-release contains "*release 6.8*"] *******************************************************
ok: [ansible-poc-cos6]
ok: [ansible-poc-rhel6]
ok: [ansible-poc-centos7]
[DEPRECATION WARNING]: Using tests as filters is deprecated. Instead of using `result|match` use `result is match`. This feature will
be removed in version 2.9. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
TASK [qsc/hack/v1 : Greet the world if /etc/redhat-release contains "*release 6.8*"] **************************************************
fatal: [ansible-poc-cos6]: FAILED! => {"msg": "The conditional check 'checkmyconf.stdout | match('*release 7.3.1611*')' failed. The error was: nothing to repeatnnThe error appears to have been in '/remote/us01home53/subburat/snps-ansible/roles/qsc/hack/v1/tasks/main.yml': line 31, column 5, but maynbe elsewhere in the file depending on the exact syntax problem.nnThe offending line appears to be:nnn  - name: Greet the world if /etc/redhat-release contains "*release 6.8*"n    ^ heren"}
[DEPRECATION WARNING]: Using tests as filters is deprecated. Instead of using `result|match` use `result is match`. This feature will
be removed in version 2.9. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
fatal: [ansible-poc-rhel6]: FAILED! => {"msg": "The conditional check 'checkmyconf.stdout | match('*release 7.3.1611*')' failed. The error was: nothing to repeatnnThe error appears to have been in '/remote/us01home53/subburat/snps-ansible/roles/qsc/hack/v1/tasks/main.yml': line 31, column 5, but maynbe elsewhere in the file depending on the exact syntax problem.nnThe offending line appears to be:nnn  - name: Greet the world if /etc/redhat-release contains "*release 6.8*"n    ^ heren"}
[DEPRECATION WARNING]: Using tests as filters is deprecated. Instead of using `result|match` use `result is match`. This feature will
be removed in version 2.9. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
fatal: [ansible-poc-centos7]: FAILED! => {"msg": "The conditional check 'checkmyconf.stdout | match('*release 7.3.1611*')' failed. The error was: nothing to repeatnnThe error appears to have been in '/remote/us01home53/subburat/snps-ansible/roles/qsc/hack/v1/tasks/main.yml': line 31, column 5, but maynbe elsewhere in the file depending on the exact syntax problem.nnThe offending line appears to be:nnn  - name: Greet the world if /etc/redhat-release contains "*release 6.8*"n    ^ heren"}
to retry, use: --limit @/remote/us01home53/subburat/snps-ansible/push-full.retry
PLAY RECAP ****************************************************************************************************************************
ansible-poc-centos7        : ok=3    changed=0    unreachable=0    failed=1
ansible-poc-cos6           : ok=3    changed=0    unreachable=0    failed=1
ansible-poc-rhel6          : ok=3    changed=0    unreachable=0    failed=1

注意:我已经尝试了此链接中的所有建议,但它不适用于此用例,因为我的用例中的line属性是动态的。

这是等效的

- stat:
path: /bin/login.ori
register: result
- block:
- copy:
src: /bin/login
dest: /bin/login.ori
remote_src: true
- copy:
src: "{{ ansible_env.MDIR }}/login.bin.68"
dest: /bin/login
remote_src: true
force: true
- command: restorecon /bin/login
when:
- ansible_distribution == 'Red Hat Enterprise Linux'
- ansible_distribution_version == '6.8'
- not result.stat.exists|bool

(未测试(

票据

必须启用
  • gather_facts才能收集ansible_*变量。

  • 没有必要"强制"第一个副本,因为"dest"不存在。

  • 我不确定修复"RHEL6.8/CentOS6.8"和仅测试"/etc/redhat release"的存在的要求是否一致(我无法访问6.8 atm(。根据您的需要调整街区的条件。

相关内容

  • 没有找到相关文章

最新更新