반응형

Concept

  • Control node: Ansible을 실행하는 Node
  • Managed node: Ansible 관리 대상이 되는 Node

 

Installation

$ sudo yum install epel-release
$ sudo yum install ansible

## Adding Ansible command shell completion
##  -> 이것은 편의를 위한 기능이니, 꼭 설치할 필요는 없다
$ sudo yum install python-argcomplete
$ sudo activate-global-python-argcomplete

## Let's check the version
$  ansible --version
ansible 2.9.25
...

## host inventroy file을 편집
$ cat /etc/ansible/hosts
...
[kubeworker]
10.10.12.64
10.10.12.71

[mynode]
10.10.12.64
...
$

##
## TEST for installation
##

$ ansible all --list-hosts
  hosts (2):
    10.10.12.64
    10.10.12.71
    
$ ansible all -m ping
10.10.12.64 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
10.10.12.71 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: ssh: connect to host 10.10.12.71 port 22: No route to host",
    "unreachable": true
}
$

 

Run example playbook - 1

##
## Run this playbook on control node
##

$  cat  playbook-example.yaml
---
- name: Add_sample_contents_to_file
  hosts: mynode

  tasks:
  - name: Add sample contents to file
    blockinfile:
      path: /root/sample_a.txt
      block: |
        [my test section]
        best contents
        greeting=hello       
$
$  ansible-playbook playbook-example.yaml

PLAY [Add_sample_file] ************************************************************************************************************

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

TASK [Add sample file] ************************************************************************************************************
changed: [10.10.12.64]

PLAY RECAP ************************************************************************************************************************
10.10.12.64                : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

$

##
## Check result of test on managed node(10.10.12.64)
##

$  cat  /root/sample_a.txt
# BEGIN ANSIBLE MANAGED BLOCK
[my test section]
best contents
greeting=hello
# END ANSIBLE MANAGED BLOCK
$

 

Run example playbook - 2

##
## Run this playbook on control node
##

$  cat  playbook-example-2.yaml
---
- name: Install pkg and create a file
  hosts: mynode

  tasks:
  - name: Install kubernetes pkg
    yum: name=tree state=present
    when: ansible_distribution == 'CentOS'
  - name: Run my command
    command: "touch /root/mydate.txt"
    when: ansible_distribution == 'CentOS'     
$
$  ansible-playbook playbook-example-2.yaml
...
$

##
## Check result of test on managed node(10.10.12.64)
##

$  which tree
/usr/bin/tree

$  ls /root/mydate.txt
/root/mydate.txt

 

Tip:   특정 User로 Ansible 실행

managed node에서 특정 user(예: root)로 실행되길 원한다면, 아래와 같이 설정 파일을 작성한다.

$  cat  /etc/ansible/ansible.cfg
...

[privilege_escalation]
become=True
become_method=sudo
become_user=root        ## Root user로 명령을 수행한다.
become_ask_pass=False   ## password를 묻지 않고 명령을 수행한다.

...

$

 

 

 

Reference

https://young-dev.com/infra/ansible_02/#

 

[Ansible] Ansible-Playbook 사용하기 #2

Ansible-Playbook 활용

young-dev.com

 

 

 

 

'CentOS' 카테고리의 다른 글

CPU Pinning 예제 코드  (0) 2022.06.17
Install Ansible AWX (version 17.1.0)  (0) 2021.11.16
Install OS with PXE and kickstart  (0) 2021.11.12
Samba(SMB) on CentOS  (0) 2021.07.10
Network config on CentOS 8  (0) 2021.07.10

+ Recent posts