티스토리 뷰

엔지니어링

Python Ansible 셋팅 & 사용법

warpmemory 2017. 9. 18. 10:44

현재 Ansible 컨트럴 서버는 python2는 2.6이상, python3는 3.5 이상이 설치 되어있어야한다.

설치 방법은 어러가지가 있으나, pip 로 설치하면 의존성 패키지까지 바로설치 되기 때문에 제일 간편하게 설치할 수 있다.

python 설치
easy_install pip

pip install ansible


기본값 설정 파일 /etc/ansible/ansible.cfg 생성

기본 설정 파일 : https://raw.githubusercontent.com/ansible/ansible/devel/examples/ansible.cfg

설정 디스크립션 : http://docs.ansible.com/ansible/latest/intro_configuration.html


inventory 설정 파일 /etc/ansible/hosts 생성


[webserver:vars]

ansible_connection=ssh

ansible_ssh_user=root

ansible_ssh_pass=webserverpasswd

ansible_python_interpreter=/usr/local/python2.6/bin/python


[dbserver:vars]

ansible_connection=ssh

ansible_ssh_user=root

ansible_ssh_pass=dbserverpasswd

ansible_python_interpreter=/usr/local/python2.7/bin/python


[webserver]

web1.com

web2.com

web3.com


[dbserver]

db1.com

db2.com


ini 설정 타입으로 섹션별로 서버 설정을 달리 할 수 있다.

Ansible 관리 노드 서버들은 python2 이상이 설치되어있어야하고, pytyon2.5 이하는 simplejson 모듈이 설치 되어있어야한다.
(ansible python2server --sudo -m raw -a "yum install -y python2 python-simplejson")


ping 테스트(접속 테스트)

ansible all -m ping


커멘드 모듈

ansible all -m command -a "hostname -s"


playbook 설정 파일 생성


hostname.yaml 파일 생성

- name: echo_hostname

  hosts:

    - webserver

    - dbserver

  tasks:

    - name: short_hostname

      command: "hostname -s"


playbook 실행

ansible-playbook hostname.yaml


dist_file.yaml 파일 생성(파일 전송 예제)

- name: dist file

  hosts:

    - test

  tasks:

    - name: file copy

      copy:

        src: /home/test/{{item}}

        dest: /tmp/.

        mode: 0644

      with_items:

        - test1.txt

        - test2.txt

ansible-playbook dist_file.yaml

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
페이지
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함