Ansible 是一種 agentless(基於 ssh),可實現批量配置、命令執行和控制,基於 Python 實現的自動化運維工具。
其特性有:javascript
一、安裝ansible(依賴於epel源)php
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum install ansible -y
執行ansible報錯html
ERROR! Unexpected Exception, this is probably a bug: (cryptography 0.8.2 (/usr/lib64/python2.7/site-packages), Requirement.parse('cryptography>=1.1'))
解決方法
rpm -qa |grep python-crypto 把列出的 rpm 包所有刪除
rpm -e python-cryptography --nodeps 強制刪除
二、定義HOSTS文件,配置主機,配置登錄方式java
cd /etc/ansible/
cp hosts{,.bak}
2-1使用配置文件認證方式(不推薦這種方式)node
vim hosts
[webs]
192.168.2.100 ansible_ssh_user=root ansible_ssh_pass=123456
192.168.2.101 ansible_ssh_user=root ansible_ssh_pass=123456 ansible_ssh_port=22
[dbs]
192.168.2.102 ansible_ssh_user=root ansible_ssh_pass=123456 ansible_ssh_port=22
若報這個錯,需用SSH鏈接一次便可python
[root@ansibles ansible]# ansible
192.168.2.101 | FAILED! => {
"msg": "Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this. Please add this host's fingerprint to your known_hosts file to manage this host."
}
[root@ansibles ansible]# ssh 192.168.2.101
The authenticity of host '192.168.2.101 (192.168.2.101)' can't be established.
ECDSA key fingerprint is 58:da:34:71:b4:08:0c:95:c3:ff:72:15:c4:05:10:24.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.2.101' (ECDSA) to the list of known hosts.
root@192.168.2.101's password:
Last login: Tue Oct 23 13:26:43 2018 from 192.168.2.254
2-2基於密鑰方式登錄mysql
ssh-keygen -t rsa
ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.2.100
ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.2.101
ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.2.102
配置完測下linux
ansible all -m ping
192.168.2.101 | SUCCESS => {
"changed": false,
"ping": "pong"
}
192.168.2.100 | SUCCESS => {
"changed": false,
"ping": "pong"
}
192.168.2.102 | SUCCESS => {
"changed": false,
"ping": "pong"
}
三、如何查看模塊幫助
ansible-doc -l 列出哪些模塊
ansible-doc -s MODULE_NAME 查看模塊參數web
語法
ansible <host-pattern> [options]
-f forks:啓動的併發線程數
-m module_name:要使用的模塊
-a args:模塊物有的參數sql
經常使用模塊
ansible all -m cron -a 'minute="30" hour="3" job="usr/sbin/ntpdate ntp.aliyun.com" name="ntp date time"'
查看下結果
[root@ansibles ~]# ansible all -a 'crontab -l'
刪除此計劃任務
ansible all -m cron -a 'name="ntp date time" state=absent'
建立用戶
ansible dbs -m user -a 'name="user1"'
192.168.2.102 | SUCCESS => {
"changed": true,
"comment": "",
"createhome": true,
"group": 1000,
"home": "/home/user1",
"name": "user1",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 1000
}
刪除用戶
ansible dbs -m user -a 'name="user1" state=absent'
ansible dbs -m group -a 'name=mysql gid=306 system=yes'
ansible dbs -m user -a 'name=mysql uid=306 system=yes group=mysql'
驗證下
ansible dbs -a 'grep mysql /etc/passwd'
複製文件
ansible dbs -m copy -a 'src=/etc/fstab dest=/tmp/fstab.ans owner=root mode=600'
ansible dbs -a 'ls /tmp/fstab.ans'
content=取代src=,代表用此處的信息生成目標文件
使用信息輸出目標文件
ansible dbs -m copy -a 'content="ansbile test" dest=/tmp/test.ans'
ansible dbs -a 'cat /tmp/test.ans'
更換文件權限
ansible dbs -m file -a 'owner=mysql group=mysql mode=600 path=/tmp/test.ans'
建立連接文件
ansible dbs -m file -a 'path=/tmp/fstab.link src=/tmp/fstab.ans state=link'
測試遠程主機的是否在線
ansible all -m ping
ansible dbs -m service -a 'enabled=true name=mysqld state=started'
雖然執行成功,但密碼沒有設置成功,可能當作本地的命令符號
注意:尤爲是用到管道等功能的複雜命令,使用shell模塊
ansible all -a 'echo password|passwd --stdin user1'
若使用到管理符號請使用shell模塊 ansible all -m shell -a 'echo password|passwd --stdin user1'
將本地腳本複製到遠程主機並運行
注意:要使用相對路徑指定腳本
ansible all -m script -a '/root/test.sh'
安裝程序包
ansible dbs -m yum -a 'name=vsftpd'
查看遠程主機的相關 facts 變量信息
ansible all -m setup
inventory modules ad hoc commands playbooks tasks:任務 variables:變量 templates:模板 handlers:處理器 roles:角色
示例1 安裝httpd包,配置文件,啓動服務
vim webs.yml
- hosts: webs
remote_user: root
tasks:
- name: install httpd package
yum: name=httpd
- name: install httpd config
copy: src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf
- name: start httpd service
service: enabled=true name=httpd state=started
示例2 若更改配置文件,觸發handlers將重啓服務
vim webs.yml
- hosts: webs
remote_user: root
tasks:
- name: install httpd package
yum: name=httpd
- name: install httpd config
copy: src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf
notify:
- restart httpd
- name: start httpd service
service: enabled=true name=httpd state=started
handlers:
- name: restart httpd
service: name=httpd state=restarted
示例3 引用變量
- hosts: webs
remote_user: root
vars:
- package: httpd
- service: httpd
tasks:
- name: install httpd package
yum: name={{ package }}
- name: install httpd config
copy: src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf
notify:
- restart httpd
- name: start httpd service
service: enabled=true name={{ service }} state=started
handlers:
- name: restart httpd
service: name={{ package }} state=restarted
輸出facts中的某個變量並輸入到文件
- hosts: dbs
remote_user: root
tasks:
- name: copy file
copy: content="{{ ansible_all_ipv4_addresses }}" dest=/tmp/vars.ans
when 條件測試
知足條件ansible_fqdn == "db1",將執行tasks
vim when.yml
- hosts: all
remote_user: root
vars:
- username: user1
tasks:
- name: crate {{ username }} user
user: name={{ username }}
when: ansible_fqdn == "db1"
迭代:重複同類task時使用
調用:item
定義循環列表:with_items
- httpd
- php
- mysql-server
注意:with_items中的列表值也能夠是字典,但引用時要使用tiem.KEY
- {name: httpd, conf: configfile/httpd.conf}
- {name: php, conf: configfile/php.ini}
- {name: mysql-server, conf: configfile/my.cnf}
http_port 變量定義在/etc/ansible/hosts文件裏
[root@ansibles ~]# grep port /etc/ansible/hosts
192.168.2.100 http_port=80
192.168.2.101 http_port=8080
[root@ansibles ~]# vim webs.yml
- hosts: webs
remote_user: root
vars:
- package: httpd
- service: httpd
tasks:
- name: install httpd package
yum: name={{ package }}
- name: install httpd config
template: src=/root/httpd.conf.t1 dest=/etc/httpd/conf/httpd.conf
notify:
- restart httpd
- name: start httpd service
service: enabled=true name={{ service }} state=started
handlers:
- name: restart httpd
service: name={{ package }} state=restarted
tags:在playbook能夠爲某個或某些任務定義一個標籤,在執行此playbook時,經過爲ansible-playbook命令使用--tags選項能實現僅運行指定的tasks而非全部的:
template: src=/root/httpd.conf.t1 dest=/etc/httpd/conf/httpd.conf
tags:
- conf
特殊tags: always
--tags="conf" --tars="restart"
- hosts: webs remote_user: root
vars:
- package: httpd - service: httpd tasks:
- name: install httpd package yum: name={{ package }}
tags:
- always - name: install httpd config template: src=/root/httpd.conf.t1 dest=/etc/httpd/conf/httpd.conf
tags:
- conf notify:
- restart httpd - name: start httpd service service: enabled=true name={{ service }} state=started
tags:
- restart handlers:
- name: restart httpd service: name={{ package }} state=restarted
目錄名同角色名
目錄結構有固定格式
建立目錄
mkdir -pv ansible_playbooks/roles/{webs,dbs}/{tasks,files,templates,meta,handlers,vars}
查看建立的目錄結構
[root@ansibles ~]# tree ansible_playbooks/
ansible_playbooks/
├── roles
│ ├── dbs
│ │ ├── files
│ │ ├── handlers
│ │ ├── meta
│ │ ├── tasks
│ │ ├── templates
│ │ └── vars
│ └── webs
│ ├── files
│ │ └── httpd.conf
│ ├── handlers
│ │ └── main.yml
│ ├── meta
│ ├── tasks
│ │ └── main.yml
│ ├── templates
│ │ └── httpd.conf.j2
│ └── vars
│ └── main.yml
└── site.yml
下面是相關的配置文件
vim /root/ansible_playbooks/roles/webs/handlers/main.yml - name: restart httpd service: name=httpd state=restarted
vim /root/ansible_playbooks/roles/webs/tasks/main.yml
- name: install httpd package
yum: name=httpd
- name: install conf file
template: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
tags:
- conf
notify:
- restart httpd
- name: start httpd service
service: name=httpd state=started enabled=true
grep "{{" /root/ansible_playbooks/roles/webs/templates/httpd.conf.j2
Listen {{ http_port }}
ServerName {{ ansible_fqdn }}
vim /root/ansible_playbooks/roles/webs/vars/main.yml
http_port: "{{ 8888 }}"
驗證下
[root@ansibles ~]#ansible-playbook ansible_playbooks/site.yml
[root@ansibles ~]# ansible webs -a 'ss -tnl'|grep 8888
LISTEN 0 128 :::8888 :::*
LISTEN 0 128 :::8888 :::*