卸載服務
ansible all -m shell -a ‘yum -y remove nginx’
檢查用戶 組 uid gid
ansible all -m shell -a ‘getent passwd nginx’
ansible all -m shell -a ‘getent group nginx’
ansible all -m shell -a ‘getent passwd | grep 123’
刪除用戶
ansible all -m shell -a ‘userdel -r nginx’
ansible all -m user -a ‘name=apache state=absent’
驗證
ansible all -m shell -a ‘ss -ntlpe’ /* 端口 */
ansible all -m shell -a ‘ps aux | grep nginx /* 用戶 進程數 */
編譯安裝
一臺機器編譯好
安裝目錄 打包復制
ansible /* 任意文件夾 */
│
├── group_vars /* 變量 –> 所有 role */
│
│
├── File_role.yml /* 與 roles 平級 */
│ …
│
│
└── roles
│
│
├── Pro_1
│ │
│ ├── tasks /* 必須有 其他文件夾 可無 */
│ │ ├── main.yml
│ │ … /* File.yml */
│ │
│ ├── handlers
│ │ └── main.yml
│ ├── vars
│ │ └── main.yml
│ ├── templates
│ │ ├── File.j2
│ │ …
│ └── files
│ ├── File
│ …
│
│
└── Pro_2
├── …
mkdir -pv Pro_1/{tasks,templates,vars,handlers,files}
vim app_role.yml
– hosts: all
remote_user: root
roles:
– app
– Pro_2 /* 調用多個角色 */
– { role: httpd , tags: [‘web’,’httpd’] } /* 添加標簽 */
– { role: nginx , tags: [‘web’,’nginx’] , when: ansible_distribution_major_version == “7” } /* 條件判斷 */
ansible-playbook -C -t httpd app_role.yml /* 挑選 標簽 執行 */
vim tasks/main.yml
– include: group.yml
– include: user.yml
– include: yum.yml
– include: templ.yml
– include: start.yml
– include: copyfile.yml
– include: roles/Pro_2/tasks/File.yml /* 調用其他角色中任務 copy路徑問題…*/
vim group.yml
– name: create group
group: name=app system=yes gid=123
vim user.yml
– name: create user
user: name=app group=app system=yes shell=/sbin/nologin uid=123
vim yum.yml
– name: install package
yum: name=httpd
vim templ.yml
– name: copy conf
template: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf /* File.j2 即可 */
notify: restart service
vim start.yml
– name: start service
service: name=httpd state=started enabled=yes
vim copyfile.yml
– name: copy config
copy: src=vhosts.conf dest=/etc/httpd/conf.d/ owner=app /* 若被其他role調用 注意改 絕對路徑 */
vim handlers/main.yml
– name: restart service
service: name=httpd state=restarted
vim vars/main.yml
username: app
groupname: app
cp /etc/httpd/conf/httpd.conf templates/httpd.conf.j2
vim templates/httpd.conf.j2
Listen {{ ansible_processor_vcpus*10 }}
User {{ username }}
Group {{ groupname }}
本文來自投稿,不代表Linux運維部落立場,如若轉載,請注明出處:http://www.www58058.com/103709