1

I am trying to convert a small bash code snippet to Ansible but I am finding it hard to implement. Basically, it is first checking if /etc/redhat-release exists. If yes, it is looking for a regex pattern *release 6.8*. If the pattern is found, then it is checking for another file /bin/login.ori. If it exists, then it performs a couple of operations.

#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

Here is what I have tried so far:

- 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*')

But I am getting below error. Kindly help.

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

Note: I have tried all the suggestions in this link but it does not work for this use-case as the line attribute in my use-case is dynamic.

  • I think the varialbe `checkmyconf` does not have the attribute stdout, lineinfile is to modify the one line for the file. – Z.Liu May 07 '20 at 06:38

1 Answers1

2

This is the equivalent

    - 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

(not tested)

Notes

  • gather_facts must be enabled to collect ansible_* variables.

  • It's not necessary to "force" the first copy because the "dest" does not exist.

  • I'm not sure if the requirement to fix both "RHEL6.8/CentOS6.8" and testing the existence of "/etc/redhat-release" only is consistent (I have no access to 6.8 atm). Fit the conditions in the block to your needs.

Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63