Python Ansible 셋팅 & 사용법
현재 Ansible 컨트럴 서버는 python2는 2.6이상, python3는 3.5 이상이 설치 되어있어야한다.
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