ansible-亂

工做機制:ssh 無客戶端html

工做方式:
1,CMDB
2,公有云私有云API
3,使用ad-hoc
4,ansible-playbook
ansible 執行命令,底層調用傳輸鏈接模塊,將命令或文件傳輸至遠程服務器的/tmp目錄,遠程執行,操做完後刪除,返回結果。

python

配置文件linux

/etc/ansible 功能;inventory 主機信息配置 工具功能
/usr/bin 系列命令默認存放位置
ansible 讀取命令的順序:
當前執行命令目錄----用戶家目錄下 .ansible.cfg ------/etc/ansible.cfg ,先找到,先使用nginx

ansible 配置項


2)[privilege_escalation] sudo 用戶提權
3)
4)【ssh_connection】
5)【accelerate】
6)【selinux】
7)【colours】web

公私鑰
ssh-keygen -N " " -b 4096 -t rsa -C " " -f /root/.ssh/stanley.rsa
本機添加認證
ssh-copy-id -i /root/.ssh/stanley.rsa root@localhost正則表達式

ssh -i /root/.ssh/stanley.rsa root@local
命令格式shell

ansible [options] apache

執行後命令狀態
紅色: 過程異常,終止剩餘任務
綠色: 執行結束後目標沒有狀態變化
橘黃色:執行正常,目標狀態有變化
ansible-doc
-l l列出模塊
ping 顯示說明
ansibl-playbook .yml
ansibl-vault 加密配置文件
Inventory 是管理主機的配置文件,默認存放在 /etc/ansible/hosts
使用(默認只有一個inventory時不須要指定路徑
ansible -i /etc/ansible/hosts webs -m ping
inventory 在其餘路徑時可使用-i 指定位置
ansible all --listjson

Inventory 配置
定義主機及組centos

192.168.22.1
ntp.magedu.com:2222
nfs.magedu.com

#分組
[webserv]
web1.magedu.com
web[10:20].magedu.com 10-20 之間全部數字

主機變量 定義主機時定義變量
[webserv]
web1.magedu.com http_port=808 maxRequestsPerchild=801

組變量
 [groupeservers]
 web1.magedu.com 
 web2.magedu.com
 [groupeservers:vars]
 ntp_server=ntp.magedu.com     組中全部主機的ntp_serve值
 com
 nfs_server=nfs.magedu.com
 com 

   定義組嵌套變量及組變量

[apache]
httpd1.magedu.com
httpd2.magedu.com
[nginx]
ngx1.magedu.com
ngx2.magedu.com
[webservers:children]
apache
nginx
[webservers:vars]
ntp_server=ntp.magedu.com

多重變量

變量可在INventory 定義,也可在以外定義,單獨存儲在YAML配置文件中,以.yml .yaml .json 後綴
或無後綴,從如下位置檢索:
inventory 配置文件中 默認 /etc/ansible/hosts
Playbook 中vars 定義的域
Roles vars 目錄下文件
Roles 同級目錄 group_vars hosts_vars 目錄下文件

優先級:

DINGY

正則表達式
針對inventory 中主機列表使用
ansible -m -a
對webserver 匹配
ansible webservers -m service -a "name=httpd state=restart "
全量匹配
ansible all -m ping
ans- " " -M ping
ans- 192.168.1.
-m ping
邏輯或 or 匹配
ans- "web1:web2" -m ping
邏輯非 !
webservers:!phoenix (全部在webservers 組但不在phonenix 中的主機)
邏輯與
webservers:&phoenix 2組同時存在

多條件組合
webservers:dbservers:&staging:!phoenix webservers,dbservers全部主機在staging存在後且在phoenix 中不存在

模糊匹配

  • 0或 多個任意字符
    .magedu.com
    one
    .magedu.com
    域切割
    str = '123456'
    print str[0:1]
    例子:
    [webservers]
    web1
    web2
    web3
    webservers[0] # web1
    webservers[-1] web3
    webservers[0:1] #webservers[0] webservers[1]
    webservers[1:] webservers[1] webservers[2] web2 web3
    正則匹配 ~ 表示正則匹配開始
    ~(web|db).*.example.com

    ans- ~192.168.[0-9]{\2}.[0-9]{2,} -m ping

限定主機作變動
ansible app -m command -a "service ntpd status" --limit "192.168.36.3"

ansible 192.168.36.1 -m command -a "service ntpd status"

playbook 正式運行前使用  --check 或 -C 檢測playbook改變哪些內容
 ansible-playbook   --check 
   ansible-playbook       xxxx.yml    --limit    webserver    限定webserver組

inventory 內置參數
General for all connections:
ansible_host
The name of the host to connect to, if different from the alias you wish to give to it.
ansible_port
The ssh port number, if not 22
ansible_user
The default ssh user name to use.

Specific to the SSH connection:
    
    ansible_host
    The name of the host to connect to, if different from the alias you wish to give to it.
    ansible_port
    The ssh port number, if not 22
    ansible_user
    The default ssh user name to use.
    Specific to the SSH connection:

    ansible_ssh_pass
    The ssh password to use (never store this variable in plain text; always use a vault. See Variables and Vaults)
    ansible_ssh_private_key_file
    Private key file used by ssh. Useful if using multiple keys and you don’t want to use SSH agent.
    ansible_ssh_common_args
    
    playbook    語法檢測
    ansible-play    nginx.yml   --syntax-check
                                           --list-hosts

ansible test70 -m blockinfile -a 'path=/testdir/rc.local block="systemctl start mariadb\nsystemctl start httpd" ' 末尾插入2行
效果:

BEGIN ANSIBLE MANAGED BLOCK

systemctl start mariadb
systemctl start httpd

BEGIN ANSIBLE MANAGED BLOCK

自定義標記
ansible test70 -m blockinfile -a 'path=/testdir/rc.local block="systemctl start mariadb\nsystemctl start httpd" marker="#{mark} serivce to start" '
ansible test70 -m blockinfile -a 'path=/testdir/rc.local block="systemctl start mariadb" marker="#{mark} serivce to start" ' 更新上條語句塊的內容

ansible test70 -m blockinfile -a 'path=/testdir/rc.local block="" marker="#{mark} serivce to start" ' 刪除內容
ansible test70 -m blockinfile -a 'path=/testdir/rc.local  marker="#{mark} serivce to start" state=absent' 刪除內容
ansible test70 -m blockinfile -a 'path=/testdir/rc.local block="####blockinfile test####"  marker="#{mark} test reg" insertafter="^#!/bin/bash" ' 指定位置插入

lineinfile ,確保某一行存在指定文本中。

確保指定的一行文本 存在於文件中,若是指定的文本存在,不作操做,不然在文件末尾添加
ansible test70 -m lineinfile -a 'path=/testdir/test line="test text"'
根據正則表達式替換某一行,若是有許多行匹配,只有最後一個匹配的行纔會被替換,被替換爲指定的文本,如沒有匹配獲得任一行,line 內容添加到最後一行
ansible test70 -m lineinfile -a 'path=/testdir/test regexp="^line" line="test text" '

根據正則表達式替換某一行,若是有許多行匹配,只有最後一個匹配的行纔會被替換,被替換爲指定的文本,如沒有匹配獲得任一行,不操做
ansible test70 -m lineinfile -a 'path=/testdir/test regexp="^line" line="test text" backrefs=yes '
匹配到的行刪除
ansible test70 -m lineinfile -a 'path=/testdir/test line="lineinfile -" state=absent'
根據正則表達式匹配,並刪除
ansible test70 -m lineinfile -a 'path=/testdir/test regexp="^lineinfile" state=absent'
開啓後向引用匹配
ansible test70 -m lineinfile -a 'path=/testdir/test regexp="(H.{4}).*(H.{4})" line="\2" backrefs=yes'

變量的定義
變量名由字母數字,下劃線組成,變量名以字母開頭,內置的關鍵字不能做變量名

變量優先級

文件定義的變量優先級大於playbook hosts內的變量

定義;

- hosts: test70
  vars:
    testvar1: testfile #定義
  remote_user: root
  tasks:
  - name: task1
    file:
      path: /testdir/{{ testvar1 }} #引用
      state: touch
定義多個變量:
vars:
  testvar1: testfile
  testvar2: testfile2

yaml語法定義
vars:
  - testvar1: testfile
  - testvar2: testfile2

以屬性值方式定義

- hosts: test70
  remote_user: root
  vars:
    nginx:
      conf80: /etc/nginx/conf.d/80.conf
      conf8080: /etc/nginx/conf.d/8080.conf
  tasks:
  - name: task1
    file:
      path: "{{nginx.conf80}}" #或者 "{{nginx['conf8080']}}" 引用
      state: touch
  - name: task2
    file:
      path: "{{nginx.conf8080}}"
      state: touch

引用變量時使用了雙引號,變量在引用時處於開頭位置。
path: /testdir/{{ testvar1 }} 不處於開頭能夠不用雙引號
可以使用等號賦值,不須要引號
---
- hosts: test70
  remote_user: root
  vars:
    nginx:
      conf80: /etc/nginx/conf.d/80.conf
      conf8080: /etc/nginx/conf.d/8080.conf
  tasks:
  - name: task1
    file:
      path={{nginx.conf80}}
      state=touch
  - name: task2
    file:
      path={{nginx['conf8080']}}
      state=touch

在單獨文件中定義變量並應用,文件名爲nginx_vars.yml 在文件中定義變量不須要vars關鍵字,直接定義。

語法一示例:
  testvar1: testfile
  testvar2: testfile2
語法二示例:
  - testvar1: testfile
  - testvar2: testfile2
語法三示例:
nginx:
  conf80: /etc/nginx/conf.d/80.conf
  conf8080: /etc/nginx/conf.d/8080.conf
引用
---
- hosts: test70
  remote_user: root
  vars_files:
  - /testdir/ansible/nginx_vars.yml
  tasks:
  - name: task1
    file:
      path={{nginx.conf80}}
      state=touch
  - name: task2
    file:
      path={{nginx['conf8080']}}
      state=touch

能夠引用多個文件,
vars 和vars_files可同時使用
  vars:
  - conf90: /etc/nginx/conf.d/90.conf
  vars_files:
  - /testdir/ansible/nginx_vars.yml

ansible test70 -m setup 顯示收集的信息(不少信息

使用關鍵字查看想要的信息
ansible test70 -m setup -a 'filter=ansible_memory_mb'
通配符過濾
ansible test70 -m setup -a "filter=mb"
在遠程主機寫入自定義的信息

在遠程主機 /etc/ansible/facts.d/testinfo.fact 寫入信息
[root@test70 facts.d]# cat testinfo.fact
[testmsg]
msg1=This is the first custom test message
msg2=This is the second custom test message
json格式
{
   "testmsg":{
       "msg1":"This is the first custom test message",
       "msg2":"This is the second custom test message"
   }
}

調用
ansible test70 -m setup -a "filter=ansible_local"

默認會查找遠程主機 /etc/ansible/facts.d目錄,若是將local tacts信息放入其餘目錄,需指定
ansible test70 -m setup -a 'fact_path=/testdir'

debug 模塊

- hosts: test70
  remote_user: root
  tasks:
  - name: touch testfile
    file:
      path: /testdir/testfile
      state: touch
  - name: debug demo
    debug:
      msg: this is debug info,The test file has been touched
執行後控制檯會輸出信息
debug 模塊輸出自定義及變量信息
---
- hosts: test70
  remote_user: root
  vars:
    testvar: value of test variable
  tasks:
  - name: debug demo
    debug:
      var: testvar


- hosts: test70
  remote_user: root
  vars:
    testvar: testv
  tasks:
  - name: debug demo
    debug:
      msg: "value of testvar is : {{testvar}}" #msg引用了變量 變量引用前有: 須要使用「」

獲取主機內存信息

- hosts: test70
  remote_user: root
  tasks:
  - name: debug demo
    debug:
      msg: "Remote host memory information: {{ansible_memory_mb}}"

語法一示例:
debug:
     msg: "Remote host memory information : {{ansible_memory_mb.real}}"
語法二示例:
debug:
     msg: "Remote host memory information : {{ansible_memory_mb['real']}}"
上述兩種語法前文中已經進行過示例,此處再也不贅述。

註冊變量:
模塊運行時會返回值,默認不顯示(使用-vvvv顯示),能夠將其寫入變量後經過引用進行提取


- hosts: test70
  remote_user: root
  tasks:
  - name: test shell
    shell: "echo test > /var/testshellfile"
    register: testvar #註冊變量
  - name: shell module return values
    debug:
      var: testvar # 引用

返回的值
changed: [192.168.36.73] => {
"changed": true,
"cmd": "echo test1 > /data/testfile",
"delta": "0:00:00.002276",
"end": "2019-04-23 18:30:31.285899",
能夠經過指定的key獲取其value,方式;
語法一
  - name: shell module return values
    debug:
      msg: "{{testvar.cmd}}"
語法二
  - name: shell module return values
    debug:
      msg: "{{testvar['cmd']}}"
返回值含義文檔: https://docs.ansible.com/ansible/2.4/shell_module.html
提示用戶輸入信息:
---
- hosts: test70
  remote_user: root
  vars_prompt:
    - name: "your_name"
      prompt: "What is your name"
#默認輸入不顯示;但願顯示
private: no
    - name: "your_age"
      prompt: "How old are you"
  tasks:
   - name: output vars
     debug:
      msg: Your name is {{your_name}},You are {{your_age}} years old.

爲提示信息設置默認值

- hosts: test70
  remote_user: root
  vars_prompt:
    - name: "solution"
      prompt: "Choose the solution you want \n
      A: solutionA\n
      B: solutionB\n
      C: solutionC\n"
      private: no
      default: A
  tasks:
   - name: output vars
     debug:
      msg: The final solution is {{solution}}.
腳本:用戶輸入密碼後建立帳戶(須要對密碼加密,使用passlib庫,python.用戶能夠確認密碼


- hosts: test70
  remote_user: root
  vars_prompt:
    - name: "user_name"
      prompt: "Enter user name"
      private: no
    - name: "user_password"
      prompt: "Enter user password"
      encrypt: "sha512_crypt"
      confirm: yes
  tasks:
   - name: create user
     user:
      name: "{{user_name}}"
      password: "{{user_password}}"

經過命令行傳入變量

playbook中未定義變量,想直接引用可經過命令行傳入
ansible-playbook cmdvar.yml --extra-vars "pass_var=cmdline pass var" #傳入長變量
ansible-playbook cmdvar.yml -e 'pass_var="test" pass_var1="test1"' 短變量
若是未定義變量,也沒有傳入變量,會報錯,可playbook在playbo添加默認變量


- hosts: test70
  remote_user: root
  vars:
    pass_var: test_default
  tasks:
  - name: "Passing Variables On The Command Line"
    debug:
      msg: "{{pass_var}}"

在清單中爲主機添加變量,主機變量的使用範圍僅限於對應的主機
test70 ansible_host=10.1.1.70 testhostvar=test70_host_var 應用。使用{{testhostvar}}
yaml語法配置
all:
 hosts:
   test70:
     ansible_host: 10.1.1.70
     ansible_port: 22
     testhostvar: test70_host_var
     testhostvar1: test70_host_var1

主機組變量

[testB]
test70 ansible_host=10.1.1.70
test71 anisble_host=10.1.1.71
 
[testB:vars]
test_group_var1='group var test'
test_group_var2='group var test2'

YAML
all:
 children:
   testB:
     hosts:
       test70:
         ansible_host: 10.1.1.70
         ansible_port: 22
       test71:
         ansible_host: 10.1.1.71
         ansible_port: 22
     vars:
       test_group_var1: 'group var test1'
       test_group_var2: 'group var test2'

經過set_fact 定義變量

能夠經過set_fact將一個變量的值賦予另外一個變量,示例以下

- hosts: test70
  remote_user: root
  vars:
    testvar1: test1_string
  tasks:
  - shell: "echo test2_string"
    register: shellreturn
  - set_fact:
      testsf1: "{{testvar1}}"
      testsf2: "{{shellreturn.stdout}}"
  - debug:
      msg: "{{testsf1}} {{testsf2}}"

http://www----zsythink.net/archives/2698 最後有不懂得
內置變量

ansible all -m debug -a "msg={{ansible_version}}"
hostvars
inventory_hostname 當前被操做的主機名稱,是對應清單中的主機名,
inventory_hostname_short ,主機名更短
play_hosts 當前play 所操做的全部主機的列表
groups 分組的信息 每一個分組的主機,以及沒有被分組的單獨的主機
group_names
inventory_dir


uncomment this to disable SSH key host checking

71 #host_key_checking = False 第一次檢查key 取消註釋,默認回答yes

以sudo執行,須要輸入sudo口令,使用-K
chrony 同步 playbook
server 0.centos.pool.ntp.org iburst

allow 192.168.0.0/16

local stratum 10

計劃任務

不輸入確認口令 visudo NOPASSWORD:ALL

ansible all -m ping -u yon -b -K
-u 使用-u 默認使當前用戶(即在控制端使用的用戶)
啓用日誌 log_path
ansible 'appservers:!webservers' -m ping
ansible 命令行選項

修改ansible 默認模塊 -m shell 默認使用shell執行

user 建立家目錄,可是不生成家目錄的文件
文件分類放置,yaml和配置文件

變量優先級 變量文件 > playbook > -e > hosts主機變量 > hosts公共變量

跨角色 引用文件,從roles/httpd/files/xxxx.index.html

角色默認的路經

相關文章
相關標籤/搜索