본문 바로가기

리눅스

[리눅스] ansible copy모듈(copy module)

반응형

ansible copy모듈(copy module)

ansible --version
$ ansible --version
ansible [core 2.12.5]
  config file = /home/vagrant/.ansible.cfg
  configured module search path = ['/home/vagrant/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.10/dist-packages/ansible
  ansible collection location = /home/vagrant/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/local/bin/ansible
  python version = 3.10.6 (main, Aug 10 2022, 11:40:04) [GCC 11.3.0]
  jinja version = 2.11.3
  libyaml = True

hosts(/etc/hosts) 파일 업데이트

기존 호스트 파일 확인

ansible -i ~/kubespray/inventory/mycluster/inventory.ini kube-node1 -m shell -a 'cat /etc/hosts'
$ ansible -i ~/kubespray/inventory/mycluster/inventory.ini kube-node1 -m shell -a 'cat /etc/hosts'
kube-node1 | CHANGED | rc=0 >>
127.0.0.1 localhost localhost.localdomain

# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback localhost6 localhost6.localdomain
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

# Kubernetes
192.168.0.51    kube-control1
192.168.0.59    kube-nfs1
192.168.0.61    kube-node1
192.168.0.62    kube-node2
192.168.0.63    kube-node3
192.168.0.74    kube-node4
192.168.0.75    kube-node5
192.168.0.76    kube-node6

# Ansible inventory hosts BEGIN
192.168.0.51 kube-control1.cluster.local kube-control1 
192.168.0.61 kube-node1.cluster.local kube-node1 
192.168.0.62 kube-node2.cluster.local kube-node2 
192.168.0.63 kube-node3.cluster.local kube-node3 
192.168.0.74 kube-node4.cluster.local kube-node4 
192.168.0.75 kube-node5.cluster.local kube-node5 
192.168.0.76 kube-node6.cluster.local kube-node6 
# Ansible inventory hosts END

hostsfileUpdate.yaml 파일 작성

mkdir -p templates/etc
vim templates/etc/hosts.j2
$ cat templates/etc/hosts.j2 
127.0.0.1 localhost localhost.localdomain

# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback localhost6 localhost6.localdomain
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

# Kubernetes
192.168.0.51    kube-control1
192.168.0.59    kube-nfs1
192.168.0.61    kube-node1
192.168.0.62    kube-node2
192.168.0.63    kube-node3
192.168.0.74    kube-node4
192.168.0.75    kube-node5
192.168.0.76    kube-node6

# Ansible inventory hosts BEGIN
192.168.0.51 kube-control1.cluster.local kube-control1 
192.168.0.61 kube-node1.cluster.local kube-node1 
192.168.0.62 kube-node2.cluster.local kube-node2 
192.168.0.63 kube-node3.cluster.local kube-node3 
192.168.0.74 kube-node4.cluster.local kube-node4 
192.168.0.75 kube-node5.cluster.local kube-node5 
192.168.0.76 kube-node6.cluster.local kube-node6 
# Ansible inventory hosts END
vim hostsfileUpdate.yaml
$ cat hostsfileUpdate.yaml
---
- hosts: all
  gather_facts: False

  tasks:
    - name: check /tmp/test file
      file:
        path: /tmp/test
        state: touch
      tags:
        - test_file_check

    - name: update /etc/hosts file
      become: true
      copy:
        src: templates/etc/hosts.j2
        dest: /etc/hosts
        owner: root
        group: root
        mode: 0600
        backup: yes
      register: hosts_file_copy_result
      tags:
        - hosts_file_copy
    - debug: msg="{{ hosts_file_copy_result }}"

ansible-playbook 실행

ansible-playbook -i ~/kubespray/inventory/mycluster/inventory.ini hostsfileUpdate.yaml --limit "kube-node"
$ ansible-playbook -i ~/kubespray/inventory/mycluster/inventory.ini hostsfileUpdate.yaml --limit "kube-node5"

PLAY [all] ********************************************************************************************************

TASK [check /tmp/test file] ***************************************************************************************
changed: [kube-node5]

TASK [update /etc/hosts file] *************************************************************************************
changed: [kube-node5]

TASK [debug] ******************************************************************************************************
ok: [kube-node5] => {
    "msg": {
        "backup_file": "/etc/hosts.588208.2022-10-19@15:27:04~",
        "changed": true,
        "checksum": "b0fe69ac3226bca7161dc8024621064812357ae8",
        "dest": "/etc/hosts",
        "diff": [],
        "failed": false,
        "gid": 0,
        "group": "root",
        "md5sum": "0466e22a83f7078ab238331ea5d5c74f",
        "mode": "0600",
        "owner": "root",
        "size": 907,
        "src": "/home/vagrant/.ansible/tmp/ansible-tmp-1666160821.692827-2302550-80854982873732/source",
        "state": "file",
        "uid": 0
    }
}

PLAY RECAP ********************************************************************************************************
kube-node5                 : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

변경된 호스트 파일 확인

ansible -i ~/kubespray/inventory/mycluster/inventory.ini kube_node -m shell -a 'cat /etc/hosts'

호스트 파일 추가(blockinfile module)

hostsfileAdd.yaml 파일 생성

vim hostsfileAdd.yaml
$ vim hostsfileAdd.yaml
---
- hosts: all
  gather_facts: False

  tasks:
    - name: add /etc/hosts file
      become: true
      blockinfile:
        path: /etc/hosts
        block: |
          1.1.1.1 test
        backup: yes
      register: hosts_file_blockinfile_result
      tags:
        - hosts_file_add
    - debug: msg="{{ hosts_file_blockinfile_result }}"

ansible-playbook 실행

ansible-playbook -i ~/kubespray/inventory/mycluster/inventory.ini hostsfileAdd.yaml --limit "kube-node1"
$ ansible-playbook -i ~/kubespray/inventory/mycluster/inventory.ini hostsfileAdd.yaml --limit "kube-node1"           

PLAY [all] ********************************************************************************************************

TASK [add /etc/hosts file] ****************************************************************************************
changed: [kube-node1]

TASK [debug] ******************************************************************************************************
ok: [kube-node1] => {
    "msg": {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python3"
        },
        "backup_file": "/etc/hosts.2203118.2022-10-19@15:42:14~",
        "changed": true,
        "diff": [
            {
                "after": "",
                "after_header": "/etc/hosts (content)",
                "before": "",
                "before_header": "/etc/hosts (content)"
            },
            {
                "after_header": "/etc/hosts (file attributes)",
                "before_header": "/etc/hosts (file attributes)"
            }
        ],
        "failed": false,
        "msg": "Block inserted"
    }
}

PLAY RECAP ********************************************************************************************************
kube-node1                 : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

변경된 hosts(/etc/hosts) 파일 확인

ansible -i ~/kubespray/inventory/mycluster/inventory.ini kube-node1 -m shell -a 'cat /etc/hosts'
$ ansible -i ~/kubespray/inventory/mycluster/inventory.ini kube-node1 -m shell -a 'cat /etc/hosts'
kube-node1 | CHANGED | rc=0 >>
127.0.0.1 localhost localhost.localdomain

# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback localhost6 localhost6.localdomain
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

# Kubernetes
192.168.0.51    kube-control1
192.168.0.59    kube-nfs1
192.168.0.61    kube-node1
192.168.0.62    kube-node2
192.168.0.63    kube-node3
192.168.0.74    kube-node4
192.168.0.75    kube-node5
192.168.0.76    kube-node6

# Ansible inventory hosts BEGIN
192.168.0.51 kube-control1.cluster.local kube-control1 
192.168.0.61 kube-node1.cluster.local kube-node1 
192.168.0.62 kube-node2.cluster.local kube-node2 
192.168.0.63 kube-node3.cluster.local kube-node3 
192.168.0.74 kube-node4.cluster.local kube-node4 
192.168.0.75 kube-node5.cluster.local kube-node5 
192.168.0.76 kube-node6.cluster.local kube-node6 
# Ansible inventory hosts END
# BEGIN ANSIBLE MANAGED BLOCK
1.1.1.1 test
# END ANSIBLE MANAGED BLOCK

앤서블에 의해 추가된 부분

# BEGIN ANSIBLE MANAGED BLOCK
1.1.1.1 test
# END ANSIBLE MANAGED BLOCK
728x90
반응형