becool

20210806 (금) ansible role, dependency, strategy 본문

ansible

20210806 (금) ansible role, dependency, strategy

gusalstm 2021. 8. 6. 13:17
반응형

09:31 review

 

Role : defaults, files, handlers, tasks, templates, vars, meta

 ※roles 내 변수의 우선순위

   ①vars 디렉터리에 정의된 변수

   ②defaults 디렉터리에 정의된 변수

 ※ tasks와 role의 우선순위

 roles와 tasks는 같은 레벨에서 플레이북에 적어주게 되어있고, roles가 우선하여 실행되지만

 include_roles: 모듈로 tasks 내에 위치하게 적어주면 일반적인 tasks와 마찬가지로 순차적으로 실행된다.

 import_role: 모듈로 tasks 내에 적어주면 정적으로 역할을 불러와서 사용 가능

 

Role 종속성

 Role이 실행될 때 선행되어야하는 Role이 있는 경우 선행될 role 먼저 실행

 종속성에 대한 정의는 roles/ROLENAME/meta 디렉터리에 위치

 마찬가지로 tasks 내의 include_roles나 import_roles 모듈을 이용하는 경우 tasks -> 선행role -> 기본 role 순서로 진행

 

[user@ansible-server 20210806]$ cat 0806_include_role1.yaml
---
- name: include_role example1
  hosts: all

  tasks:
  - name: initial task Playbook
    debug:
      msg: Initial task
  - name: include_role playbook
    include_role:
      name: first_role

[user@ansible-server 20210806]$ ansible-playbook include_role1.yaml

PLAY [include_role example1] *******************************************************************************************

TASK [Gathering Facts] *************************************************************************************************
ok: [192.168.56.11]
ok: [192.168.56.12]
ok: [192.168.56.13]

TASK [initial task Playbook] *******************************************************************************************
ok: [192.168.56.11] => {
    "msg": "Initial task"
}


TASK [include_role playbook] *******************************************************************************************

TASK [first_role : compare defaults and vars directory] ****************************************************************
ok: [192.168.56.11] => {
    "msg": "var1: test, var2: 10"
}


TASK [first_role : print role_title] ***********************************************************************************
ok: [192.168.56.11] => {
    "msg": "role_title: my first role"
}


PLAY RECAP *************************************************************************************************************
192.168.56.11              : ok=4    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0


##### 종속성 #####
/ansbile/roles/third_role/meta/main.yaml 설정


dependencies: []
  # List your role dependencies here, one per line. Be sure to remove the '[]' above,
  # if you add dependencies to this list.

↓ 아래와 같이 주석 처리 및 역할 입력 : third_role를 실행하기 앞서 first_role을 먼저 실행(playbook에 내용이 없더라도)

dependencies: #[]
  # List your role dependencies here, one per line. Be sure to remove the '[]' above,
  # if you add dependencies to this list.

- role: first_role




[user@ansible-server 20210806]$ cat 0806_third_role2.yaml
---
- name: third role example 2
  hosts: all

  tasks:
  - name: initial task playbook
    debug:
      msg: initial task

  - name: include_role
    include_role:
      name: third_role

[user@ansible-server 20210806]$ ansible-playbook 0806_third_role2.yaml

PLAY [third role example 2] ********************************************************************************************

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

TASK [initial task playbook] *******************************************************************************************
ok: [192.168.56.11] => {
    "msg": "initial task"
}

TASK [include_role] ****************************************************************************************************

TASK [first_role : compare defaults and vars directory] ****************************************************************
ok: [192.168.56.11] => {
    "msg": "var1: test, var2: 10"
}

TASK [first_role : print role_title] ***********************************************************************************
ok: [192.168.56.11] => {
    "msg": "role_title: my first role"
}

TASK [third_role : first task third role] ******************************************************************************
ok: [192.168.56.11] => {
    "msg": "first task"
}

TASK [third_role : second task third role] *****************************************************************************
ok: [192.168.56.11] => {
    "msg": "second role"
}

PLAY RECAP *************************************************************************************************************
192.168.56.11              : ok=6    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

[user@ansible-server 20210806]$


728x90
##### ansible-galaxy 명령어 #####
[user@ansible-server roles]$ ansible-galaxy --help
usage: ansible-galaxy [-h] [--version] [-v] TYPE ...

Perform various Role and Collection related operations.

positional arguments:
  TYPE
    collection   Manage an Ansible Galaxy collection.
    role         Manage an Ansible Galaxy role.

optional arguments:
  --version      show program s version number, config file location,
                 configured module search path, module location, executable
                 location and exit
  -h, --help     show this help message and exit
  -v, --verbose  verbose mode (-vvv for more, -vvvv to enable connection
                 debugging)

[user@ansible-server roles]$ ansible-galaxy role --help
usage: ansible-galaxy role [-h] ROLE_ACTION ...

positional arguments:
  ROLE_ACTION
    init       Initialize new role with the base structure of a role.
    remove     Delete roles from roles_path.
    delete     Removes the role from Galaxy. It does not remove or alter the
               actual GitHub repository.
    list       Show the name and version of each role installed in the
               roles_path.
    search     Search the Galaxy database by tags, platforms, author and
               multiple keywords.
    import     Import a role
    setup      Manage the integration between Galaxy and the given source.
    info       View more details about a specific role.
    install    Install role(s) from file(s), URL(s) or Ansible Galaxy

optional arguments:
  -h, --help   show this help message and exit
[user@ansible-server roles]$ ansible-galaxy role init second_role --offline
- Role second_role was created successfully

 

Ansible Strategies : linear, free, host pinned, debug

 forks: 작업을 한번에 실행할 호스트 갯수 지정

 serials: 플레이를 한번에 실행할 호스트 갯수 지정

 throttle: 특정 작업이나 특정 블록을 한번에 실행할 수 있는 호스트 갯수제한

 

 

 

728x90
Comments