ansible
安裝html
與salt對比python
- 相同
- 都是爲了同時在多臺機器上執行相同的命令
- 都是python開發
- 不一樣
- agent(saltstack須要安裝、ansible不須要)
- 配置(salt配置麻煩,ansible基本不用配置)
- 學習路線(salt比較陡峭,ansible比較平緩)
- 第三方工具(salt比較少)
- 開源社區的對接(salt比較少)
- 現有用戶(salt仍是ansible少一些)
- 二次開發擴展的能力(ansible比較差,尤爲是2.0之後)
- 大規模併發(200之內同樣,200以上salt會好一些,固然咱們也能夠對ansible作一些配置使其在200以上追上salt)
- Windows的支持(salt會好不少)
安裝
yum install -y ansible
查看ansible生成的命令,用到的命令mysql
ansible ansible-doc ansible-galaxy(下載第三方插件) ansible-playbook
查看ansible 安裝生成的linux
rpm -ql ansible |more /etc/ansible /etc/ansible/ansible.cfg #配置文件 /etc/ansible/hosts #主要文件
hosts文件詳解nginx
cat /etc/ansible/hosts # This is the default ansible 'hosts' file. # # It should live in /etc/ansible/hosts # # - Comments begin with the '#' character #註釋爲# # - Blank lines are ignored #空白行被忽略 # - Groups of hosts are delimited by [header] elements #主機組名被寫在[]裏面 # - You can enter hostnames or ip addresses #你能夠寫ip地址也能夠寫hostnames # - A hostname/ip can be a member of multiple groups #一個主機能夠被多個主機組包含
能夠在hosts文件中填寫的內容git
ansible_ssh_host ansible經過ssh鏈接的IP或者FQDN ansible_ssh_port SSH鏈接端口 ansible_ssh_user 默認SSH鏈接用戶 ansible_ssh_pass SSH鏈接的密碼(這是不安全的,ansible極力推薦使用--ask-pass選項或使用SSH keys) ansible_sudo_pass sudo用戶的密碼 ansible_connection SSH鏈接的類型:local,ssh,paramiko,在ansible 1.2以前默認是paramiko,後來智能選擇,優先使用基於ControlPersist的ssh(支持的前提) ansible_ssh_private_key_file SSH鏈接的公鑰文件
生成祕鑰web
ssh-keygen 一路回車就能夠
將公鑰複製到遠程主機上redis
ssh-copy-id root@192.168.226.101
查看ansible的命令幫助
ansible <host-pattern> [options]
-a MODULE_ARGS 模塊參數
-m MODULE_NAME 模塊名稱sql
-f forks 指定一次執行的機器mongodb
-C 測試
--list-hosts 查看運行的機器
-v 輸出詳細信息
第一個ansible程序
ansible test -m ping
獲取文檔
ansible-doc --help -s 指定模塊名稱 -l 列出全部的模塊
操做日誌
/var/log/message
命令相關
shell command script command模塊 [執行遠程命令] ansible all -a 'echo "hello world"' ansible all -a 'pwd' ansible all -a 'echo "oldboy"|passwd --stdin user1' #直接輸出結果 script模塊 [在遠程主機執行主控端的shell/python腳本 ] (使用相對路徑) ansible all -m script -a 'a.sh' #執行本地腳本 ansible all -a 'ls /' #查看文件目錄 shell模塊 [執行遠程主機的shell/python腳本,支持管道] ansible all -m shell -a 'echo oldboy|passwd --stdin user1' #設置密碼 ansible all -m shell -a 'cat /etc/shadow|grep user1' #查看用戶 ansible all -m shell -a 'python a.py' #執行遠程腳本
文件相關
copy dest 目標地址 src 本地地址 mode 權限 wrx/755 owner 屬主 group 屬組 backup content 直接寫內容,能夠用轉移符號 ansible all -m copy -a 'dest=/data src=/data/a.txt' #複製單個文件 ansible all -m copy -a 'src=/etc/init.d dest=/data/' ansible all -m copy -a 'src=/etc/init.d/ dest=/data' #若是帶/則複製裏面的內容,不帶/則複製目錄,若是是目錄的話,則會遞歸複製 ansible all -m copy -a 'content="hello world" dest=/data/test.txt' 直接輸入內容 file path src state file file表明拷貝後是文件 link link表明最終是個軟連接 directory directory表明文件夾 hard hard表明硬連接 touch touch表明生成一個空文件 absent absent表明刪除 ansible all -m file -a 'dest=/data/html state=directory' #建立目錄 ansible all -m file -a 'dest=/data/html2 state=link src=/etc' #建立軟連接 ansible all -m file -a 'dest=/data/a.txt state=touch' ansible all -m file -a 'dest=/data/a.txt state=absent' 刪除 fetch dest src ansible 10.211.55.14 -m fetch -a 'src=/data/test.txt dest=/data'
軟件相關
pip ansible all -m pip -a 'name=django==1.11'
ansible all -m pip -a "name='django==1.11' state=absent"#卸載
yum name state absent #卸載 installed #安裝 latest #安裝最新的版本 present #安裝 removed #卸載 ansible all -m yum -a 'name=python-pip state=latest' ansible all -m yum -a 'name=nginx state=latest' ansible all -m yum -a 'name=nginx state=absent'
service
name state ansible all -m service -a 'name=nginx state=started' ansible all -m service -a 'name=nginx state=stopped'
cron
name weekday 周 hour 時 day 天 minute 分鐘 month 月 job state absent #刪除 present #建立 ansible all -m cron -a 'name=testjob minute=1 job=ntpdate' ansible all -m cron -a 'name=testjob state=absent'
user
name password shell state uid group groups update_password home ansible all -m user -a 'name=usertest' ansible all -m user -a 'name=usertest state=absent'
group
gid name state #present 建立 absent 刪除 system #是不是系統組 ansible all -m group -a 'name=usertest ' ansible all -m group -a 'name=usertest state=absent'
playbook 劇本
操做都是冪等的
- 什麼是冪等的
- 操做過之後就不會操做了
爲何要用playbook
- 有一個更好的知識沉澱
- 有一些好的功能
知識回顧
ymal
格式
- 字典:
- key : value 冒號後面必須有空格
- 列表:
- -
建議一個文件處理一個對應一組相關的任務
ansible-playbook [options] playbook.yml -C # 幹跑,檢查 -f FORKS # 用來作併發,來指定併發數 --list-hosts #列出執行命令的主機 --syntax-check # 檢查語法 --list-tasks #列出playbook要執行的任務列表 -t TAGS, #指定要運行到tags -e EXTRA_VARS #給playbook傳遞變量
#單個playbook - hosts: web #指定要運行命令的主機 remote_user: root # 指定運行命令的用戶 tasks: #任務列表 - name: mkdir # 任務1,name是必須的 file: path=/data state=directory # 指定的模塊: 模塊的參數 - name: copyfile copy: src=/etc/fstab dest=/data/f
##多個playbook - hosts: web remote_user: root tasks: - name: mkdir file: path=/data state=directory - name: copyfile copy: src=/etc/fstab dest=/data/f - hosts: db remote_user: root tasks: - name: wget shell: "wget -O /data/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo"
##指定tags 這樣就能夠單獨調用yml文件中的設置了tags的任務 調用方法 nasible-playbook -t copyfile a.yml - hosts: web remote_user: root tasks: - name: mkdir file: path=/data state=directory - name: copyfile copy: src=/etc/fstab dest=/data/f tags: copyfile
任務是從上到下依次執行,每臺機器都執行完才執行下一個任務
變量引入
- 寫一個playbook,須要屢次建立,好比每次須要建立的不一樣的用戶
第一種設置變量的方法
## 傳遞變量 -e"key=value" - hosts: web remote_user: root tasks: - name: yum {{pkg_name}} pkg yum: name={{pkg_name}}
第二種設置變量的方法
- hosts: web remote_user: root vars: - pkg_name: memcached tasks: - name: yum {{pkg_name}} pkg yum: name={{pkg_name}}
第三種設置變量的方法
#在hosts文件裏面寫,值能夠不一樣 [web] 192.168.19.9 pkg_name=nginx 192.168.19.26 pkg_name=redis
第四種設置變量的方法
[web:vars] pkg_name=nginx
第五種傳參方式
第五種傳參方式: 經過register註冊,直接用.stdout來獲取值 - hosts: db remote_user: root tasks: - name: installbc yum: name=bc - name: sum shell: echo 20+21|bc register: sum - name: echo shell: echo {{sum}} > /tmp/sum.txt - name: createuser user: name=alex{{sum.stdout}}
變量的應用順序
-e > yml文件 > hosts文件 #命令行裏面是最高的,hosts文件是最低的
條件
when 條件判斷
- hosts: cache remote_user: root tasks: - name: copyfile1 copy: content='大弦嘈嘈如急雨' dest=/tmp/a.txt when: ansible_os_family=="RedHat" #只有爲真的時候纔會執行上面的操做 - name: copyfile2 copy: content='小弦切切如私語' dest=/tmp/b.txt when: ansible_os_family=="OpenBSD"
循環
with_items 循環添加
- hosts: cache remote_user: root tasks: - name: create user user: name={{item}} ## 循環下面的with_items with_items: - yuchao - yantao - name: create group group: name={{item}}## 循環下面的with_items with_items: - yuchao2 - yantao2
循環嵌套
- hosts: cache remote_user: root tasks: - name: create group group: name={{item}} with_items: - yuchao4 - yantao4 - name: create user user: name={{item.name}} group={{item.group}} #能夠經過字典取值 with_items: - {"name":yuchao3,"group":yuchao4} - {"name":yantao3,"group":yuchao4}
服務啓動
- hosts: web remote_user: root vars: - pkg_name: nginx tasks: - name: yum {{pkg_name}} pkg yum: name={{pkg_name}} state=installed - name: start service: name={{pkg_name}} state=started
handlers(每次變動文件須要重啓服務)
- hosts: web remote_user: root vars: - pkg_name: nginx tasks: - name: yum {{pkg_name}} pkg yum: name={{pkg_name}} state=installed - name: copyfile copy: src=nginx.conf dest=/etc/nginx/nginx.conf notify: restart #變動後纔會觸發handlers的任務 handlers: - name: restart service: name={{pkg_name}} state=restarted
template
基於janja2語言
- hosts: cache remote_user: root tasks: - name: install redis yum: name=redis - name: copyfile template: src=redis.conf.j2 dest=/etc/redis.conf ## 模板基於jinja2 - name: start service: name=redis state=started #模板文件放在templates(在yml文件同級目錄建立 ),能夠直接用相對路徑去調用配置文件
roles
做用:
- 結構清晰
- 能夠重用
tasks #目錄是必須的,存聽任務 templates #是存放模板 vars #用來存放變量 ### 切記,不能加-,加-報錯 files #用來存放文件 mkdir -p {nginx,uwsgi,mysql}/{tasks,templates,vars,files} #建立目錄結構的命令
hosts: web remote_user: root roles: - role: nginx tags: [ 'web','nginx'] # 指定tags - { role: http,tags: [ 'web','http']} # 指定tags 用 -t 來調用
- hosts: web remote_user: root roles: - role: nginx tags: [ 'web','nginx'] when: ansible_distribution_major_version== "7" # 條件 - role: http tags: [ 'web','http'] when: ansible_distribution_major_version== "6" # 條件
Ansible Galaxy
Ansible Galaxy 是一個自由網站,網站提供全部類型的由社區開發的 roles,這對於實現你的自動化項目是一個很好的參考。網站提供這些 roles 的排名、查找以及下載。
應用實例
你如今已經學過 tasks 和 handlers,那怎樣組織 playbook 纔是最好的方式呢?簡單的回答就是:使用 roles ! Roles 基於一個已知的文件結構,去自動的加載某些 vars_files,tasks 以及 handlers。基於 roles 對內容進行分組,使得咱們能夠容易地與其餘用戶分享 roles 。
若是 roles/x/tasks/main.yml 存在, 其中列出的 tasks 將被添加到 play 中
若是 roles/x/handlers/main.yml 存在, 其中列出的 handlers 將被添加到 play 中
若是 roles/x/vars/main.yml 存在, 其中列出的 variables 將被添加到 play 中
若是 roles/x/meta/main.yml 存在, 其中列出的 「角色依賴」 將被添加到 roles 列表中 (1.3 and later)
全部 copy tasks 能夠引用 roles/x/files/ 中的文件,不須要指明文件的路徑。
全部 script tasks 能夠引用 roles/x/files/ 中的腳本,不須要指明文件的路徑。
全部 template tasks 能夠引用 roles/x/templates/ 中的文件,不須要指明文件的路徑。
全部 include tasks 能夠引用 roles/x/tasks/ 中的文件,不須要指明文件的路徑。
production # inventory file for production servers 關於生產環境服務器的清單文件 stage # inventory file for stage environment 關於 stage 環境的清單文件 group_vars/ group1 # here we assign variables to particular groups 這裏咱們給特定的組賦值 group2 # "" host_vars/ hostname1 # if systems need specific variables, put them here 若是系統須要特定的變量,把它們放置在這裏. hostname2 # "" library/ # if any custom modules, put them here (optional) 若是有自定義的模塊,放在這裏(可選) filter_plugins/ # if any custom filter plugins, put them here (optional) 若是有自定義的過濾插件,放在這裏(可選) site.yml # master playbook 主 playbook webservers.yml # playbook for webserver tier Web 服務器的 playbook dbservers.yml # playbook for dbserver tier 數據庫服務器的 playbook roles/ common/ # this hierarchy represents a "role" 這裏的結構表明了一個 "role" tasks/ # main.yml # <-- tasks file can include smaller files if warranted handlers/ # main.yml # <-- handlers file templates/ # <-- files for use with the template resource ntp.conf.j2 # <------- templates end in .j2 files/ # bar.txt # <-- files for use with the copy resource foo.sh # <-- script files for use with the script resource vars/ # main.yml # <-- variables associated with this role defaults/ # main.yml # <-- default lower priority variables for this role meta/ # main.yml # <-- role dependencies webtier/ # same kind of structure as "common" was above, done for the webtier role monitoring/ # "" fooapp/ # ""
site.yml
在 site.yml 中,咱們包含了一個定義了整個基礎設施的 playbook.注意這個 playbook 是很是短的, 由於它僅僅包含了其餘 playbooks.記住, playbook 不過就是一系列的 plays:
--- # file: site.yml - include: webservers.yml - include: dbservers.yml
在諸如 like webservers.yml 的文件中(一樣也在頂層結構),咱們僅僅將 Web 服務器組與對應的 role 行爲作映射.一樣值得注意的是這也很是的短小精悍.例如:
--- # file: webservers.yml - hosts: webservers roles: - common - webtier
理念是咱們可以經過 「運行」(running) site.yml 來選擇整個基礎設施的配置.或者咱們可以經過運行其子集 webservers.yml 來配置. 這與 Ansible 的 「–limit」 相似,並且相對的更爲顯式:
ansible-playbook site.yml --limit webservers ansible-playbook webservers.yml
接下來的示例任務文件展現了一個 role 是如何工做的.咱們這裏的普通 role 僅僅用來配置 NTP,可是若是咱們想的話,它能夠作更多:
--- # file: roles/common/tasks/main.yml - name: be sure ntp is installed yum: pkg=ntp state=installed tags: ntp - name: be sure ntp is configured template: src=ntp.conf.j2 dest=/etc/ntp.conf notify: - restart ntpd tags: ntp - name: be sure ntpd is running and enabled service: name=ntpd state=running enabled=yes tags: ntp
這是個處理文件樣例.做爲一種審覈,它只有當特定的任務報告發生變化時會被觸發,並在每一個 play 結束時運行:
--- # file: roles/common/handlers/main.yml - name: restart ntpd service: name=ntpd state=restarted
筆記:
aaa={"a":11,"b":34,"c":12,"d":23} 1.實現k,v替換 2.按照value排序(("a":11),("c":12),("d":23),("b":34)) 昨日內容回顧 file 在被控機上建立文件夾或者文件,軟連接,硬連接 - path - src 目標源文件 - state - link - hard - directory - touch - absent - file - mode - group - owner fetch 拉取遠程主機的文件,以遠程主機的ip地址或者hostname建立目錄,並保存目錄的機構 - src - dest yum 安裝linux軟件包 - name - state pip 安裝python的三方包 - name - chdir - state service 操做服務 - name - state - started - stopped - restarted - reloaded - enabled cron 定時任務 - minute - hour - day - month - weekday - job - name - disabled - 分鐘最好不要用* user 建立用戶 - name - home - uid - group - groups - password - shell zsh 內建命令 外部命令 - system - remove - state group 建立用戶組 - gid - name - state - system web組 1. 建立alex10用戶,指定用戶的家目錄爲/opt/alex10,指定用戶的id爲3000,指定用戶的附加組爲root ansible web -m user -a "name=alex10,home=/opt/alex10,uid=3000,groups=root" 2. 建立wusir10用戶組 ansible web -m group -a "name=wusir10" 3. 將本地的/etc/fstab 複製到遠程,並指定屬主是alex10,屬組是wusir10 ansible web -m copy -a "src=/etc/fstab dest=/tmp owner=alex10 group=wusir10" 4. 安裝redis並啓動,設置開機自啓動 ansible web -m yum -a "name=redis" ansible web -m service -a "name=redis enabled=yes state=started" 5. 安裝django ansible web -m pip -a "name=django==1.11" 6. 設置計劃任務天天凌晨2點半備份/etc/目錄 ansible web -m cron -a "minute=30 hour=2 job='tar zcf /tmp/etcdate +%F.tar.gz /etc' name=backup" 7. 將被控節點上的網卡文件拉取到本地的/tmp目錄下 ansible web -m fetch -a "src=/etc/sysconfig/network-scripts/ifcfg-ens33 dest=/tmp" 8. 在被控節點上建立軟連接,源文件爲/etc/fstab,目標文件爲/tmp/f ansible web -m file -a "path=/tmp/f src=/etc/fstab state=link" 9. 安裝開發包組 ansible web -m yum -a "name='@Development Tools'" 補充 echo `date +%F` echo $(date +%F) a=10 echo '$a' $a echo "$a" 10 Ad-hoc 在命令行直接執行的 playbook yaml xml json - 字典 key:value - 列表 [] - - alex - wusir - yantao - yuchao [alex,wusir,yantao,yuchao] playbook格式 Usage: ansible-playbook [options] playbook.yml [playbook2 ...] -C, --check #白跑,執行可是不會有結果 --list-hosts #列出符合的主機 -f FORKS, --forks=FORKS #作併發 --syntax-check #檢查語法 -k, --ask-pass #輸入密碼 單個playbook - hosts: web remote_user: root tasks: - name: createuser user: name=alex20 home=/opt/alex20 uid=4000 多個playbook - hosts: web remote_user: root tasks: - name: createuser user: name=alex20 home=/opt/alex20 uid=4000 - name: copyfile copy: src=/etc/fstab dest=/tmp/fs 冪等性 無論執行多少次,獲得的結果都是同樣的 - hosts: web remote_user: root tasks: - name: createuser user: name=alex20 home=/opt/alex20 uid=4000 - name: copyfile copy: src=/etc/fstab dest=/tmp/fs - hosts: db tasks: - name: copyfile copy: src=/etc/fstab dest=/tmp/fs 傳參 第一種方式 - hosts: web tasks: - name: create{{user}} user: name={{user}} ansible-playbook -e user=wusir20 p3.yml 第二種方式 [web] 192.168.226.[101:102] user=alex30 192.168.226.104 user=alex100 第三種方式 [web:vars] user=alex31 第四種方式 - hosts: web vars: - user: alex32 tasks: - name: create{{user}} user: name={{user}} 第五種傳參方式 - hosts: web tasks: - name: yum yum: name=bc - name: sum shell: echo 11+22|bc register: user - name: echo shell: echo {{user.stdout}} > /tmp/echo.txt - name: create{{user.stdout}} user: name=alex{{user.stdout}} 優先級 -e > playbook > hosts setup ansible_all_ipv4_addresses #全部的ipv4地址 ansible_all_ipv6_addresses #全部的ipv6地址 ansible_architecture #系統的架構 ansible_date_time #系統時間 ansible_default_ipv4 #默認的ipv4地址 address ip地址 alias 網卡名稱 broadcast 廣播地址 gateway 網關 netmask 子網掩碼 network 網段 ansible_default_ipv6 #默認的ipv6地址 ansible_device_links #系統的磁盤信息 ansible_distribution #系統名稱 ansible_distribution_file_variety #系統的基於公司 ansible_distribution_major_version #系統的主版本 ansible_distribution_version #系統的所有版本 ansible_dns #系統的dns 默認udp 端口53 ansible_domain #系統的域 ldap ipv4 #ipv4地址 ansible_env #系統的環境 ansible_fqdn #系統的完整主機名 ansible_hostname #系統的簡寫主機名 ansible_kernel #系統的內核版本 ansible_machine #系統的架構 ansible_memtotal_mb #系統的內存 ansible_memory_mb #系統的內存使用狀況 ansible_mounts #系統的掛載信息 ansible_os_family #系統家族 ansible_pkg_mgr #系統的包管理工具 ansible_processor #系統的cpu ansible_processor_cores #每顆cpu的核數 ansible_processor_count #cpu的顆數 ansible_processor_vcpus #cpu的個數=cpu的顆數*每顆cpu的核數 ansible_python #系統python信息 ansible_python_version #系統python的版本 ansible_system #系統名字 tags - hosts: web tasks: - name: install yum: name=redis - name: copyfile copy: dest=/etc/redis.conf src=/etc/redis.conf tags: copy - name: start service: name=redis state=started ansible-playbook -t copy p7.yml# -t 是先給任務打上標籤,而後能夠只運行這一個任務,別的任務不運行 handlers - hosts: web tasks: - name: install yum: name=redis - name: copyfile copy: dest=/etc/redis.conf src=/etc/redis.conf tags: copy notify: restart#當copyfile任務運行完成時會出發這個restart任務 - name: start service: name=redis state=started handlers:#這個是restart任務 - name: restart service: name=redis state=restarted template 絕對路徑 - hosts: web tasks: - name: install yum: name=redis - name: copyfile template: dest=/etc/redis.conf src=/etc/redis.conf tags: copy notify: restart - name: start service: name=redis state=started handlers: - name: restart service: name=redis state=restarted mv redis.conf{,.j2} = mv redis.conf redis.conf.j2 相對路徑 - hosts: web tasks: - name: install yum: name=redis - name: copyfile template: dest=/etc/redis.conf src=redis.conf.j2 tags: copy notify: restart - name: start service: name=redis state=started handlers: - name: restart service: name=redis state=restarted 在當前目錄下建立一個templates的目錄,就可使用相對路徑 yy 複製一行 # yy 複製多行 p 粘貼 dd 刪除一行 # dd 刪除多行 d$ 從當前位置刪除到結尾 when - hosts: web tasks: - name: copyfile copy: content="大弦嘈嘈如急雨" dest=/tmp/a.txt when: ansible_distribution_major_version=="7" - name: copyfile copy: content="小弦切切如私語" dest=/tmp/a.txt when: ansible_distribution_major_version=="6" - hosts: web tasks: - name: copyfile copy: content="大弦嘈嘈如急雨" dest=/tmp/a.txt when: user=="4" - name: copyfile copy: content="小弦切切如私語" dest=/tmp/a.txt when: user=="3" with_items - hosts: web tasks: - name: createuser user: name={{item}} with_items: - alex50 - wusir50 - taibai50 - hosts: web tasks: - name: createuser user: name={{item}} with_items: - alex51 - wusir51 - taibai51 - name: creategroup group: name={{item}} with_items: - alex60 - wusir60 - taibai60 嵌套循環 - hosts: web tasks: - name: crateuser user: name={{item.name}} group={{item.group}} with_items: - {"name":alex52,"group":alex60} - {"name":wusir52,"group":wusir60} - {"name":taibai52,"group":taibai60} 今日內容總結 yaml - 字典 key:value - 列表 :[] - 傳參: - -e - host文件ip地址後面寫 - hosts文件裏面[web:vars] - playbook vars - register 獲取值stdout - -e > playbook > hosts文件 setup tags 打一個標籤,能夠指定單獨的標籤運行 handlers 被觸發的任務,notify 觸發 template 能夠動態的傳遞參數,在setup獲取 when 判斷 with_items 循環
一 1. 建立alex10用戶,指定用戶的家目錄爲/opt/alex10,指定用戶的id爲3000,指定用戶的附加組爲root ansible db -m user -a "name=alex10 uid=3000 home='/opt/alex10' groups=root " 二 建立wusir10用戶組 1. 建立wusir10用戶組 三 將本地的/etc/fstab 複製到遠程,並指定屬主是alex10,屬組是wusir10 1.ansible db -m copy -a "src=/etc/fstab dest=/tmp owner=alex group=wusir10" 四 安裝redis並啓動,設置開機自啓動 1 ansible db -m yum -a "name='redis' " 2 ansible db -m service -a "name='redis' enabled = yes" 五 安裝django 1 ansible db -m yum -a " name='python2-pip' " 2 ansible db -m pip -a "name='django==1.11' " 六 設置計劃任務天天凌晨2點半備份/etc/目錄 1. ansible db -m cron -a "name='etc_backup_copy' hour=2 minute=30 job='cp -rf /etc /tmp/etc_back' " 七 將被控節點上的網卡文件拉取到本地的/tmp目錄下 1 ansible db -m fetch -a "dest=/tmp src=/etc/sysconfig/network-scripts/ifcfg-ens33" 八 在被控節點上建立軟連接,源文件爲/etc/fstab,目標文件爲/tmp/f 1 ansible db -m file -a "state=link src=/etc/fstab path=/tmp/f" 九 安裝開發包組
一 安裝ansible 1. yum install -y wget #下載wget 2. wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/rpel-7.repo 3. yum install -y ansible 二 獲取db組/opt目錄下文件 1 ansible db -m command -a "ls /opt" 2 ansible db -m command -a "chdir=/ ls" 三 .建立xiaoqiang2用戶,並設置密碼爲xiaoqiang 1 ansible db -m shell -a "useradd xiaoqiang3"#建立新用戶 2.ansible db -m shell -a "echo 'xiaoqiang' | passwd xiaoqiang3"#這條命令既能夠修改用戶密碼也能夠給新用戶設置密碼 四..在/tmp目錄下生成xiaoqiang.txt文件,文件的內容爲"大弦嘈嘈如急雨,小弦切切如私語,嘈嘈切切錯雜彈,大珠小珠落玉盤" 1 ansible db -m shell -a "touch /tmp/xiaoqiang.txt" 2 ansible db -m copy -a " content='大弦嘈嘈如急雨,小弦切切如私語,嘈嘈切切錯 雜彈,大珠小珠落玉盤' dest=/tmp/xiaoqiang.txt" 五 將本地的網卡配置文件複製到遠程機器上,並命名爲network,用戶爲xiaoqiang2,用戶組爲xiaoqiang2,權限爲744 1. ansible db -m copy -a " src=/etc/sysconfig/network-scripts/ifcfg-ens33 dest=/tmp/network group=xiaoqiang3 owner=xiaoqiang3 mode=755 " 六 將/etc/rc3.d目錄複製到遠程機器上,指定用戶名爲alex,權限爲755 1. ansible db -m copy -a "src=/etc/rc3.d owner=alex mode=755 dest=/tmp/rc3.d " 七 將/etc/rc6.d目錄下的文件複製到遠程機器上 1. ansible db -m copy -a "src=/etc/rc3.d/ owner=alex mode=755 dest=/tmp/rc3.d " 八 寫一個腳本獲取web組的系統內核版本號 1. cd /tmp 2. vi a.sh 3. 在文本中寫入 #!/bin/bash cat /proc/version wq!保存退出 4. 在管控機上執行 ansible db -m script -a "/tmp/a.sh"
一 : 建立alex61,alex62,alex63用戶,分別指定用戶的家目錄爲/opt/alex61,/opt/alex62,/opt/alex63,指定用戶的id爲3000,3001,3002,指定用戶的附加組爲root - hosts: db tasks: - name: createuser user: name={{item.name}} home={{item.home}} uid={{item.uid}} groups=root with_items: - {"name":alex61,"home":/opt/alex61,"uid":3000} - {"name":alex62,"home":/opt/alex62,"uid":3001} - {"name":alex63,"home":/opt/alex63,"uid":3002} 二 建立wusir10用戶組 - hosts: db tasks: - name: create group group: name=wusir51 三 將本地的/etc/fstab 複製到遠程,並指定屬主是alex10,屬組是wusir10 - hosts: db tasks: - name: copy copy: src=/etc/fstab dest=/tmp owner=alex62 group=wusir51 四4.安裝nginx並啓動,設置開機自啓動,指定監聽地址爲ip地址 - hosts: db tasks: - name: install nginx yum: name=nginx - name: copyfile template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf notify: restart nginx tags: copy - name: power up service: name=nginx enabled=yes state=started - name: restart nginx service: name=nginx state=restarted server { listen {{ansible_default_ipv4.address}}:80 default_server; server_name _; root /usr/share/nginx/html; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; 五.安裝django - hosts: db tasks: - name: setup django pip: name='django==1.11' 六設置計劃任務天天凌晨2點半備份/etc/目錄 - hosts: db tasks: - name: backup etc cron: hour=2 minute=30 job='tar zcv /tmp/a.tar.gz /etc/' name='backup etc' 七.將被控節點上的網卡文件拉取到本地的/tmp目錄下 - hosts: db tasks: - name: fetch fetch: src=/etc/sysconfig/network-scripts/ifcfg-ens33 dest=/tmp 八.在被控節點上建立軟連接,源文件爲/etc/fstab,目標文件爲/tmp/f - hosts: db remote_user: root tasks: - name: link file: src=/etc/fstab path=/tmp/f state=link 九安裝開發包組 - hosts: db tasks: - name: deve tools yum: name='@Development Tools' 十 在centos6裏面添加「飛流直下三千尺」,centos7裏面添加「疑是銀河落九天」 - hosts: db remote_user: root tasks: - name: centos 6 copy: content='飛流直下三千尺' dest=/tmp/as.txt when: ansible_distribution_major_version == '6' - name: centos 7 copy: content='疑是銀河落九天' dest=/tmp/as.txt when: ansible_distribution_major_version == '7'
安裝
#下載wget yum install -y wget #將epel源下載到本地 wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo #安裝ansible yum install -y ansible
ansible 命令格式
Usage: ansible <host-pattern> [options] -a MODULE_ARGS, #模塊參數 -C, --check # 幹跑,白跑 -f FORKS, --forks=FORKS #指定併發,默認5個 --list-hosts #列出主機 -m MODULE_NAME# 模塊名稱 --syntax-check #檢查語法 -k #密碼
ping走什麼協議 ICMP
rpm -ql ansible|more # 查看ansible生成的文件 /etc/ansible /etc/ansible/ansible.cfg #配置文件 /etc/ansible/hosts /etc/ansible/roles #空文件夾
ansible配置主機配置文件
/etc/ansible/hosts
[web] 192.168.226.[101:102] [db] 192.168.226.102 192.168.226.103 [cache] 192.168.226.103
生成祕鑰
ssh-keygen 一路回車就能夠
將公鑰複製到遠程主機上
ssh-copy-id root@192.168.226.101
ansible 底層是經過ssh實現的
## www[001:006].example.com 表明 www001-www006(包含)
ansible的第一個命令
ansible 192.168.226.101 -m ping #單獨機器的ping ansible 192.168.226.101,192.168.226.102 -m ping #多個機器的ping ansible all -m ping #所有機器 ansible web -m ping #單個的組 ansible web,db -m ping #多個組的並集 ansible 'web:&db' -m ping #多個組的交集 ansible 'web:!db' -m ping #多個組的差集,在前面可是不在後面
host-pattern的格式
- 單個的ip地址
- 多個的ip地址,用,分割
- 單個組
- 多個組所有 all表示
- 並集
- web,db
- ‘web:db’
- 交集 ‘web:&db’
- 差集 ‘web:!db’
- 並集
ansible-doc 命令格式
ansible-doc [-l|-F|-s] [options] [-t <plugin type> ] [plugin] -j #以json的方式返回數據 -l, --list #列出全部的模塊 -s, --snippet #以片斷式顯示模塊信息 #直接查看完整信息
command
ansible web -m command -a "pwd" ansible web -m command -a "ls" ansible web -m command -a "chdir=/tmp pwd" #切換目錄並執行命令 ansible web -m command -a "creates=/tmp pwd" #由於tmp目錄存在,pwd不會執行 ansible web -m command -a "creates=/tmp2 pwd" #由於tmp2不存在,pwd執行 ansible web -m command -a "removes=/tmp2 pwd" #由於tmp2不存在pwd不執行 ansible web -m command -a "removes=/tmp pwd" #由於tmp目錄存在,pwd會執行 echo "1234" |passwd --stdin alex #設置用戶的密碼
shell
ansible web -m shell -a "echo '1234' |passwd --stdin alex" ansible web -m shell -a "chdir=/tmp pwd" shabang ansible 192.168.226.101 -m shell -a "bash a.sh" #執行shell腳本 ansible 192.168.226.101 -m shell -a "/root/a.sh" # 執行shell腳本,文件要有執行的權限 ansible 192.168.226.101 -m shell -a "/root/a.py" #執行Python文件
script
ansible db -m script -a "/root/a.sh" #執行本地的文件,管控機的文件 ansible db -m script -a "creates=/root/a.sh /root/a.sh" # 判斷被控機上的文件是否存在,若是不存在,就執行,若是存在,就跳過 ansible db -m script -a "creates=/tmp /root/a.sh" #判斷被控機上的文件
copy
backup #建立一個備份文件,以時間結尾 content #直接往文件裏面寫內容 dest #目標地址 group #屬組 mode# 文件的權限 W 2 R 4 X 1 owner #屬主 src #源地址 ansible web -m copy -a "src=/etc/fstab dest=/tmp/f" #複製本地文件到遠程主機,並修改文件名,屢次執行不會改變,由於checksum值是同樣的 ansible web -m copy -a "src=a.sh dest=/tmp/a.sh backup=yes" #複製本地文件,並備份 ansible web -m copy -a "src=a.sh dest=/tmp/a.sh backup=yes group=alex mode=755"# 複製本地文件到遠程主機,並指定屬組和權限 ansible web -m copy -a "src=/etc/init.d dest=/tmp backup=yes group=alex mode=755" #複製本地的目錄到遠程主機,修改目錄權限,則目錄裏面的文件也會跟着變動 ansible web -m copy -a "src=/etc/init.d/ dest=/tmp backup=yes group=alex mode=755" #複製本地目錄下的全部文件, ansible web -m copy -a "content='大弦嘈嘈如急雨,小弦切切如私語,嘈嘈切切錯 雜彈,大珠小珠落玉盤' dest=/tmp/b" #直接往文件裏面寫內容,覆蓋寫,慎用
今日內容總結
安裝:epel源
命令格式
ansible host-pattern options
host-pattern
- 單個的ip地址
- 多個的ip地址,用,分割
- 單個組
- 多個組所有 all
- 交集 'web:&db'
- 並集
- 'web:db'
- web,db
- 差集 ‘web:!db’
command
- chdir
- creates
- removes
shell 支持特殊字符 <>|&$;
- chdir
- creates
- removes
script 執行本地的腳本,判斷被控機上是否存在文件
- chdir
- creates
- removes
copy
- backup 備份,以時間結尾
- content 直接往文件裏面寫內容
- dest 目標文件,被控機的路徑
- src 源文件,直接寫目錄,則複製整個目錄,若是目錄後面以/結尾,則複製的目錄下的全部文件
- owner 屬主
- group 屬組
- mode 權限
FILE
group # 屬組
mode #權限
owner #屬主
path #路徑
state =link
state =hard
state
directory 目錄
file
touch 空文件
absent 刪除
link 軟鏈接
hard 硬連接
ansible web -m file -a "path=/alex5 state=directory owner=alex" #建立目錄,並制定屬主
ansible web -m file -a "path=/tmp/wusir.txt state=touch mode=777" #建立文件,並指定權限
ansible web -m file -a "path=/tmp/cron src=/var/log/cron state=link" #建立軟連接,連接的是本身的文件
ansible web -m file -a "path=/tmp/cron state=absent" # 刪除軟鏈接
ansible web -m file -a "path=/alex5 state=absent" #刪除文件夾
補充
軟鏈接 快捷方式 ln -s 源文件修改軟鏈接修改 源文件刪除軟鏈接失效 能夠跨分區
硬連接 硬盤的位置 ln 源文件修改硬連接修改 源文件刪除硬連接不變 不能夠跨分區
複製 開闢新空間 cp 源文件修改cp的不變 源文件刪除不變 能夠跨分區
fetch
dest 目標地址
src 源地址
ansible web -m fetch -a "src=/var/log/cron dest=/tmp" #拉取遠程主機的文件,並以主機ip地址或者主機名爲目錄,而且保留了原來的目錄結構
yum
補充
1.yum跟rpm有什麼關係,有什麼區別
rpm redhat package manager
yum 能夠解決依賴關係
2.yum源怎麼配置
[epel] #名稱
name=Extra Packages for Enterprise Linux 7 - $basearch #全名或者描述信息
baseurl=http://mirrors.aliyun.com/epel/7/$basearch # 源url地址
failovermethod=priority
enabled=1 #是否啓用,1啓用,0不啓用
gpgcheck=0 #是否檢驗key文件,0不校驗 1校驗
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
3.yum怎麼安裝包組
yum grouplist #查包組信息
disablerepo #禁用某個源
enablerepo #啓用某個源
name #包名
state
install
remove
ansible web -m yum -a "name=python2-pip" #安裝軟件包
ansible web -m yum -a "name=python2-pip,redis" #安裝多個包
ansible web -m yum -a "name='@Development Tools'" #安裝包組
ansible web -m yum -a "name=nginx state=absent" #卸載
pip
補充
pip freeze > a.txt #將本地環境導出 #原生centos命令
pip install -r a.txt #安裝全部的包
pip list #查看全部的包
pip uninstall flask #卸載
# 下載包
Python setup.py build
python setup.py install
chdir #切換目錄
name #包名
requirements #導出的文件
virtualenv #虛擬環境
ansible db -m pip -a "name='django==1.11' "#指定版本安裝 不然安裝最新版
ansible db -m pip -a "name='django'state=absent "#卸載
service
補充
啓動
systemctl start redis centos7 #原生centos命令
service redis start centos6
開機自啓動
systemctl enable redis centos7
chkconfig redis on centos6
0 關機 1單用戶 3命令行 5圖形界面 6重啓
ps -ef|grep redis #查進程
ss
-t tcp
-u udp
-n 以端口形式顯示
-l 顯示全部已經啓動的端口
-p 顯示pid
ssh 22
http 80
https 443
mysql 3306
redis 6379
mongodb 27017
oracle 1521
tomcat 8080
windows 遠程桌面 3389
ftp 20 21
django 8000
flask 5000
enabled #開機啓動
name #服務名稱
state
started
stopped
restarted
reloaded
user #啓動的用戶
ansible web -m service -a "name=redis state=started" #啓動
ansible web -m service -a "name=redis state=stopped" #關閉
ansible web -m service -a "name=redis enabled=yes" #設置開機自啓動
cron
補充
crontab
* * * * * job
分 時 日 月 周 任務
3/* * * * * job 每隔3分鐘執行一次
4 10-12 * * * job 10點-12點第4分鐘執行,包括12點
0-59 0-23 1-31 1-12 0-7
分鐘最好不要用*
分鐘最好不要用*
分鐘最好不要用*
分鐘最好不要用*
儘可能寫絕對路徑
* * * * * tar zcf /tmp/etc.tar.gz /etc #原生centos命令
備份
同步時間
刪除文件
-e 編輯
-l 查看
-r 刪除
day 天
disabled 禁用crontab,表現形式加#
hour 小時
job 任務
minute 分鐘
month 月
name 名字,描述信息
user 用戶
weekday 周
添加時名字必須不一樣,不加名稱爲None
ansible web -m cron -a "minute=12 name=touchfile job='touch /tmp/xiaoqiang.txt'"# 建立
ansible web -m cron -a "name=touchfile state=absent" #刪除
ansible web -m cron -a "minute=12 name=touchfile2 job='touch /tmp/xiaoqiang.txt' disabled=yes" #註釋
ansible web -m cron -a "name=None state=absent" #刪除名稱爲空的計劃任務
user
補充
tail /etc/passwd
tail /etc/shadow
id alex2
useradd #原生centos命令
-d 設置用戶家目錄
useradd -d /opt/alex2 alex2
-g 設置用戶的屬組
useradd -g alex2 alex3
-G, --groups 附加組
useradd -G alex2,root alex4
-r, --system 系統帳號
useradd -r alex5 # 系統帳號沒有家目錄
-s, --shell #設置用戶登陸後的shell
useradd -s /sbin/nologin alex8
-u, --uid UID #設置用戶的id
useradd -u 2000 alex9
設置了用戶的id之後,在設置用戶則從最大的id開始日後數
用戶分類
超級管理員 root 0
普通用戶
系統用戶 啓動一些服務或者進程,不能登陸 1-999 centos7 1-499 centos6 從大到小
登陸用戶 能夠登陸的用戶 1000-65535 centos7 500-65535 centos6
從小到大
userdel
userdel alex8 默認不刪除家目錄
-r 刪除用戶的家目錄
userdel -r alex9 刪除用戶並刪除用戶的家目錄
group 屬組
groups 附加組
home 設置家目錄
name 用戶名
remove 刪除用戶並刪除用戶的家目錄
shell 用戶登陸後的shell
system 系統用戶
uid 用戶的id
ansible web -m user -a "name=alex10 shell=/sbin/nologin home=/opt/alex10 uid=3000 groups=root" #建立用戶,並指定用戶的shell,家目錄,uid,以及附加組
ansible web -m user -a "name=alex11 shell=/sbin/nologin home=/opt/alex11"
ansible web -m user -a "name=alex12 system=yes" #建立系統用戶
ansible web -m user -a "name=alex12 state=absent" #刪除用戶,單不刪除家目錄
ansible web -m user -a "name=alex11 state=absent remove=yes" # 刪除用戶並刪除用戶的家目錄
group
補充
groupadd #原生centos命令
-g 設置id
-r 系統組
超級管理員組 root 0
普通組
系統組 1-999 centos7 1-499 centos6 從大到小
登陸用戶組 1000-65535 centos7 500-65535 centos6
從小到大
查看
tail /etc/group
groupadd -g 3000 wusir10
groupadd -r wusir11
gid 組id
system 系統組
name 名稱
ansible web -m group -a "name=wusir10 system=yes gid=5000" 建立系統組
ansible web -m group -a "name=wusir11" 建立普通的組
ansible web -m group -a "name=wusir11 state=absent" #刪除組