- name: download kubectl
get_url:
url: <https://dl.k8s.io/release/v1.33.0/bin/linux/amd64/kubectl>
dest: /usr/local/bin/kubectl
- name: download docker-repo
get_url:
url: <https://download.docker.com/linux/centos/docker-ce.repo>
dest: /etc/yum.repos.d/docker.ce.repo
| 모듈 | 설명 |
|---|---|
| lineinfile | 특정행이 파일에 있는지 확인하거나 역참조 정규식을 사용하여 기존행의 내용을 변경. |
| blockinfile | 마커 선으로 둘러싸인 여러 줄의 텍스트 블록을 삽입 및 업데이트 또는 제거 |
| copy | 특정 파일을 관리호스트의 특정 위치로 복사. file모듈과 유사한 속성을 지님 |
| fetch | copy와 반대로 동작. 관리호스트의 파일을 제어노드로 가져와 저장. |
| file | 권한, 소유권, SELinux 컨텍스트, 일반 파일의 타임 스탬프, 심볼릭 링크, 하드 링크 및 디렉터리와 같은 속성을 설정. 일반 파일, 심볼릭 링크, 하드 링크 및 디렉터리를 생성하거나 제거. |
| stat | linux stat 명령어와 유사. 파일의 상태 정보를 조회 |
관리 호스트에서 새로운 파일을 만들거나 파일 속성을 수정 가능
selinux 컨텍스트 설정 예시
-------------------------
---
- name: set context
hosts: web
tasks:
- name: create file
file:
path: /tmp/samba_file
owner: ansible-user
mode: '0640'
state: touch
- name: selinux config
file:
path: /tmp/samba_file
setype: samba_share_t
출력
----
[root@controller ansible-test]# ansible -a "ls -ltrZ /tmp/samba_file" web
serverc | CHANGED | rc=0 >>
-rw-r-----. 1 ansible-user root unconfined_u:object_r:samba_share_t:s0 0 7월 30 22:10 /tmp/samba_file
파일 제거
---------
---
- name:
hosts: web
tasks:
- name: delete file
file:
path: /tmp/samba_file
state: absent
file 모듈의 속성을 더해서 복사하거나 편집하는 기능을 제공
copy 모듈
* 작업디렉토리의 파일을 관리 호스트로 복사
----------------------------------------
- name: copy file
copy:
src: file
dest: /path/to/file
fetch 모듈
* 관리 호스트의 파일을 제어노드로 복사
------------------------------------
- name: fetch from host
fetch:
src: "/home/{{ user }}/.ssh/id_rsa.pub"
dest: "files/keys/{{ user }}.pub"
stat 모듈은 매개 변수는 파일 속성을 검색하고 파일의 체크섬을 확인하는 등의 기능 제공
- name: verity checksum
stat:
path: /path/to/fie
checksum_algorithm: md5
register: result
- debug:
msg: "Checksum is {{ result.stat.checksum }}"
파일의 내용을 확인하고 수정하도록 지원하는 모듈 lineinfile, blockinfile 존재
lineinline 사용
---------------
---
- name:
hosts: web
tasks:
- name: add new line
lineinfile:
line: '1.1.1.1 test.dom'
path: /etc/hosts
출력
----
[root@controller ansible-test]# ansible -a "cat /etc/hosts" web
serverc | CHANGED | rc=0 >>
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
1.1.1.1 test.com
blockinfile 사용
----------------
---
- name: test
gather_facts: no
hosts: web
tasks:
- name: block add
blockinfile:
block: |
hello world
itworks
path: /var/www/html/index.html
출력
----
[root@controller ansible-test]# ansible -a 'cat /var/www/html/index.html' web
serverc | CHANGED | rc=0 >>
hello world # 기존에 있던 내용
# BEGIN ANSIBLE MANAGED BLOCK
hello world
itworks
# END ANSIBLE MANAGED BLOCK
--
blockinfile 사용2
-----------------
---
- name: test
gather_facts: no
hosts: web
tasks:
- name: block add
blockinfile:
block: >
hello wordl
itworks
path: /var/www/html/index.html
출력
----
[root@controller ansible-test]# ansible -a 'cat /var/www/html/index.html' web
serverc | CHANGED | rc=0 >>
hello world
# BEGIN ANSIBLE MANAGED BLOCK
hello world itworks
# END ANSIBLE MANAGED BLOCK
