본문 바로가기
Ansible

Ansible Conditionals

by 코딩+아빠 2023. 6. 27.

플레이 북에서 팩트 (원격 시스템에 대한 데이터), 변수 또는 이전 작업의 결과 값에 따라 다른 작업을 실행하거나 다른 목표를 가질 수 있다. 일부 변수의 값이 다른 변수의 값에 종속되도록 할 수 있다. 또는 호스트가 다른 기준과 일치하는지 여부에 따라 추가 호스트 그룹을 생성 할 수 있다. 조건부로이 모든 것을 할 수 있다.

# Inventory

# We have created a group for web servers. Similarly create a group for database servers named 'db_servers' and add db1 server to it
# --------------------------------
# Sample Inventory File

# Web Servers
web1 ansible_host=server1.company.com ansible_connection=ssh ansible_user=root ansible_ssh_pass=Password123!
web2 ansible_host=server2.company.com ansible_connection=ssh ansible_user=root ansible_ssh_pass=Password123!
web3 ansible_host=server3.company.com ansible_connection=ssh ansible_user=root ansible_ssh_pass=Password123!

# Database Servers
db1 ansible_host=server4.company.com ansible_connection=winrm ansible_user=administrator ansible_ssh_pass=Password123!

[web_servers]
web1
web2
web3

[db_servers]
db1
-
    name: 'Execute a script on all web server nodes'
    hosts: all_servers
    tasks:
        -
            service: 'name=mysql state=started'
            when: ansible_host == "server4.company.com"
-
    name: 'Am I an Adult or a Child?'
    hosts: localhost
    vars:
        age: 25
    tasks:
        -
            command: 'echo "I am a Child"'
            when: age<18
        -
            command: 'echo "I am an Adult"'
            when: age >= 18
-
    name: 'Add name server entry if not already entered'
    hosts: localhost
    tasks:
        -
            shell: 'cat /etc/resolv.conf'
            register: command_output
        -
            shell: 'echo "nameserver 10.0.250.10" >> /etc/resolv.conf'
            when: command_output.stdout.find('10.0.250.10') == -1
반응형

'Ansible' 카테고리의 다른 글

Ansible Roles  (0) 2023.06.28
Ansible Loops  (0) 2023.06.27
Ansible Variables  (0) 2023.06.27
Ansible Module - Package  (0) 2023.06.27
Ansible Module - Setup  (0) 2023.06.27