標籤:角色 編寫 OLE group hosts 指令碼批量 指令碼 handler 一個
[[email protected]:/etc]# tree /etc/ansible//etc/ansible/├── ansible.cfg├── hosts├── python.yml└── roles └── python_install ├── files │ └── Python-3.6.6.tgz ├── tasks │ ├── copy.yml │ ├── install.yml │ └── main.yml └── templates └── python_install.sh說明: files:存放需要同步到異地伺服器的源碼檔案及設定檔; handlers:當資源發生變化時需要進行的操作,若沒有此目錄可以不建或為空白; meta:角色定義可留空; tasks:python安裝過程成需要進行的執行的任務; templates:用於執行python安裝的模板檔案,一般為指令碼; vars:本次安裝定義的變數,若無必要可以不建python3.6.6源碼存放目錄:python_install/files/Python-3.6.6.tgz具體操作1.建立python角色檔案,用於調用python_install[[email protected]:/etc/ansible]# cat /etc/ansible/python.yml ---- hosts: all remote_user: root roles: - python_install2.建立任務檔案[[email protected]:/etc/ansible/roles]# cat python_install/tasks/copy.yml - name: copy python_tgz to client copy: src=/etc/ansible/roles/python_install/files/Python-3.6.6.tgz dest=/usr/local/src/- name: copy install_python_script to client copy: src=/etc/ansible/roles/python_install/templates/python_install.sh dest=/tmp/python_install.sh owner=root group=root mode=755[[email protected]:/etc/ansible/roles]# cat python_install/tasks/install.yml - name: install python shell: /bin/bash /tmp/python_install.sh[[email protected]:/etc/ansible/roles]# cat python_install/tasks/main.yml - include: copy.yml- include: install.yml注意: a.copy如果複製目錄,需要加上遞迴參數,recurse; b.copy如果複製目錄,沒有目錄將會在目標伺服器上建立; c.copy如果複製檔案到目標伺服器的某一個目錄下,需要在dest參數上加上/usr/local/src/,而不是/usr/local/src,否則ansible將會把檔案複製為src,而不是放在src目錄下。 4.編寫模板指令碼[[email protected]:/etc/ansible/roles]# cat python_install/templates/python_install.sh #!/bin/bash# install python3.6.6# yum toolsyum -y groupinstall "Development tools"yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-develcd /usr/local/srcmkdir /usr/local/python3tar -zxf Python-3.6.6.tgzcd Python-3.6.6./configure --prefix=/usr/local/python3make && make installln -s /usr/local/python3/bin/python3 /usr/bin/python3ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3# end安裝指令碼功能: 1)安裝yum依賴包2)建立目錄,解壓檔案並編譯安裝到目標目錄3)產生軟串連執行playbook測試[[email protected]:/etc/ansible]# ansible-playbook -C python.yml 執行[[email protected]:/etc/ansible]# ansible-playbook python.yml
ansible配合shell指令碼批量編譯安裝python3.6.6