Ansible Common Mistakes

background

Because work often used ansible, so finishing the ansible common causes of errors and analysis, for yourself and for others to reference.

1.shell module Common Errors

1.1 shell encounters "msg": "non-zero return code"

ansible script as follows:

- name: Check the weblogic without wc
  shell: "ps -ef|grep weblogic|grep -v grep"
  register: check_weblogic0
  ignore_errors: true

ansible returns an error:

TASK [Check the weblogic without wc] *********************************************************************************************************************************************************************************************************fatal: [robin.org.cn]: FAILED! => {"changed": true, "cmd": "ps -ef|grep weblogic|grep -v grep", "delta": "0:00:00.036565", "end": "2020-02-23 18:08:03.100106", "msg": "non-zero return code", "rc": 1, "start": "2020-02-23 18:08:03.063541", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
...ignoring

ok: [robin.org.cn] => {
    "msg": {
        "changed": true,
        "cmd": "ps -ef|grep weblogic|grep -v grep",
        "delta": "0:00:00.036565",
        "end": "2020-02-23 18:08:03.100106",
        "failed": true,
        "msg": "non-zero return code",
        "rc": 1,
        "start": "2020-02-23 18:08:03.063541",
        "stderr": "",
        "stderr_lines": [],
        "stdout": "",
        "stdout_lines": []
    }
}

Cause Analysis:

When using the shell module and return an empty, ansible I will think wrong shell scripts, rc returns 1.

solution:

In the end of the command shell cat increases, the content will be returned to the cat passed through the pipeline, using rc cat returned is always 0. best solution, whether you want to get the entire contents of the return or returns the number of rows.

- name: Check the weblogic without wc but use cat
  shell: "ps -ef|grep weblogic|grep -v grep|cat"
  register: check_weblogic1
  ignore_errors: true

- name: print the check_weblogic1
  debug:
    msg: "{{ check_weblogic1 }}"

In the end of the command shell increase wc -l, number of rows returned, to ensure that shell never returns empty.

- name: Check the weblogic with wc
  shell: "ps -ef|grep weblogic|grep -v grep|wc -l"
  register: check_weblogic2
  ignore_errors: true

- name: print the check_weblogic2
  debug:
    msg: "{{ check_weblogic2.stdout|int }}"

In the final script face increased ignore_errors: true, most not recommended, unless the time being did not find the root cause of the emergency.

- name: Check the weblogic without wc
  shell: "ps -ef|grep weblogic|grep -v grep"
  register: check_weblogic0
  ignore_errors: true

2.copy module Common Errors

2.1 copy module encountered Remote copy does not support recursive copy of directory

ansible all -m copy -a 'src=/root/ansible/file1 dest=/etc/cc/file1 remote_src=yes backup=yes mode=0755'

TASK [cp files below folder4 to bak1] *************************************************************
ok: [localhost] => (item=subfile1)
ok: [localhost] => (item=subfile2)
failed: [localhost] (item=subfolder1) => {"changed": false, "item": "subfolder1", "msg": "Remote copy does not support recursive copy of directory: /apps/ansible-test/folder4/subfolder1"}
        to retry, use: --limit @/apps/ansible-test/test-cp.retry

PLAY RECAP ****************************************************************************************
localhost                  : ok=3    changed=1    unreachable=0    failed=1

Cause Analysis:

If the copy execution on a remote machine, execute cp command corresponding to the remote machine in the machine, remote_src: true. For asible 2.6, it supports only a single file copy, does not allow recursive copy. For ansible 2.8 has support recursive copy. See the official description: https: //docs.ansible.com/ansible/latest/modules/copy_module.html

solution:

Use ansible 2.8 or using linux shell cp -rf achieve recursive copy.
ansible all -m shell -a 'cp -rf /root/ansible/* /etc/cc/file1'

Guess you like

Origin www.cnblogs.com/lihuanhuan/p/12354112.html