ansible 自動化運維

 

Ansible 自動化運維php


1、ansible安裝

安裝epel
#yum list all *ansible*
#yum install *ansible*
#yum info ansible
#rpm -ql ansiblehtml

pip3 install ansiblenode

二,配置文件

配置文件: /etc/ansible/ansible.cfg
Invertory: /etc/ansible/hosts mysql


cd /etc/ansible/
cp hosts{,.bak}
#vim hosts
192.168.1.100nginx

Ansible中文教程 網址: https://www.ansible.com.cn/ web

3、登陸配置

1,密碼登錄:sql

(1),安裝sshpassshell

(2) /etc/ansible/hosts文件中添加用戶密碼,認證ssh鏈接;apache

    [testhost]vim

192.168.1.112 ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass=123456

(3) ssh第一次鏈接的時候通常會提示輸入yes 進行確認爲將key字符串加入到 ~/.ssh/known_hosts 文件中。而本機的~/.ssh/known_hosts文件中並有fingerprint key串
解決方法:在ansible.cfg文件中更改下面的參數:
#host_key_checking = False 將#號去掉便可;

 2,祕鑰登錄:

生成祕鑰:ssh-keygen
新鑰匙就在您的用戶可用~/.ssh目錄。 公鑰(一個能夠共享)被稱爲id_rsa.pub 。 私鑰(您保持安全的)被稱爲id_rsa
測試#ssh root@192.168.1.100 'date'
cp祕鑰#ssh-copy-id -i /root/.id_rsa.pub root@192.168.1.100 ;再次測試

 

4、介紹ansible

1,查看模塊幫助

#man ansible-doc
#ansile-doc -l ansible 查看支持的全部模塊
#ansible-doc -s MODULE_NAME

ansible命令應用基礎:
語法: ansible <host-pattern> [-m module_name] [-a args] [options]
-f forks:啓動的併發線程數;
-m module_name: 要使用的模塊
-a args:模塊特有的參數;


2,常見模塊:

ansible 192.168.1.100 -m command -a 'date'
ansible openstack -m command -a 'date'
ansible all -m command -a 'tail -2 /etc/passwd'

command :命令模塊, 默認模塊,用於遠程執行命令;
#ansible all -a 'date'

cron: #ansible-doc -s cron
state: 狀態
present:安裝
absent:移除
*/10 * * * * /bin/echo hello
#ansible websrvs -m cron -a 'minute="*/10" job="/bin/echo hello" name="test"'
#ansible websrvs -a 'crontab -l'
#ansible websrvs -m cron -a 'minute="*/10" job="/bin/echo hello" name="test" state=absent'      #移除cron命令;

 

user :#ansible-doc -s user
#ansible openstack -m user -a 'name="user1"' #建立user1用戶
#tail /etc/passwd
#tail /etc/group
name= ;用於指明user用戶名字

group: #ansible-doc -s group
#ansible openstack -m group -a 'name=mysql gid=306 system=yes'
#ansible openstack -m user -a 'name=mysql uid=306 system=yes group=mysql'

 

copy: #ansible-doc -s copy 複製文件
src=: 定義本地源文件路徑;
dest=:定義遠程目標文件路徑;
#ansible all -m copy -a 'src=/etc/fstab dest=/tmp/fstab.ansible owner=root mode=640'
還能夠用content
#ansible all -m copy -a 'content="Hello Ansible\nHi TOM" dest=/tmp/test.ansible'

 

file : #ansible-doc -s file 設定文件屬性
path: 指定文件路徑,可使用name或dest來替代
#ansible openstack -m file -a 'owner=mysql group=mysql mode=644 path=/tmp/fstab.ansible'
建立文件符號連接:
src=:指明源文件
path=:指明返回連接文件路徑;
#ansible openstack -m file -a 'path=/tmp/fstab.link src=/tmp/fstab.ansible state=link'

 

ping :ansible-doc -s ping 測試指定主機是否能鏈接
#ansible all -m ping

service : ansible-doc -s service 控制服務的啓動狀態
enabled=:是否開機自動啓動,取值true或false
name=: 服務名
state=: 狀態,取值有started, stoped, restarted;
#ss -tnl 只顯示監聽套接字; 和netstat相似
#ansible all -a 'service httpd status'
#ansible openstack -a 'chkconfig --list httpd'
#ansible openstack -m service -a 'enabled=true name=httpd state=started'

shell : ansible-doc -s shell 與command模塊相似
尤爲是用到管道等功能命令時使用shell
#ansible all -m user -a 'name=user1'
#ansible all -m shell -a 'echo mageedu | passwd --stdin user1'

script :#ansible-doc -s script
將本地腳本複製到遠程主機運行(要使用相對路徑指定腳本)
vim test.sh #echo "hello ansible script" > /tmp/script.ansible
#ansible all -m script -a 'test.sh'

yum:
安裝軟件包
name=:指明要安裝的程序包,能夠帶上版本號;
state=: present,latest表示安裝, absent 表示卸載
#ansible openstack -m yum -a "name=zsh"
#ansible openstack -m yum -a "name=zsh state=absent" 卸載軟件包zsh

setup:
收集遠程主機的facts
每一個被管理節點在接收並運行管理命令以前,會將資金主機相關信息,如操做系統版本,ip地址等報告給遠程的ansible主機;

5、YAML介紹


list 列表的全部元素均使用‘-’打頭:
#A listof tasty fruits
- Apple
- Orange
- Mango

dictionary 字典經過key與value進行標識,
例如:name:tom
job:doctor
也能夠將key:value放置於{} 中進行表示;
如{name: tom, job: doctor}

Ansible中使用YAML基礎元素
變量
Inventory
條件測試
迭代

playbook的組成結構:
Inventory 主機信息清單
Modules 調用的模塊
Ad Hoc Commands 使用的命令
Playbooks:
Tasks: 任務,即調用模塊完成的某操做;
Variables: 變量
Templates:模板
Handler:處理器,由某件事觸發執行的操做
Roles : 角色
基本結構:
- host : websrvs
remote_user:
tasks:
- task1
module_name: module_args
- task2


- host : openstack

ansible-playbook: #man ansible-playbook
#ansible-playbook <filename.yml>


1,例1


vim nginx.yml
- hosts: websrvs
remote_user: root
tasks:
- name: create nginx group
group: name=nginx system=yes gid=208
- name: create nginx user
user: name=nginx uid=208 group=nginx system=yes

- hosts: dbsrvs
remote_user: root
tasks:
- name: copy file to dbsrvs
copy: src=/etc/inittab dest=/tmp/inittab.ansible

handlers:
#用於當關注的資源發生變化時採起必定的操做;

 

2,例2


vim apache.yml
- hosts: websrvs
remote_user: root
tasks:
- name: install httpd package
yum: name=httpd state=latest
- name: install configuration file for httpd
copy: src=/root/conf/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
#ansible-playbook apache.yml

rpm -q httpd
grep "Listen" /etc/httpd/conf/httpd.conf
service httpd status
ss -tnlp

變量vars

 

3,例3


vim apache.yml
- hosts: websrvs
remote_user: root
vars:
- package: httpd
- service: httpd
tasks:
- name: install httpd package
yum: name={{ package }} state=latest
- name: install configuration file for httpd
copy: src=/root/conf/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=httpd state=restarted

 

4,例4


vim test.yml
- hosts: websrvs
remote_user: root
tasks:
- name: copy file
copy: content="{{ ansible_all_ipv4_addresses }}" dest=/tmp/vars.ansible
在hosts中使用變量
vim hosts
192.168.1.112 testvar="1.112"
192.168.1.113 ansible_ssh_user=root ansible_ssh_pass=openstack

vim test.yml
- hosts: websrvs
remote_user: root
tasks:
- name: copy file
copy: content="{{ ansible_all_ipv4_addresses }} , {{testvar}} , {{ ansible_ssh_user}} , {{ ansible_ssh_pass }}" dest=/tmp/vars.ansible

條件測試when:

 

5,例5


vim cond.yml
- hosts: all
remote_user: root
vars:
- username: user10
tasks:
- name: create {{ username }} user
user: name={{ username }}
when: ansible_fqdn == "node2.xxx.com"

迭代:重複執行同類task時使用 的任務
調用item
定義循環列表: with_items
- apache
- php
- mysql-server
注: with_items中的列表值也能夠是字典,但引用時要使用item.KEY
- {name: apache, conf: conffiles/httpd.conf }
- {name: php, conf:conffiles/php.ini}
- {name: mysql-server, conf: conffiles/my.cnf}

模板
vim template/httpd.conf
Listen {{ http_port }}
ServerName {{ ansible_fqdn }}
vim /etc/ansible/hosts
192.168.1.112 http_port=80
vim apache.yml
- name: install configuration file for httpd
template: src=/root/templates/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf

tags: 只運行其中某部分的命令:
在playbook 能夠爲某個或某些任務定義一個「標籤」,在執行playbook時,經過爲 ansible-playbook 命令使用 --tags 選項能實現僅運行指定的tasks而非全部
ansible-playbook apache.yml --tags="conf"
- name: install configuration file for httpd
template: src=/root/templates/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
tags:
- conf

 

6,例6


vim apache.yml
- hosts: websrvs
remote_user: root
vars:
- package: httpd
- service: httpd
tasks:
- name: install httpd package
yum: name={{ package }} state=latest
- name: install configuration file for httpd
template: src=/root/templates/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
tags:
- conf
notify:
- restart httpd
- name: start httpd service
service: enabled=true name={{ service }} state=started
handlers:
- name: restart httpd
service: name=httpd state=restarted

特殊tags
tags:
- always 老是不運行

Roles :
(1) 目錄名同角色名
(2) 目錄結構有固定格式;
files: 靜態文件;
templates: jinjia2 模板文件;
tasks: 至少有main.yml 文件,定義各tasks;
handlers: 至少有一個main.yml 文件, 定義各 handlers ;
meta: 定義依賴關係等信息;
(3) roles同級別中有site.yml文件;
site.yml 中定義playbook,額外也能夠有其餘的yml文件;
建立目錄
#mkdir -pv ansible_playbooks/roles/{websrvs,dbsrvs}/{tasks,files,templates,meta,handlers,vars}
#tree ansible_playbooks
vim site.yml
- hosts: 192.168.1.100
remote_user: root
roles:
- websrvs

- hosts: 192.168.1.101
remote_user: root
roles:
- dbsrvs

- hosts: 192.168.1.100
remote_user: root
roles:
- websrvs
- dbsrvs
#cd roles/dbsrvs/
#vim tasks/main.yml
- name: install mysql-server package
yum: name=mysql-server state=latest
- name: install configuration file
copy: src=my.cnf dest=/etc/my.cnf
tags:
- conf
notify:
- restart mysqld
- name: start mysqld service
service: name=mysqld enabled=true state=started
#vim handlers/main.yml
- name: restart mysqld
service: name=mysqld state=restarted

 


相關博文:

 https://www.cnblogs.com/keerya/p/7987886.html

相關文章
相關標籤/搜索