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

Suhas Srivats Subburathinam

我正在尝试将一个小的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 repeat\n\nThe error appears to have been in '/remote/us01home53/subburat/snps-ansible/roles/qsc/hack/v1/tasks/main.yml': line 31, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n  - name: Greet the world if /etc/redhat-release contains \"*release 6.8*\"\n    ^ here\n"}
[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 repeat\n\nThe error appears to have been in '/remote/us01home53/subburat/snps-ansible/roles/qsc/hack/v1/tasks/main.yml': line 31, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n  - name: Greet the world if /etc/redhat-release contains \"*release 6.8*\"\n    ^ here\n"}
[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 repeat\n\nThe error appears to have been in '/remote/us01home53/subburat/snps-ansible/roles/qsc/hack/v1/tasks/main.yml': line 31, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n  - name: Greet the world if /etc/redhat-release contains \"*release 6.8*\"\n    ^ here\n"}
        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

(未测试)

笔记

  • 必须启用collect_facts来收集ansible_*变量。

  • 不必“强制”第一个副本,因为“目标”不存在。

  • 我不确定同时修复“ RHEL6.8 / CentOS6.8”和测试“ / etc / redhat-release”是否存在要求(我无法访问6.8 atm)。使块中的条件适合您的需求。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章