728x90
#1 이론
1. 반복문
- Ansible 플레이북에서 작업이나 코드 일부를 여러 번 반복 사용
- loop 지시문에 반복할 값 명시, 순서대로 한번 씩 item 변수에 삽입
- loop안에 있는 요소는 위에서부터 순차적으로 하나씩 삽입
- 모두 다 넣으면 완료
- item : 반복문에서 사용하는 전용 변수
---
- name: Apache and Tomcat are running
service:
name: "{{ item }}"
state: started
loop:
- httpd
- tomcat
2. 조건문
- when : 테스트 조건이 값으로 입력
- 조건을 충족치 않으면 건너뜀
- 가장 마지막에 두는 것이 일반적
#2 실습
1. 반복문
- loop-exam1.yaml
-
더보기- name: Working with loop module
hosts: seoul
gather_facts: false
tasks:
- name: Iterate over hashes
debug:
msg:
- "Hello '{{ item.fname }}', nice to meet you"
- "Your last name as per our record is '{{ item.lname }}'"
- "Your country of residence is '{{ item.location }}'"
loop:
- { fname: 'deepak', lname: 'prasad', location: 'India' }
- { fname: 'amit', lname: 'kumar', location: 'Argentina' }
- { fname: 'rahul', lname: 'sharma', location: 'Canada' }
- { fname: 'vivek', lname: 'mittal', location: 'Brazil' }
- { fname: 'chul', lname: 'jeong', location: 'Korea' } - loop-exam2.yaml
-
더보기- name: Working with loop module
hosts: seoul
gather_facts: false
tasks:
- name: Iterate over dictionary
debug:
msg:
- "The {{ item.key }} of your car is {{ item.value }}"
loop: "{{ my_car | dict2items }}"
vars:
my_car:
Color: Black
Model: Benz GLC400
Transition: Manual
Price: $200,000 - loop-register.yaml
-
더보기- name: Loop Register Test
gather_facts: no
hosts: seoul
tasks:
- name: Looping Task
shell: "echo This is my item: {{ item }}"
loop:
- tiger
- lion
register: results1
- name: Show results variable
debug:
var: results1 - create_user.yaml
-
더보기- hosts: seoul
become: yes
tasks:
- name: Create new users
user:
name: '{{ item.name }}'
uid: '{{ item.uid }}'
state: present
loop:
- name: park
uid: 1020
- name: kim
uid: 1030
- name: lee
uid: 1040
vim loop-exam1.yaml
ansible-playbook loop-exam1.yaml
vim loop-exam2.yaml
ansible-playbook loop-exam2.yaml
vim loop-register.yaml
ansible-playbook loop-register.yaml
vim create_user.yaml
ansible-playbook create_user.yaml
loop-exam1 |
loop-exam2 |
loop-register |
create_user |
2. 조건문
- when-exam1.yaml
-
더보기- name: when conditional operator
hosts: seoul
gather_facts: false
vars:
x: 10
y: 15
list: [10, 20, 30]
tasks:
- debug:
msg:
- "The value of x is {{ x }} and the value of y is {{ y }}"
- "Our list contains: {{ list }}"
when: x == y
- debug:
msg: "x is present in the list"
when: x in list - when-exam2.yaml
-
더보기- name: List when test
hosts: seoul
vars:
supported_distros:
- RedHat
- CentOS
- Fedora
- Ubuntu
tasks:
- name: debug
debug:
msg: "{{ ansible_distribution }}"
- name: Install httpd using yum
yum:
name: httpd
state: present
when: ansible_distribution in supported_distros - when-exam3.yaml
-
더보기- hosts: seoul
vars:
user: nana
tasks:
- name: Check if file already exists
command: ls /home/{{ user }}/myfile
register: file_exists
ignore_errors: yes
- name: create file for user
file:
path: /home/{{ user }}/myfile
state: touch
when: file_exists is failed
- name: show message if file exists
debug:
msg: The user file already exists.
when: file_exists is succeeded
vim when-exam1.yaml
ansible-playbook when-exam1.yaml
vim when-exam2.yaml
ansible-playbook when-exam2.yaml
vim when-exam3.yaml
ansible-playbook when-exam3.yaml
#3 남은 것!
ansible error 처리, handler 및 playboo 확장인 roles, galaxy
ansible 응용 - docker, ec2생성은 다음에
'Ansible' 카테고리의 다른 글
Ansible 복습문제 2 (1) | 2023.08.18 |
---|---|
Ansible-playbook & multiple (0) | 2023.08.17 |
Ansible 복습문제 (0) | 2023.08.17 |
Ansible 기본 (1) | 2023.08.16 |