Need to hide failed log in ansible task

[亡魂溺海] 提交于 2021-02-18 10:43:30

问题


I am new to ansible tasks, am creating a yml which performs a login operation and if login gets failed, some script need to be called.

  - name: Logging Action
    shell: "/usr/local/bin/cqlsh -u xyzyx -p 1234abc"
    register: loginoutput
    ignore_errors: yes
    no_log: True


  - name: Run the cql script to create new user
    shell:  sh create-new-user.cql"
    when: loginoutput|failed

for the above one i created taks that works fine.

My question, when performing login operation - it got failed and showing error messages like below...

I dont want to display the log messgaes even not any failed string in logs.

I tried no_log: True , this gives

failed: [127.0.0.1] => {"censored": "results hidden due to no_log parameter", "changed": true, "rc": 1}

I don't want to show failed string in o/p.


回答1:


You can't. no_log: Trueis the best there is to prevent output.

If it helps, beside ignore_errors: yes, there is the failed_when option where you can define exactly what makes the task fail. The idea would be to not let the task fail in the first place.

- name: Logging Action
  shell: "/usr/local/bin/cqlsh -u xyzyx -p 1234abc"
  register: loginoutput
  failed_when: false
  no_log: True

So in this case, the task will never fail (false) but you can provide any sophisticated condition which may be based on the output of the task.

Now, the task did not fail, but the return code of cqlsh still is 1, which you can use in the condition of the next task.

- name: Run the cql script to create new user
  shell:  sh create-new-user.cql"
  when: loginoutput.rc == 1

In theory this should have the same outcome as before, just that the first task is not marked as failed and therefore no_log: True should have the desired effect.




回答2:


If you are expecting failures, I wonder if you can adjust your inventory to just not run these on the expected failed nodes. Check out groups and perhaps you can build a group (on setup a dynamic inventory).

Another strategy would be to do a check for some file or account to exist to identify where this is likely to succeed and then only run the task when that has succeeded.



来源:https://stackoverflow.com/questions/35723913/need-to-hide-failed-log-in-ansible-task

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!