조건이 발견되면 ansible이 터미널에 에코됩니다.

조건이 발견되면 ansible이 터미널에 에코됩니다.

lineinfile을 사용하여 조건이 충족되면 터미널에 출력을 인쇄하는 방법

- lineinfile:
    path: /home/pc/date.txt
    state: present
    line: 'yes'
    regexp: '^\s*Wednesday\s*$'
  check_mode: yes
  register: wednesdayOccur
- debug: msg="{{ wednesdayOccur.stdout }}" #Show on terminal "wednesday is found"
  when: wednesdayOccur == 1

답변1

주어진 파일

# test.yml
- hosts: localhost
  tasks:
  - lineinfile:
      path: /tmp/blah
      state: present
      line: 'yes'
      regexp: '^\s*Wednesday\s*$'
    check_mode: yes
    register: wednesdayOccur
  - debug: msg="wednesday is found"
    when: wednesdayOccur is defined

무엇보다도 이는 다음을 생성합니다.

$ printf Wednesday'\n' > /tmp/blah
$ ANSIBLE_NOCOLOR=1 ansible-playbook test.yml
...
TASK [debug] *******************************************************************
ok: [localhost] => {
    "msg": "wednesday is found"
}
...

이것이 Ansible이 디버그 출력을 렌더링하는 방법입니다.

변수에 뭔가가 필요한 경우 각 모듈은 자체 작업을 수행하므로 각 모듈에 대한 자세한 설명서를 참조해야 합니다.파일 라인또는 변수를 기록하여 debug주어진 조건에서 변수에 포함된 내용을 확인합니다.

# test2.yml
- hosts: localhost
  tasks:
  - lineinfile:
      path: /tmp/blah
      state: present
      line: 'yes'
      regexp: '^\s*Wednesday\s*$'
    check_mode: yes
    register: wednesdayOccur
  - debug: msg="wednesdayOccur"
    when: wednesdayOccur is defined

무엇보다도 이는 다음을 생성합니다.

TASK [debug] *******************************************************************
ok: [localhost] => {
    "msg": {
        "backup": "",
        "changed": true,
        "diff": [
            {
                "after": "",
                "after_header": "/tmp/blah (content)",
                "before": "",
                "before_header": "/tmp/blah (content)"
            },
            {
                "after_header": "/tmp/blah (file attributes)",
                "before_header": "/tmp/blah (file attributes)"
            }
        ],
        "failed": false,
        "msg": "line replaced"
    }
}

그런 다음 이를 어떻게 활용할 것인지 생각해야 합니다.

관련 정보