• include_tasks를 사용하여 각각 실행시키고자 하는 yaml 파일을 포함
  • 간단하게 또는 소규모로 작성할 경우 하나의 yaml 파일에다가 다 작성해도 괜찮음
  • 규모가 커지면 관리가 어려워지고 가독성이 떨어지기에 분리해서 yaml 파일을 작성

1. Centos7인지 Cenos6인지, Centos 아닌지 따라 ip 출력하는 playbook 코드(Include_tasks 사용)

  • centos_check.yml 파일은 메인 yaml 파일

  • include_tasks를 사용해서 조건에 맞게 centos7.yml, centos6.yml, not_centos.yml 파일을 가지고 올 수 있게 함

    $ vi centos_check.yml
    ---
    - name: print ipv4 address for ansible client
      hosts: all
      #gather_facts: no
    
      tasks:
        - name: debug by msg in Centos7
          include_tasks: centos7.yml
          when:
            - ansible_distribution == 'CentOS'
            - ansible_distribution_major_version == '7'
    
        - name: debug by msg in Centos6
          include_tasks: centos6.yml
          when:
            - ansible_distribution == 'CentOS'
            - ansible_distribution_major_version == '6'
    
        - name: debug by msg not in Cenos
          include_tasks: not_centos.yml
          when:
            - ansible_distribution != 'CentOS'

2. include_tasks를 통해 불러오는 보조용 yaml 파일들

2.1. centos7.yml 파일 내용

$ vi centos7.yml
- name: "Print IP in Centos7"
  debug:
    msg:
      - "Centos7 ip is {{ ansible_all_ipv4_addresses[0] }}"

2.2. centos6.yml 파일 내용

$ vi centos6.yml
- name: "Print IP in Centos6"
  debug:
    msg:
      - "Centos6 ip is {{ ansible_all_ipv4_addresses[0] }}"

2.3. not_centos.yml 파일 내용

$ vi not_centos.yml
- name: "Print IP not in Centos"
  debug:
    msg:
      - "Not Centos ip is {{ ansible_all_ipv4_addresses[0] }}"



3. centos_check.yml 파일 실행 결과

$ ansible-playbook centos_check.yml

PLAY [print ipv4 address for ansible client] *************************************************************************

TASK [Gathering Facts] *****************************************************************************************
ok: [192.168.1.13]

TASK [debug by msg in Centos7] **********************************************************************************
included: /root/centos7.yml for 192.168.1.13

TASK [Print IP in Centos7] ****************************************************************************************
ok: [192.168.1.13] => {
    "msg": [
        "Centos7 ip is 192.168.1.13"
    ]
}

TASK [debug by msg in Centos6] **********************************************************************************
skipping: [192.168.1.13]

TASK [debug by msg not in Cenos] *********************************************************************************
skipping: [192.168.1.13]

PLAY RECAP ****************************************************************************************************
192.168.1.13             : ok=3    changed=0    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0

+ Recent posts