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

Bash 在Ansible中的文件中搜索正则表达式,bash,ansible,Bash,Ansible,我正在尝试将一个小的bash代码片段转换为Ansible,但我发现它很难实现。基本上,它首先检查/etc/redhat release是否存在。如果是,它正在寻找正则表达式模式*版本6.8*。如果找到该模式,则它正在检查另一个文件/bin/login.ori。如果它存在,那么它将执行两个操作 #fixed RHEL6.8/CentOS6.8 rlogin issue if [ -f /etc/redhat-release ]; then case `cat /etc/redhat-relea

我正在尝试将一个小的bash代码片段转换为Ansible,但我发现它很难实现。基本上,它首先检查
/etc/redhat 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
注意:我已经尝试了本文中的所有建议,但它不适用于此用例,因为我的用例中的
属性是动态的。

这是等效的

    - 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
(未测试)

注释

  • 必须启用以收集
    ansible.*
    变量

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

  • 我不确定修复“RHEL6.8/CentOS6.8”和测试“/etc/redhat release”是否一致(我无法访问6.8 atm)。根据您的需要调整区块中的条件


我认为varialbe
checkmyconf
没有stdout属性,lineinfle将修改文件的一行。