1.Ansible Inventory定義主機和主機組,定義多個主機組方便維護html
例如:node
192.168.1.1 ansible_ssh_pass='1111111' #定義了主機和密碼python
192.168.1.2 ansible_ssh_pass='1111111'nginx
[docker] #定義了一個組叫dockerweb
192.168.1.10[1:3] #定義了docker組下主機從192.168.1.101到192.168.1.103docker
[docker:vars] # 定義docker組ssh登陸的密碼、端口或者帳號等等shell
ansible_ssh_pass='1111111'json
ansible_ssh_port=22 ruby
[ansible:children] #定義一個組叫ansible,這個組下面包含docker組bash
docker
測試:ansible 192.168.1.1 -m ping -o
192.168.1.1 | SUCCESS => {"changed": false, "ping": "pong"}
多個主機列表
1)修改ansible.cfg中host定義到目錄inventory = /etc/ansible/inventory/
測試:ansible docker --list-hosts和ansible ansible --list-hosts
hosts (2):
192.168.1.1
192.168.1.2
#ansible test1 -m ping -o
192.168.1.1 | SUCCESS => {"changed": false, "ping": "pong"}
192.168.1.2 | SUCCESS => {"changed": false, "ping": "pong"}
2.動態Inventory: ansible中主機列表和變量信息支持從外部拉取
須要本身編寫腳本獲取,不受語言限制
Inventory內置參數
名稱 例子 解釋
ansible_ssh_host 主機的名字 SSH目的主機名或IP
ansible_ssh_port 22 SSH目的端口
ansible_ssh_user root SSH登陸使用的用戶名
ansible_ssh_pass none SSH認證所使用的密碼
ansible_connection smart ansible使用何種鏈接模式鏈接到主機
ansible_ssh_private_key_file none SSH認證所使用的私鑰
ansible_shell_type sh 命令所使用的shell
ansible_python_interpreter
/usr/bin/python
主機上的python解釋器
ansible_sudo 定義host sudo 用戶 ansible_sudo=xxx
ansible_sudo_pass none host sudo密碼
ansible_sudo_exe ansible_sudo_exe=/usr/bin/sudo host sudo路徑
ansible_connection ansible_connection=local 定義hosts 連接方式
ansible_ssh_private_key_file ansible_ssh_private_key_file=/root/key 定義host祕鑰
ansible_shell_type ansible_shell_type=zsh 定義hosts shell類型
ansible_python_interpreter ansible_python_interpreter=/usr/bin/python2.7 定義hosts任務執行python路徑
遠程命令模塊:command、shell、scripts
Ansible Ad-Hoc組件
ansible命令都是併發執行
copy模塊批量下發文件:文件變化經過MD5來判斷
例如:$ansible 192.168.1.2 -m copy -a 'src=/home/test/dump.sh dest=/home/test/ owner=test group=test mode=600 backup=yes' -o
192.168.1.2 | SUCCESS => {"changed": true, "checksum": "61c25bb854593abf2b6d5bd47b187b207a141c72", "dest": "/home/test/dump.sh", "gid": 500, "group": "test", "md5sum": "2ec1900bf05cadeb74a28f6d6597f4aa", "mode": "0600", "owner": "test", "size": 6979, "src": "/home/test/.ansible/tmp/ansible-tmp-1504495347.87-254032109614069/source", "state": "file", "uid": 500}
例如:ansible strike -m copy -a 'src=/home/tain/test dest=/usr/tain/test owner=cctain group=cctain mode=644' -o //指定拷貝到某個組
獲取遠程文件狀態信息:ansible 192.168.1.2 -m stat -a "path=/etc/sysctl.conf"
1)get_url 模塊:實如今遠程主機下載指定URL到本地
$ansible 192.168.1.2 -m get_url -a "url=http://www.baidu.com dest=/tmp/index.html mode=0440 force=yes"
2)定時任務模塊:
ansible 192.168.1.2 -m cron -a "name='crontab test' minute=0 hour=5,2 job='ls -alh > /dev/null'"
使用Ad-Hoc命令管理包和服務
$ansible 192.168.1.2 -m yum -a 'name=httpd state=latest' -f 5 -o //安裝httpd,卸載就改爲state=remove
$ansible 192.168.1.2 -m service -a 'name=nginx state=started' -f -o
192.168.1.2 | SUCCESS => {"changed": false, "name": "nginx", "state": "started"}
驗證服務是否有安裝:
$ansible 192.168.1.2 -m shell -a "rpm -qa nginx" -f 5 -o
192.168.1.2 | SUCCESS | rc=0 | (stdout) nginx-1.12.1-1.el6.x86_64
$ansible 192.168.1.2 -m shell -a "netstat -tpln| grep nginx" -f 5
192.168.1.2 | SUCCESS | rc=0 >>
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 9930/nginx
用戶管理
1)經過openssl生成密碼:由於ansible user的passwd參數須要接受加密後的值
$echo ansible | openssl passwd -1 -stdin
$1$fcpQxeGv$VErvNXelrmLxprVIZC/DS1
使用user模塊批量新建用戶
ansible192.168.1.2 -m user -a 'name=test password="$1$fcpQxeGv$VErvNXelrmLxprVIZC/DS1"' -f 5 -o
經過ssh 192.168.1.2 -l test 驗證是否能夠登陸
ansible playbook 進行配置管理的組件,Ad-Hoc 沒法支撐複雜環境的配置管理工做
1.ansible facts採集主機靜態信息的一個組件
例如:ansible 192.168.1.2 -m setup 收集該服務器各類設備信息
ansible test -m setup -a "filter=ansible_all_ipv4_addresses" //setup中單獨指定一個信息
ansible 192.168.1.2 -m shell -a 'rpm -qa ruby-json facter' //判斷被控制機器上是否安裝了這2個包
ansible 192.168.1.2 -m facter
2.ohai擴展facts信息
ohai是chef配置管理工具中檢測節點屬性的工具
ansible 192.168.1.2 -m shell -a "yum list| grep ohai"
ansible 192.168.1.2 -m ohai
Ansible role:採用role方式管理playbook,規範目錄結構
[root@node1 playbook]# cat web.yml - hosts: test \\主機組,在/etc/ansible/hosts定義 remote_user: root \\遠端執行任務的用戶 tasks: \\任務 - name: install httpd \\任務描述 command: yum -y install httpd \\調用ansible的command模塊安裝httpd - name: provide httpd.conf \\任務描述 copy: src="/root/httpd.conf" dest="/etc/httpd/conf/httpd.conf" \\調用ansible的copy模塊,httpd安裝完成後將事先準備好的httpd.conf文件複製到/etc/httpd/conf目錄下 tags: conf \\給此任務打標記,可單獨執行標記的任務,使用 ansible-playbook -C 命令執行 notify: \\文件內容變動通知 - server restart \\通知到指定的任務 - name: server start \\任務描述 service: name=httpd state=started enabled=true \\調用ansible的service模塊的屬性定義安裝完成httpd之後httpd服務的管理 handlers: \\定義接受關注的資源變化後執行的動做 - name: server restart \\任務描述 service: name=httpd state=restarted \\當關注的資源發生變化後調用service模塊,採起的響應的動做
roles/ \\ansible全部的信息都放到此目錄下面對應的目錄中 └── nginx \\角色名稱 ├── default \\爲當前角色設定默認變量時使用此目錄,應當包含一個main.yml文件; ├── files \\存放有copy或script等模塊調用的文件 ├── handlers \\此目錄總應當包含一個main.yml文件,用於定義各角色用到的各handler ├── meta \\應當包含一個main.yml,用於定義角色的特殊設定及其依賴關係;1.3及之後版本支持 ├── tasks \\至少包含一個名爲main.yml的文件,定義了此角色的任務列表,可以使用include指令 ├── templates \\template模塊會自動在此目錄中尋找Jinja2模板文件 └── vars \\應當包含一個main.yml文件,用於定義此角色用到的變量 mkdir -pv roles/nginx/{tasks,files,templates,handlers,vars,meta,default}
Ansible Galaxy :官方分析role功能的平臺
網址:https://galaxy.ansible.com/list#/roles
使用ansible-galaxy install默認安裝到/etc/ansible/roles/