[Ansible] 使用Ansible 安裝 Debian 套件 的yml
#Step 01 –
ssh-copy-id root@192.168.96.160
ssh 'root@192.168.96.160'
ansible -i hosts.yml -m shell -a "reboot" all
或
ansible -i hosts.yml -m shell -a "reboot" server01
##########################################################
ansible -i hosts.yml -m shell -a "apt install -y sudo" all
或
ansible -i hosts.yml -m apt -a "name=sudo" server01
###########################################################
# host 可以修改為單主機 也可以使用 all 來套用 host 所有清單
#vi set_python_3_Default_v1.yml
- name: Ansible Set 'python 3'
hosts: server01
tasks:
- name: set '1'
command: update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
register: result
- name: set '2'
command: update-alternatives --install /usr/bin/python python /usr/bin/python3.7 2
register: result
- name: print stdout
debug:
msg: "{{ result.stdout }}"
- name: Ansible Update Cache and install python3-pip
apt:
name:
- python3-pip
- name: set '3'
file:
src: "/usr/bin/pip3"
dest: "/usr/bin/pip"
state: link
# 執行 ansible-playbook set_python_3_Default_v1.yml -i hosts.yml
###########################################################
# vi test_apt_module4.yml 將下面內容貼到 檔案內
- name: Ansible apt module examples Common and general Pakete installieren
hosts: server01
become: true
tasks:
- name: Ansible Update Cache and install openssl
apt:
name:
- make
- gcc
- curl
- wget
- vim
- git-core
- git
state: latest
update_cache: yes
# 執行 ansible-playbook test_apt_module4.yml -i hosts.yml
#Step 02 –
[13:24:04 root@Debian10-3-Jenkins-Ansible-03 ~/anslab]# cat hosts.yml
[lemp]
server01 ansible_host=192.168.96.160
server02 ansible_host=192.168.96.161
server03 ansible_host=192.168.96.162
server04 ansible_host=192.168.96.163
server05 ansible_host=192.168.96.164
server06 ansible_host=192.168.96.165
[lemp:vars]
ansible_python_interpreter=/usr/bin/python3
[13:27:49 root@Debian10-3-Jenkins-Ansible-03 ~/anslab]#
#Step 03 –
我的 hosts.yml 範例
[lemp]
server01 ansible_host=192.168.96.160
server02 ansible_host=192.168.96.161
server03 ansible_host=192.168.96.162
server04 ansible_host=192.168.96.163
server05 ansible_host=192.168.96.164
server06 ansible_host=192.168.96.165
[lemp:vars]
ansible_python_interpreter=/usr/bin/python3
[webdb]
DBserver01 ansible_host=192.168.96.170
DBserver02 ansible_host=192.168.96.171
DBserver03 ansible_host=192.168.96.172
DBserver04 ansible_host=192.168.96.173
DBserver05 ansible_host=192.168.96.174
DBserver06 ansible_host=192.168.96.175
[webdb:vars]
ansible_python_interpreter=/usr/bin/python3
[DebianGroup:children]
lemp
webdb
################################################################
延續上面的 Groups of Groups 當作 Inventory file 的範例
playbook 則修改如下:
#vi set_python_3_Default_v1.yml
---
- name: Ansible Set 'python 3'
hosts: "{{ VMhost }}"
tasks:
- name: set '1'
command: update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
register: result
- name: set '2'
command: update-alternatives --install /usr/bin/python python /usr/bin/python3.7 2
register: result
- name: print stdout
debug:
msg: "{{ result.stdout }}"
- name: Ansible Update Cache and install python3-pip
apt:
name:
- python3-pip
- name: set '3'
file:
src: "/usr/bin/pip3"
dest: "/usr/bin/pip"
state: link
於是當我執行 playbook 時,我可以透過 -e 改變 VMhost 的值,以此來彈性的決定本次要套用的 hosts 爲何。
例如:
ansible-playbook -i hosts.yml set_python_3_Default_v1.yml -e VMhost=DebianGroup
如此所有主機都會被套用 playbook。
ansible-playbook -i hosts.yml set_python_3_Default_v1.yml -e VMhost=lemp
如此只有 lemp 這個 Group 會被套用 playbook。
ansible-playbook -i hosts.yml set_python_3_Default_v1.yml -e VMhost=server01
如此只有 server01 這台主機會被套用 playbook。
ssh-copy-id root@192.168.96.160
ssh 'root@192.168.96.160'
# 如果直接使用 shell 模式
ansible -i hosts.yml -m shell -a "reboot" server01
ansible -i hosts.yml -m shell -a "apt install -y sudo" server01
#Step 04 –
#Step 05 –
#Step 06 –
#Step 07 –
#Step 08 –
#Step 09 –
#Step 10 –
沒有留言:
張貼留言