
공식 문서 : https://docs.ansible.com/
python3 --version
sudo apt install software-properties-common
sudo apt-add-repository ppa:ansible/ansible
sudo apt update
sudo apt install ansibleansible --version
/etc/ansible/hosts 에 설정을 합니다.[group_name]
서버주소
서버주소
…ex.[webservers]
192.168.0.241
192.168.0.242[webservers]
web1 ansible_host=192.168.0.241 ansible_user=사용자
web2 ansible_host=192.168.0.242 ansible_user=사용자ansible <타겟> -m 명령 -a 인자ansible all -m ping- hosts: localhost # 수행될 컴퓨터로 inventory에 있는 컴퓨터 이름이나 all 또는 localhost
become: yes # 특정 사용자로 전환(become)할지 여부
become_method: sudo # 관리자 모드로 수행
tasks:
- name: ensure apache is at the latest version
apt: name=apache2 state=latest
- name: ensure apache is running
service: name=apache2 state=started enabled=yes실행
ansible-playbook playbook.yaml

apache2 설치 및 실행 확인
sudo systemctl status apache2


-f 스레드개수 를 설정하면 태스크를 병렬로 수행할 수 있습니다.ansible-playbook -f 2 playbook.yaml
- hosts: localhost # 수행될 컴퓨터로 inventory에 있는 컴퓨터 이름이나 all 또는 localhost
become: yes # 특정 사용자로 전환(become)할지 여부
become_method: sudo # 관리자 모드로 수행
tasks:
- name: ensure apache is at the latest version
apt: name=apache2 state=latest
- name: ensure apache is running
service: name=apache2 state=started enabled=yes
- name: copy configuration
copy:
src: foo.conf
dest: /etc/foo.conf
notify:
- restart apache
handlers:
- name: restart apache
service:
name: apache2
state: restartedtouch foo.confansible-playbook playbook.yamlfoo.conf 파일이 생성되었기 때문에 ansible이 변경을 감지했다.
ansible-playbook playbook.yaml그대로 다시 실행하면, foo.conf 파일의 변경이 없기 때문에, change 되지 않는다.
echo "content" > foo.confansible-playbook playbook.yamlfoo.conf 파일을 변경한 후 실행하면 변경을 감지하여 수행한다.
playbook.yaml 파일 수정
- hosts: localhost # 수행될 컴퓨터로 inventory에 있는 컴퓨터 이름이나 all 또는 localhost
become: yes # 특정 사용자로 전환(become)할지 여부
become_method: sudo # 관리자 모드로 수행
vars:
http_port: 8080
tasks:
- name: print port number
debug:
msg: "Port number: {{http_port}}"
- name: ensure apache is at the latest version
apt: name=apache2 state=latest
- name: ensure apache is running
service: name=apache2 state=started enabled=yes
- name: copy configuration
copy:
src: foo.conf
dest: /etc/foo.conf
notify:
- restart apache
handlers:
- name: restart apache
service:
name: apache2
state: restarted
ansible-playbook playbook.yaml변수로 선언한 http_port 의 값이 8080 으로 출력되었다.


ansible-galaxy install geerlingguy.mysql
- hosts: localhost
become: yes
become_method: sudo
roles:
- role: geerlingguy.mysqlansible-playbook mysql-playbook.yamlsystemctl status mysql
- hosts: localhost
become: yes
become_method: sudo
tasks:
- name: Nginx Container
community.docker.docker_container:
name: nginx-container
image: nginx
state: started
published_ports:
- "80:80" # 호스트의 80번 포트를 컨테이너의 80번 포트와 연결ansible-playbook docker-playbook.yaml
docker ps
