無服務、無agent、采用ssh管理遠端主機、多線程
1、配置檔案/etc/ansible/ansible.cfg
2、管理方式
(1)ad-hoc 臨時指令
(2)playbook劇本
遠端管理
1、建立一個目錄
[root@room8pc16 nsd1709]# mkdir ansi
[root@room8pc16 nsd1709]# cd ansi
2、建立配置檔案
[root@room8pc16 ansi]# vim ansible.cfg
[defaults]
inventory = inventory # 定義被管理主機到哪個檔案中查找
remote_user = root # ssh到遠端主機的使用者
3、建立主機清單
[root@room8pc16 ansi]# vim inventory
[dbservers] # 定義主機組名
192.168.4.1 # 定義組成員主機
[webservers]
192.168.4.2
192.168.4.3
4、列出主機指令,雖然all沒有定義,但是它是保留字,表示所有主機
[root@room8pc16 ansi]# ansible all --list-hosts
[root@room8pc16 ansi]# ansible dbservers --list-hosts
[root@room8pc16 ansi]# ansible webservers --list-hosts
5、測試到遠端主機的通信
[root@room8pc16 ansi]# ansible all -m ping -k
6、在所有的主機上執行任意指令
[root@room8pc16 ansi]# ansible all -a 'touch /opt/abc.txt' -k
以下指令是遠端開機指令,與ansible無關
[root@room8pc16 ansi]# ether-wake -i enp2s0 xx:xx:xx:xx:xx:xx
name: configure vsftpd
hosts: all
tasks:
name: install vsftpd
yum:
name: vsftpd
state: latest
name: start vsftpd
service:
state: started
enabled: true
9、文法檢查
[root@room8pc16 ansi]# ansible-playbook --syntax-check a.yml
10、執行playbook
[root@room8pc16 ansi]# ansible-playbook a.yml -k
11、檢視ansible子產品清單
[root@room8pc16 ansi]# ansible-doc -l
12、檢視yum子產品使用方法
[root@room8pc16 ansi]# ansible-doc yum
13、不允許mysql伺服器上出現apache
[root@room8pc16 ansi]# vim a.yml 追加以下内容
name: remove httpd
hosts: dbservers
name: configure file
name: configure hosts file
lineinfile:
path: /etc/hosts
line: "192.168.4.254 host.tedu.cn host"
name: configure selinux file
path: /etc/selinux/config
regexp: '^SELINUX='
line: 'SELINUX=permissive'
[root@room8pc16 ansi]# ansible-playbook b.yml -k
name: configure services
name: install services
name: "{{ item }}"
with_items:
httpd
php
php-mysql
mod_ssl
mariadb-server
16、ansible中文站點 http://www.ansible.com.cn
17、常用子產品:yum/service/lineinfile/copy/file/stat/debug/firewalld/command/shell