본문 바로가기

Automation Tools/Ansible

1.9 ansible | [test][playbook] docker container start|stop

반응형

  • tomcat, docker container 등을 start|stop|restart 할건지 prompt로 입력 받아 실행.
---
- name: startup or stop docker container
  hosts: all
  gather_facts: yes
  become: yes
  vars_prompt:
    - name: container_name # container 이름 입력 받는 변수
      prompt: " input container name? "
      private: no
    - name: select
      prompt: " choose start|stop|restart [default: start]? "
      private: no
      default: start # default 갓 설정
 
  tasks:
    - name: "{{ container_name }} docker container {{ select }} tasks starting.."
      shell: docker "{{ select }}" "{{ container_name }}"
      register: execute_result
 
    - debug:
        msg:
          - "{{ container_name }}"
          - "{{ select }}"
          - "{{ execute_result.stdout }}"
 
## 결과
[root@ANSIBLE ansible]# ansible-playbook prompt.yml
 input container name? : awx_web
 choose start|stop|restart [default: start]?  [start]: stop
 
PLAY [startup or stop docker container] ******************************************************************************************************************************************
 
TASK [Gathering Facts] ***********************************************************************************************************************************************************
ok: [test02]
 
TASK [awx_web docker container stop tasks starting..] ****************************************************************************************************************************
changed: [test02]
 
TASK [debug] *********************************************************************************************************************************************************************
ok: [test02] => {
    "msg": [
        "awx_web",
        "stop",
        "awx_web"
    ]
}
 
PLAY RECAP ***********************************************************************************************************************************************************************
test02                     : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  
 
[root@ANSIBLE ansible]# ansible-playbook prompt.yml
 input container name? : awx_web
 choose start|stop|restart [default: start]?  [start]:   
 
PLAY [startup or stop docker container] ******************************************************************************************************************************************
 
TASK [Gathering Facts] ***********************************************************************************************************************************************************
ok: [test02]
 
TASK [awx_web docker container start tasks starting..] ***************************************************************************************************************************
changed: [test02]
 
TASK [debug] *********************************************************************************************************************************************************************
ok: [test02] => {
    "msg": [
        "awx_web",
        "start",
        "awx_web"
    ]
}
 
PLAY RECAP ***********************************************************************************************************************************************************************
test02                     : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
  • tomcat
---
- name: startup or stop servers
  hosts: all
  gather_facts: yes  vars_prompt:
    - name: select
      prompt: "startup or stop servers? [default: startup] "
      private: no
      default: 'startup' 
  tasks:
    - name: "{{ select }} tasks starting"
      shell: nohup "./{{ select }}.sh"
      args:
        chdir: /data/wasadm/apache-tomcat-8.5.50/bin ## tomcat 위치 이동
      become_user: yelloweb ## su user 설정
      register: execute_result 
    - debug:
        msg:
          - "{{ execute_result.stdout }}"
반응형