일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- 랜카드인식불량
- docker
- vagrant kubernetes
- docker network
- ssh
- lvcreate
- journalctl
- nmcli
- ansible
- newbingai
- mount
- Kubernetes
- yum
- 리다이렉션
- chmod
- permission
- HTTPD
- 프로세스
- vgcreate
- M365필터
- 엑셀파일명변경
- firewalld
- swapon
- pvcreate
- tar
- 같은폴더
- chatGPT
- MSBing
- 날짜변경
- docker image
- Today
- Total
becool
20210729 (목) ansible 변수, 반복문, 조건문 본문
9:34 review
변수이름 규칙
인벤토리 - 호스트파일 내에 '호스트 변수' 사용
플레이북 - 변수 사용시 ':' 기호로 정의 (인벤토리 파일에 정의시 '=' 기호로 정의)
Ansible Facts (플레이북 실행시, gathering facts 가 실행됨)
Ansible 로 관리하는 관리노드(Managed node) 시스템의 OS 정보, IP주소, 디스크, NIC, 환경변수, CPU 정보, 마운트 정보 등의 시스템 정보를 플레이 실행시 수집하여 저장하는 변수이다.
Playbook 실행시 기본값으로 Ansible Facts 를 수집하게 되며 수집여부를 설정하기 위한 변수는 gather_facts이다. [true:수집활성화 false:수집비활성화]
[user@ansible-server work]$ ansible all -m setup → 각 호스트별 facts 출력
[user@ansible-server work]$ ansible all -m setup |grep ansible_hostname
"ansible_hostname": "ansible-node01",
"ansible_hostname": "ansible-node02",
"ansible_hostname": "ansible-node03",
※ facts 수집비활성화 : 플레이북에서 gather_facts: false (기본값 true)
Ansible 주요 Facts
ansible_facts['hostname'] : 간략한 Hostname (= ansible_hostname)
ansible_facts['fqdn'] : FQDN 형태의 Hostname
ansible_facts['default_ipv4'] ['addresses'] : IPv4 address
ansible_facts['interfaces'] : 시스템에 설치된 모든 NIC 정보
ansible_facts['kernel'] : 커널 버전 정보
ansible_facts['os_family'] : 리눅스 배포판 종류
"ansible_default_ipv4": { "address": "10.0.2.11", "alias": "enp0s3", "broadcast": "10.0.2.255", "gateway": "10.0.2.1", "interface": "enp0s3", "macaddress": "08:00:27:fb:bf:a9", "mtu": 1500, "netmask": "255.255.255.0", "network": "10.0.2.0", "type": "ether" "ansible_kernel": "3.10.0-1160.21.1.el7.x86_64", "ansible_kernel_version": "#1 SMP Tue Mar 16 18:28:22 UTC 2021", "ansible_os_family": "RedHat", |
[user@ansible-server work]$ echo "Hello Ansible" > ~user/original_file
[user@ansible-server work]$ cat ~user/original_file
"ansible_local": {
"custom": {
"genaral": {
"package_name": "httpd",
"service_name": "httpd",
"state": "started"
},
"test1": {
"app_developer": "ansible_user",
"app_name": "apply_form",
"app_ver": "\"1.0\""
①custom facts 생성
②custom facts 배포
③플레이북 생성(custom facts 변수를 이용한 패키지 설치, 서비스 실행)
④플레이북 배포
[user@ansible-server work]$ vim /etc/ansible/facts.d/custom.fact → 지정된 컨픽경로에 파일 생성 [general] package_name = httpd service_name = httpd state = started [test1] app_name = apply_form app_ver = "1.0" app_developer = ansible_user [user@ansible-server work]$ cat 0729_ansible_fact_custom.yaml --- - name: Install custom ansible facts - managed nodes hosts: all become: true vars: remote_dir: /etc/ansible/facts.d → 서버의 로컬 경로를 지정 custom_facts: custom.fact → 서버의 로컬 (경로내) 파일을 지정 tasks: - name: Create directory (managed nodes) file: state: directory recurse: true path: "{{ remote_dir }}" - name: Copy custom facts copy: src: /etc/ansible/facts.d/custom.fact dest: "{{ remote_dir }}" [user@ansible-server work]$ ansible-playbook 0729_ansible_fact_custom.yaml [user@ansible-server work]$ ansible all -m setup >> fileA [user@ansible-server work]$ vim fileA → cumstom facts 배포 후 setup 모듈로 ansible_local 편집되었는지 확인 "ansible_local": { "custom": { "genaral": { "package_name": "httpd", "service_name": "httpd", "state": "started" }, "test1": { "app_developer": "ansible_user", "app_name": "apply_form", "app_ver": "\"1.0\"" [user@ansible-server work]$ cat facts.yaml --- - name: Install Apache Package and Service hosts: all become: true tasks: - name: Install Package yum: name: "{{ ansible_facts['ansible_local']['custom']['general']['package_name'] }}" → 정의된 섹션, 구역을 지정하여 작성 state: latest - name: start Apache web service service: name: "{{ ansible_facts['ansible_local']['custom']['general']['service_name'] }}" state: "{{ ansible_facts['ansible_local']['custom']['general']['state'] }}" [user@ansible-server work]$ ansible-playbook facts.yaml |
Ansible Facts를 사용하여 웹 서비스 첫 페이지에 "Welcome to my ansible-node 노드번호 (192.168.56.xx) " 를 표시
①고유의 노드번호, ip주소를 가르키는 facts 찾기 ( ansible -m setup 모듈 사용)
②copy 모듈을 사용하여 각 노드들의 index파일에 넣을 컨텐츠를 변수형태로 작성
③curl 명령어로 잘 반영되었는지 확인
--- - name: Install custom ansible facts - managed nodes hosts: all become: true vars: remote_dir: /etc/ansible/facts.d custom_facts: custom.fact tasks: - name: "Create Web Service Greetings" copy: content: "Welcome to my {{ ansible_facts['hostname'] }} {{ ansible_facts['default_ipv4']['address'] }} " dest: /var/www/html/index.html [user@ansible-server work]$ curl 192.168.56.11 Welcome to my ansible-node01 10.0.2.11 [user@ansible-server work]$ curl 192.168.56.12 Welcome to my ansible-node02 10.0.2.12 [user@ansible-server work]$ curl 192.168.56.13 Welcome to my ansible-node03 10.0.2.13 ### facts 의 다른 값을 호출하여 적용할 수도 있다.### content: "Welcome to my {{ ansible_facts['hostname'] }} {{ ansible_facts['enp0s8']['ipv4']['address'] }} " [user@ansible-server work]$ curl 192.168.56.11 Welcome to my ansible-node01 192.168.56.11 [user@ansible-server work]$ curl 192.168.56.12 Welcome to my ansible-node02 192.168.56.12 [user@ansible-server work]$ curl 192.168.56.13 Welcome to my ansible-node03 192.168.56.13 |
반복문
- 동일한 작업을 여러번 수행해야 하는 경우에 사용하는 제어문
- loop 문
[user@ansible-server work]$ cat 0729_loop_useradd2.yaml --- - name: useradd users user: name: "{{ item }}" state: present group: wheel loop: - user01 - user02 [user@ansible-server work]$ cat 0729_loop_useradd3.yaml --- - name: add several users hosts: all become: true vars: user_list: ['testuser01', 'testuser02'] tasks: - name: useradd users user: name: "{{ item }}" state: present group: wheel loop: "{{ user_list }}" [user@ansible-server work]$ ansible-playbook 0729_loop_useradd2.yaml TASK [useradd users] *************************************************************************************************** changed: [192.168.56.12] => (item=testuser01) changed: [192.168.56.13] => (item=testuser01) changed: [192.168.56.11] => (item=testuser01) changed: [192.168.56.13] => (item=testuser02) changed: [192.168.56.12] => (item=testuser02) changed: [192.168.56.11] => (item=testuser02) --- - name: add several users hosts: all become: true tasks: - name: useradd users user: name: "{{ item.name }}" state: present groups: "{{ item.groups }}" loop: - { name: 'testuser1', groups: 'wheel' } - { name: 'testuser2', groups: 'nobody' } [user@ansible-server work]$ ansible-playbook 0729_loop_useradd4.yaml changed: [192.168.56.11] => (item={u'name': u'testuser1', u'groups': u'wheel'}) changed: [192.168.56.12] => (item={u'name': u'testuser1', u'groups': u'wheel'}) changed: [192.168.56.13] => (item={u'name': u'testuser1', u'groups': u'wheel'}) changed: [192.168.56.12] => (item={u'name': u'testuser2', u'groups': u'nobody'}) changed: [192.168.56.11] => (item={u'name': u'testuser2', u'groups': u'nobody'}) changed: [192.168.56.13] => (item={u'name': u'testuser2', u'groups': u'nobody'}) [user@ansible-server work]$ vim 0729_with_together.yaml |
조건문
- 조건식의 참 거짓에 따라 실행할 것인지 실행하지 않을 것인지 결정하는 제어문
- when 문
--- - name: when statement example hosts: all vars: - run_task: true (false 인 경우 zsh가 설치되어있으면 skipping) tasks: - name: zsh is installed yum: name: zsh state: present when: run_task ### run_task: true 의 경우 결과값 : 실행함 ### TASK [zsh is installed] ************************************************************************************************ ok: [192.168.56.12] ok: [192.168.56.13] ok: [192.168.56.11] ### run_task: false 의 경우 결과값 : 실행되지 않음### TASK [zsh is installed] ************************************************************************************************ skipping: [192.168.56.11] skipping: [192.168.56.12] skipping: [192.168.56.13] |
'ansible' 카테고리의 다른 글
20210730 (금) playbook 연습장 (0) | 2021.07.30 |
---|---|
20210730 (금) ansible 조건문, 블록, 핸들러 (0) | 2021.07.30 |
20210728 (수) playbook 변수 실습 (0) | 2021.07.28 |
20210727 (화) ad-hoc 명령어 및 playbook 기초 (0) | 2021.07.27 |
20210726 (월) ansible 설치 (0) | 2021.07.26 |