1. 程式人生 > >ansible生產常用十大模組

ansible生產常用十大模組

來源:http://blog.51cto.com/liuzhengwei521/1895480

注:-a引數後的命令用單引號,單引號,單引號;雙引號有可能會出問題,特別是在user模組;

模組一:測試目標主機是否線上:ping模組

主機如果線上,則回覆pong

測試主機是否線上
[[email protected] ~]# ansible erp -m ping
192.168.10.6 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
192.168.10.7 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

模組二:command模組和shell

作用:用於在各被管理節點執行指定的命令

shell和command的區別:shell模組可以特殊字元,而command是不支援

顯示各節點的日期
[[email protected] ~]# ansible erp -m command -a 'date'
192.168.10.6 | SUCCESS | rc=0 >>
Tue Feb  7 09:20:35 CST 2017

192.168.10.7 | SUCCESS | rc=0 >>
Tue Feb  7 09:20:35 CST 2017
刪除各節點的/tmp/test目錄
[
[email protected]
~]# ansible erp -m command -a 'mkdir /tmp/test' 192.168.10.10 | SUCCESS | rc=0 >> 192.168.10.6 | SUCCESS | rc=0 >> [[email protected] ~]# ansible erp -m shell -a 'rm -rf /tmp/test' 192.168.10.6 | SUCCESS | rc=0 >> 192.168.10.10 | SUCCESS | rc=0 >>

模組三:user模組:管理使用者的模組

模組引數詳解:

    name:指定使用者名稱

    password:設定使用者密碼,password引數需要接受md5加密後的值

    state:使用者狀態,預設為present

        present:表示新增使用者

        absent:表示刪除使用者

    update_password:修改使用者密碼

        always:新密碼和舊密碼不同時進行修改

        on_create:為新建立的使用者指定密碼

    createhome:建立家目錄

        yes:預設項,即建立使用者預設是有家目錄的

        no:建立使用者時不建立家目錄

    remove:

        yes:刪除使用者家目錄,需要指定此引數

        no:預設項,刪除使用者時預設不刪除使用者的家目錄

    system:

        yes:預設建立為普通使用者,而非系統使用者

    如果不指定預設生成的選項有:

        home:建立家目錄

        shell:建立預設的shell為/bin/bash

        system:預設建立為普通使用者,而非系統使用者,指定是用yes

檢視幫助
[[email protected] ~]# ansible-doc -s user
user模組中的password是需要經過md5加密的
[[email protected] ~]# echo 123456 | openssl passwd -1 -stdin
$1$Q0WiDLXN$agpzPdHVa6.LbdMLsUXqI/
增加一個使用者
[[email protected] ~]# ansible erp -m user -a 'name=liuwei1 system=yes password=$1$Q0WiDLXN$agpzPdHVa6.LbdMLsUXqI/ state=present'
192.168.10.10 | SUCCESS => {
    "changed": true, 
    "comment": "", 
    "createhome": true, 
    "group": 496, 
    "home": "/home/liuwei1", 
    "name": "liuwei1", 
    "password": "NOT_LOGGING_PASSWORD", 
    "shell": "/bin/bash", 
    "state": "present", 
    "system": true, 
    "uid": 496
}
192.168.10.6 | SUCCESS => {
    "changed": true, 
    "comment": "", 
    "createhome": true, 
    "group": 496, 
    "home": "/home/liuwei1", 
    "name": "liuwei1", 
    "password": "NOT_LOGGING_PASSWORD", 
    "shell": "/bin/bash", 
    "state": "present", 
    "system": true, 
    "uid": 496
}
刪除一個使用者
[[email protected] ~]# ansible erp -m user -a 'name=liuwei remove=yes state=absent'
192.168.10.10 | SUCCESS => {
    "changed": true, 
    "force": false, 
    "name": "liuwei", 
    "remove": true, 
    "state": "absent"
}
192.168.10.6 | SUCCESS => {
    "changed": true, 
    "force": false, 
    "name": "liuwei", 
    "remove": true, 
    "state": "absent"
}
更新使用者的密碼
[[email protected] ~]# echo 654321 | openssl passwd -1 -stdin
$1$pfBO1D1D$UfxpHN4OlUbT8dWLe8te7.
[[email protected] ~]# ansible erp -m user -a 'name=liuwei1 update_password=always password=$1$pfBO1D1D$UfxpHN4OlUbT8dWLe8te7.'
192.168.10.10 | SUCCESS => {
    "append": false, 
    "changed": true, 
    "comment": "", 
    "group": 496, 
    "home": "/home/liuwei1", 
    "move_home": false, 
    "name": "liuwei1", 
    "password": "NOT_LOGGING_PASSWORD", 
    "shell": "/bin/bash", 
    "state": "present", 
    "uid": 496
}
192.168.10.6 | SUCCESS => {
    "append": false, 
    "changed": true, 
    "comment": "", 
    "group": 496, 
    "home": "/home/liuwei1", 
    "move_home": false, 
    "name": "liuwei1", 
    "password": "NOT_LOGGING_PASSWORD", 
    "shell": "/bin/bash", 
    "state": "present", 
    "uid": 496
}

模組四:任務計劃模組:cron

獲取幫助:ansibe-doc -s cron

模組引數詳解:

    state:

        present:建立任務

        absent:刪除任務

    backup:對遠端主機上的原任務計劃內容修改之前做備份

    job:要執行的任務

    name:該任務的描述(必須項)

    user:以哪個使用者的身份執行

    minute:分鐘(0-59,*,*/2,……),不寫預設為*

    hour:小時(0-23,*,*/2,……),不寫預設為*

    day:日(1-31,*,*/2,……),不寫預設為*

    month:月(1-12,*,*/2,……),不寫預設為*

    weekday:周(0-7,*,……),不寫預設為*

每隔10分鐘同步一下時間
[[email protected] ~]#ansible web -m cron -a 'name="sync time from ntpserver" minute=*/10 job="/usr/sbin/ntpdate 3.cn.pool.ntp.org"'

模組五:遠端複製備份模組:copy

獲取幫助:ansible-doc -s copy

模組引數詳解:  

    src:指定原始檔路徑,可以是相對路徑,也可以是絕對路徑,可以是目錄(並非是必須的,可以使用content,直接生成檔案內容)

    dest=:指定目標檔案路徑,只能是絕對路徑,如果src是目錄,此項必須是目錄

    owner:指定屬主

    group:指定屬組

    mode:指定許可權,可以以數字指定比如0644

    content:代替src,直接往dest檔案中寫內容,可以引用變數,也可以直接使用inventory中的主機變數

    backup:在覆蓋之前將原檔案備份,備份檔案包含時間資訊。有兩個選項:yes|no

    force:

        yes:預設項,如果目標主機包含該檔案,但內容不同,則強制覆蓋

        no:則只有當目標主機的目標位置不存在該檔案時,才複製

    directory_mode:遞迴的設定目錄的許可權,預設為系統預設許可權

複製本地檔案到遠端主機並對原檔案進行備份
[[email protected] tmp]# ansible erp -m copy -a 'src=/tmp/abc.txt dest=/tmp/ backup=yes'
192.168.10.10 | SUCCESS => {
    "backup_file": "/tmp/[email protected]:55:31~", 
    "changed": true, 
    "checksum": "13520f9e1a6f0b2ca9557d85825616c3680b4edc", 
    "dest": "/tmp/abc.txt", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "baae19d280afe4e2df1799daa37bebba", 
    "mode": "0644", 
    "owner": "root", 
    "size": 18, 
    "src": "/root/.ansible/tmp/ansible-tmp-1486436129.98-98537282809491/source", 
    "state": "file", 
    "uid": 0
}
192.168.10.6 | SUCCESS => {
    "backup_file": "/tmp/[email protected]:55:31~", 
    "changed": true, 
    "checksum": "13520f9e1a6f0b2ca9557d85825616c3680b4edc", 
    "dest": "/tmp/abc.txt", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "baae19d280afe4e2df1799daa37bebba", 
    "mode": "0644", 
    "owner": "root", 
    "size": 18, 
    "src": "/root/.ansible/tmp/ansible-tmp-1486436130.19-187127825454744/source", 
    "state": "file", 
    "uid": 0
}
向遠端主機的檔案中寫內容,會把原內容覆蓋掉
[[email protected] tmp]# ansible erp -m copy -a 'content="\nMy age is 26" dest=/tmp/abc.txt'
192.168.10.6 | SUCCESS => {
    "changed": true, 
    "checksum": "e1cbbec8927a295a767fa44e91dea6eeafa5a4f4", 
    "dest": "/tmp/abc.txt", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "55ec30ce5102aa8716b75ab5e98163a7", 
    "mode": "0644", 
    "owner": "root", 
    "size": 13, 
    "src": "/root/.ansible/tmp/ansible-tmp-1486436336.05-254449966786008/source", 
    "state": "file", 
    "uid": 0
}
192.168.10.10 | SUCCESS => {
    "changed": true, 
    "checksum": "e1cbbec8927a295a767fa44e91dea6eeafa5a4f4", 
    "dest": "/tmp/abc.txt", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "55ec30ce5102aa8716b75ab5e98163a7", 
    "mode": "0644", 
    "owner": "root", 
    "size": 13, 
    "src": "/root/.ansible/tmp/ansible-tmp-1486436348.0-188270058505341/source", 
    "state": "file", 
    "uid": 0
}

模組六:對遠端檔案管理的模組:file

獲取幫助:ansible-doc -s file

模組引數詳解:  

    owner:修改屬主

    group:修改屬組

    mode:修改許可權

    path=:要修改檔案的路徑

    recurse:遞迴的設定檔案的屬性,只對目錄有效

        yes:表示使用遞迴設定

    remote_src:如果yes它會從目標機上搜索src檔案,預設為no

    state:

        touch:建立一個新的空檔案

        directory:建立一個新的目錄,當目錄存在時不會進行修改

        link:建立軟連線,結果src一起使用此選項才生效

        hard:建立硬連線

        absent:刪除檔案,目錄,軟連線

    src:當state=link時,要被連線檔案的源路徑

    

新建一個檔案
[[email protected] tmp]# ansible erp -m file -a 'path=/tmp/liuwei.txt state=touch'
192.168.10.10 | SUCCESS => {
    "changed": true, 
    "dest": "/tmp/liuwei.txt", 
    "gid": 0, 
    "group": "root", 
    "mode": "0644", 
    "owner": "root", 
    "size": 0, 
    "state": "file", 
    "uid": 0
}
192.168.10.6 | SUCCESS => {
    "changed": true, 
    "dest": "/tmp/liuwei.txt", 
    "gid": 0, 
    "group": "root", 
    "mode": "0644", 
    "owner": "root", 
    "size": 0, 
    "state": "file", 
    "uid": 0
}
新建一個目錄
[[email protected] tmp]# ansible erp -m file -a 'path=/tmp/liuwei state=directory'
192.168.10.10 | SUCCESS => {
    "changed": true, 
    "gid": 0, 
    "group": "root", 
    "mode": "0755", 
    "owner": "root", 
    "path": "/tmp/liuwei", 
    "size": 4096, 
    "state": "directory", 
    "uid": 0
}
192.168.10.6 | SUCCESS => {
    "changed": true, 
    "gid": 0, 
    "group": "root", 
    "mode": "0755", 
    "owner": "root", 
    "path": "/tmp/liuwei", 
    "size": 4096, 
    "state": "directory", 
    "uid": 0
}
刪除檔案或者目錄
[[email protected] tmp]# ansible erp -m file -a 'path=/tmp/liuwei.txt state=absent'
192.168.10.10 | SUCCESS => {
    "changed": true, 
    "path": "/tmp/liuwei.txt", 
    "state": "absent"
}
192.168.10.6 | SUCCESS => {
    "changed": true, 
    "path": "/tmp/liuwei.txt", 
    "state": "absent"
}
遞迴設定檔案的屬主或者屬組
[[email protected] tmp]# ansible erp -m file -a 'path=/tmp/liuwei owner=root group=root recurse=yes'
192.168.10.10 | SUCCESS => {
    "changed": true, 
    "gid": 0, 
    "group": "root", 
    "mode": "0755", 
    "owner": "root", 
    "path": "/tmp/liuwei", 
    "size": 4096, 
    "state": "directory", 
    "uid": 0
}
192.168.10.6 | SUCCESS => {
    "changed": true, 
    "gid": 0, 
    "group": "root", 
    "mode": "0755", 
    "owner": "root", 
    "path": "/tmp/liuwei", 
    "size": 4096, 
    "state": "directory", 
    "uid": 0
}
為檔案設定軟連線
[[email protected] tmp]# ansible erp -m file -a 'src=/tmp/liuwei state=link path=/tmp/liuzhengwei'
192.168.10.6 | SUCCESS => {
    "changed": true, 
    "dest": "/tmp/liuzhengwei", 
    "gid": 0, 
    "group": "root", 
    "mode": "0777", 
    "owner": "root", 
    "size": 11, 
    "src": "/tmp/liuwei", 
    "state": "link", 
    "uid": 0
}
192.168.10.10 | SUCCESS => {
    "changed": true, 
    "dest": "/tmp/liuzhengwei", 
    "gid": 0, 
    "group": "root", 
    "mode": "0777", 
    "owner": "root", 
    "size": 11, 
    "src": "/tmp/liuwei", 
    "state": "link", 
    "uid": 0
}

模組七:在遠端主機執行本地指令碼:script

[[email protected] tmp]# ansible erp -m script -a '/tmp/a.sh'
192.168.10.10 | SUCCESS => {
    "changed": true, 
    "rc": 0, 
    "stderr": "", 
    "stdout": "Tue Feb  7 11:26:41 CST 2017\r\n", 
    "stdout_lines": [
        "Tue Feb  7 11:26:41 CST 2017"
    ]
}
192.168.10.6 | SUCCESS => {
    "changed": true, 
    "rc": 0, 
    "stderr": "", 
    "stdout": "Tue Feb  7 11:26:52 CST 2017\r\n", 
    "stdout_lines": [
        "Tue Feb  7 11:26:52 CST 2017"
    ]
}

模組八:收集遠端主機的資訊:setup

收集可用的facts,收集每個節點的相關資訊:架構資訊,IP,時間,域名,網絡卡,MAC,主機名,CPU等資訊。

這些收集的資訊,可以作為變數。

[[email protected] tmp]# ansible erp -m setup

模組九:安裝模組:yum

模組引數詳解:    

    name:表示要安裝軟體包的名字,預設最新的程式包,指明要安裝的程式包,可以帶上版本號

    state:表示是安裝還解除安裝

        present:預設的,表示為安裝

        lastest:安裝為最新的版本

        absent:表示刪除

模組十:服務模組:service

模組引數詳解:  

    enabled:表示設定服務開機是否啟動,取值為true或者false;enabled=yes

    name=:表示要控制哪一個服務

    state:

        started:表示現在就啟動此服務

        stopped:表示現在關閉此服務

        restarted:表示重啟此服務

    sleep:如果執行了restarted,在stop和start之間沉睡幾秒

    runlevel:定義在哪些級別可以自啟動

    arguments:表示向命令列傳遞的引數

[[email protected] tmp]#ansible erp -m service -a 'enabled=on name=httpd state=started'

模組十一:檔案編輯模組:lineinfile

模組引數詳解:

    path:指定要修改的配置檔案

    regexp:匹配要修改的內容

    line:要增加或者修改的內容

    state:

        absent:表示刪除,當匹配到時進行刪除

        present:表示增加,當匹配到時進行修改,當沒有匹配到時在最後增加一行,預設為此項

    backrefs:

        no:表示如果沒有匹配到,則增加line;如果匹配成功,則替換line;

        yes:表示如果沒有匹配到,則不變line;如果匹配成功,則替換line;

    backup:  

        no:表示如果沒有匹配到,則增加line;如果匹配成功,則替換line;不備份原檔案

        yes:表示如果沒有匹配到,則增加line;如果匹配成功,則替換line;備份原檔案

    insertafter(匹配的是此行):

        在匹配到的行之後新增一行

    insertbefore(匹配的是此行):

        在匹配到的行之前新增一行

修改nginx.conf測試:
[[email protected] playbook]# cat modify_nginx.yml 
---
- hosts: 192.168.2.111
  tasks:
  - name: "修改配置檔案"
    lineinfile:
      dest: "/etc/nginx/conf.d/default.conf"
      regexp: 'listen       80;'
      line: '    listen       8000;'
      #backup: yes
      backrefs: yes
    notify:
      - reload nginx
  handlers:
    - name: reload nginx
      service: name=nginx state=reloaded

注:經測試,當不新增backerfs: yes引數時,匹配到後也會進行替換,但當匹配到的內容不存在時,會在最後增加一行;所以當不增加backerfs引數時,要確定匹配到的內容存在;

wKioL1moBdWiPTAxAAA41nCeDsU246.png

替換存在的行:

#ansible oms -m lineinfile -a 'path=/etc/sudoers regexp="SYSTEM,SOFTWARE" line="STAPLES_ADMIN ALL=(ROOT) NOPASSWD:NETWORKING,LOCATE,STORAGE,DELEGATING,DRIVERS,SYSTEM,SOFTWARE,SERVICES,PROCESSES,FILE" backrefs=no'

匹配到的行後增加一行:

#ansible oms -m lineinfile -a 'dest=/etc/sudoers insertafter="Cmnd_Alias SYSTEM = /usr/sbin/reboot, /usr/sbin/halt, /usr/bin/ansible, /usr/bin/ssh" line="Cmnd_Alias FILE = /bin/mkdir,/bin/touch,/usr/bin/vim"'

刪除匹配到的行:

#ansible oms -m lineinfile -a 'path=/etc/sudoers state=absent regexp="PROCESSES,FILE"'

在末尾追加一行:insertafter指定為空就可以匹配到最後一行

#ansible 10.78.1.176 -m lineinfile -a 'dest=/tmp/test.txt line="jiadong" insertafter=""'

在末尾追加多行:換行符用\n表示

#ansible 10.78.1.176 -m lineinfile -a 'dest=/tmp/test.txt line="hao hao\nyao jia" insertafter=""'

模組十二:遠端解壓模組:unarchive -> 用於將壓縮包複製到遠端主機並解壓

模組引數:

    src:要複製的檔案

    dest:複製到遠端主機的目錄

    copy:是否把檔案複製到遠端主機

    owner:解壓後文件的屬主

yaml用法示例:

- name: "複製tomcat檔案到遠端伺服器並解壓"
  unarchive:
    src=apache-tomcat-7.0.64.zip
    dest=/data/
    copy=yes
    owner=tomcat