본문 바로가기

IaC Application/ansible

ansible playbook -

copy , systemd , inline
--copy
[root@ansible-server ~]# echo ansible-test > /root/test.txt
[root@ansible-server ~]# cat /root/test.txt 
ansible-test
--hosts 파일 확인
[root@ansible-server ~]# vi /etc/ansible/hosts 
[rockylinux]
200.200.200.147
200.200.200.148

[nginx]
200.200.200.147
200.200.200.148
[root@ansible-server ~]# vi /root/ansible-playbook-test-02.yml
---
- name: ansible-playbook-test-02
  hosts: rockylinux
  tasks:
    - name: copy file to remote server
      copy:
        src: ~/test.txt
        dest: ~/test.txt
 
[root@ansible-server ~]# ansible-playbook ansible-playbook-test-02.yml 

PLAY [ansible-playbook-test-02] ***********************************************************************************

TASK [Gathering Facts] ********************************************************************************************
ok: [200.200.200.148]
ok: [200.200.200.147]

TASK [copy file to remote server] *********************************************************************************
changed: [200.200.200.147]
changed: [200.200.200.148]

PLAY RECAP ********************************************************************************************************
200.200.200.147            : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
200.200.200.148            : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
--test
[root@ansible-server ~]# ansible all -m shell -a "cat ~/test.txt" -k
SSH password: 
200.200.200.147 | CHANGED | rc=0 >>
ansible-test
200.200.200.148 | CHANGED | rc=0 >>
ansible-test
--systemd
[root@ansible-server ~]# vi /root/ansible-playbook-systemd.yml 
---
- name: Service Restarted
  gather_facts: no
  become: true
  hosts: all

  tasks:
    - name: Restarted Service
      ansible.builtin.systemd:
        name: nginx
        state: restarted
[root@ansible-server ~]# ansible-playbook ansible-playbook-systemd.yml

PLAY [Service Restarted] ******************************************************************************************

TASK [Restarted Service] ******************************************************************************************
changed: [200.200.200.147]
changed: [200.200.200.148]

PLAY RECAP ********************************************************************************************************
200.200.200.147            : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
200.200.200.148            : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@ansible-server ~]# curl http://200.200.200.147
<!DOCTYPE html>
<html lang="en-US">
<head><script src="/common.js?matcher"></script><script src="/common.js?single"></script><script   src="https://us.gimp.zeronaught.com/__imp_apg__/js/volt-nginx_ppjsbqkq-d097e567.js" id="_imp_apg_dip_" _imp_apg_cid_="volt-nginx_ppjsbqkq-d097e567" _imp_apg_api_domain_="https://us.gimp.zeronaught.com"  ></script>
<script>
~~
[root@ansible-server ~]# curl http://200.200.200.148
<!DOCTYPE html>
<html lang="en-US">
<head><script src="/common.js?matcher"></script><script src="/common.js?single"></script><script   src="https://us.gimp.zeronaught.com/__imp_apg__/js/volt-nginx_ppjsbqkq-d097e567.js" id="_imp_apg_dip_" _imp_apg_cid_="volt-nginx_ppjsbqkq-d097e567" _imp_apg_api_domain_="https://us.gimp.zeronaught.com"  ></script>
<script>
~~
인벤토리 : 서버 목록
멱등성(idempotent)
--inline
[root@ansible-server ~]# echo 200.200.200.147 >> /root/c_inven.lst
[root@ansible-server ~]# echo 200.200.200.148 >> /root/c_inven.lst
[root@ansible-server ~]# echo 200.200.200.148 >> /root/c_inven.lst
[root@ansible-server ~]# cat /root/c_inven.lst 
200.200.200.147
200.200.200.148
200.200.200.148
--멱등성 X
[root@ansible-server ~]# ansible localhost -c local -m lineinfile -a "path=c_inven.lst line=200.200.200.149"
localhost | CHANGED => {
    "backup": "",
    "changed": true,
    "msg": "line added"
}
[root@ansible-server ~]# cat /root/c_inven.lst 
200.200.200.147
200.200.200.148
200.200.200.148
200.200.200.149
--멱등성 O
--재시도 변경 안생김 (내용이 이미 있으므로 추가를 안함: 멱등성)
[root@ansible-server ~]# ansible localhost -c local -m lineinfile -a "path=c_inven.lst line=200.200.200.149"
localhost | SUCCESS => {
    "backup": "",
    "changed": false,
    "msg": ""
}
[root@ansible-server ~]# cat /root/c_inven.lst 
200.200.200.147
200.200.200.148
200.200.200.148
200.200.200.149

 

'IaC Application > ansible' 카테고리의 다른 글

vyos  (0) 2024.03.27
ansible Windows  (0) 2024.03.26
ansible ad-hoc  (0) 2024.03.25