1)Ansible:Ansible的核心程序python
2)Host Inventory:(默認路徑:/etc/ansible/hosts)記錄了每個由Ansible管理的主機信息,信息包括ssh端口,root賬號密碼,ip地址等等。能夠經過file來加載,能夠經過CMDB加載git
3)Playbooks:YAML格式文件,多個任務定義在一個文件中,使用時能夠統一調用,「劇本」用來定義那些主機須要調用那些模塊來完成的功能.web
4)Core Modules:Ansible執行任何管理任務都不是由Ansible本身完成,而是由核心模塊完成;Ansible管理主機以前,先調用core Modules中的模塊,而後指明管理Host Lnventory中的主機,就能夠完成管理主機。shell
5)Custom Modules:自定義模塊,完成Ansible核心模塊沒法完成的功能,此模塊支持任何語言編寫。vim
6)Connection Plugins:鏈接插件,Ansible和Host通訊使用併發
1)hoc:命令行ssh
2)playbooks:劇本/腳本this
3)roles:角色spa
1)安裝: 插件
yum install epel-release
yum install anisble
2)配置客戶端
(1)server:ssh-keygen
scp id_rsa.pub root@192.168.254.25:/root/.ssh/authorized_keys
(2)vim /etc/ansible/hosts
ansible_ssh_user=root ansible_ssh_port=22 ansible_ssh_pass=root
ansible -f 修改
ansible-doc -l #查看支持的模塊
ansible-doc -s MODEL_NAME #查看模塊用法
ansible命令應用基礎
ansible all -m ping #查看client端是否正常ping通
ansible webserver -m setup #查看客戶端信息
ansible webserver -m copy -a 'src=/root/git_test/code.txt dest=/root/test' #copy文件到cient端
ansible webserver -m user -a "name=test state=present" #建立test用戶
ansible webserver -m user -a "name=test state=absent" #刪除test用戶
ansible webserver -m yum -a ‘name=epel-relese state=latest‘ #yum安裝
ansible webserver -m service -a ‘name=httpd state=stopped enabled=no‘ #中止httpd服務
ansible webserver -m script -a ‘/tmp/test.sh‘ #運行腳本
ansible webserver -m command 'date' #查看時間
使用ansible鏈接主機時出現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.報錯則把/etc/ansible/ansible.cfg配置文件中host_key_checking = False行註釋打開
1)
若是用模塊形式通常有冪等性,若是用shell或者command沒有冪等性
playbooks至關因而shell腳本,能夠把要執行的任務寫到文件當中,一次執行,方便調用
tasks:一個task至關因而一個play
varibles: 變量,必定定義,多處調用
template:模板,能夠區分不一樣主機的特色
handlers:觸發器,依賴於前一個任務,前一個任務若是執行改變,那麼就會觸發handlers
2)定義playbook任務
- hosts: testhosts -與關鍵字之間必須有空格 remote_user: root 與hosts對齊 vars: 定義變量 - file: httpd.conf tasks: 定義任務 - name: copy httpd.conf 任務名 copy: src=/root/{{ file }} dest=/etc/httpd/conf/{{ file }} 調用copy模塊 - name: restart httpd 定義多個任務 service: name=httpd state=restarted
3)定義變量
在yaml文件當中傳入模板變量
{{變量名}}
第一種:
vars:
- file: httpd.conf
第二種:
vim /etc/ansible/hosts
[testhosts:vars]
file=httpd.conf
packages=tree
第三種
執行playbook文件時候給與變量 --extra-vars
ansible-playbook test.yaml --extra-vars "touch_file=test.txt"
4)註冊變量:
register註冊變量:把date命令輸出的結果賦予給date_output
- hosts: 192.168.254.10 remote_user: root tasks: - name: get date command: date register: date_output - name: echo date_output shell: "echo {{date_output.stdout}}>/tmp/a.txt"
5)when語句
when條件語句:能夠根據setup顯示出客戶端信息爲依據來判斷
- hosts: 192.168.254.12 remote_user: root tasks: - name: echo date_output shell: "touch /tmp/a.txt" when: ansible_distribution=='CentOS' and ansible_distribution_major_version=='8'
6)異常處理
ignore_errors:若是任務出錯,直接跳過,不會影響其餘任務
- hosts: 192.168.254.12 remote_user: root tasks: - name: add several user command: touch1 a.txt ignore_errors: yes
7)循環語句:
第一種:
{{ item }}:循環建立
- hosts: 192.168.254.12 remote_user: root tasks: - name: add many users user: name={{ item }} state=present with_items: - user1 - user2 - user3 - user4
第二種:
- hosts: 192.168.254.12 remote_user: root tasks: - name: add several user user: name={{item.name}} state=present groups={{item.groups}} with_items: - { name: 'testuser1', groups: 'wheel'} - { name: 'testuser2', groups: 'root'}
8)觸發器:
handlers:若是執行的任務被改變那麼會觸發handlers的任務
- hosts: testhosts remote_user: root tasks: - name: copy httpd.conf copy: src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf notify: - restarted httpd service handlers: - name: restarted httpd service service: name=httpd state=restarted
9)模板拷貝:
template,用來區分不一樣客戶端上的特性
- hosts: testhosts remote_user: root tasks: - name: copy httpd.conf template: src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf 將copy改成template notify: - restarted httpd service handlers: - name: restarted httpd service service: name=httpd state=restarted
將要修改的文件內的區域改成變量,如把Listen 80 改成Listen {{ port }}
在組文件中添加變量的值
[testhosts]
192.168.52.234·······port=1111
192.168.52.235·······port=2222
8.roles:角色
1)建立目錄
[root@localhost ~]# tree playbooks/ playbooks/ 根據須要命名 ├── roles │ ├── dbservers 根據須要命名 │ │ ├── files 存放要複製的文件 │ │ │ └── httpd.conf │ │ ├── handlers 存放觸發器任務文件 │ │ │ └── main.yaml │ │ ├── tasks 存聽任務文件 │ │ │ └── main.yaml │ │ ├── templates 存放模板文件 │ │ │ └── httpd.conf │ │ └── vars 存放變量定義文件 │ │ └── main.yaml │ └── webservers │ ├── files │ ├── handlers │ ├── tasks │ ├── templates │ └── vars └── site.yaml 主調用文件
編輯主調用文件:
vim playbooks/site.yaml - hosts: webservers remote_user: root roles: - webservers - dbservers
2)按照playbooks語句進行編輯