轉自https://www.cnblogs.com/trymybesttoimp/p/6223979.html
1. 查看支持的模塊
[root@localhost ~]# ansible-doc -l
這里我們看下ansible的支持的模塊個數
[root@localhost ~]# ansible-doc -l |wc -l #查看支持的模塊個數 1039 [root@localhost ~]# ansible --version #查看我們的ansible版本號 ansible 2.3.1.0 config file = /etc/ansible/ansible.cfg configured module search path = Default w/o overrides python version = 2.6.6 (r266:84292, Aug 18 2016, 14:53:48) [GCC 4.4.7 20120313 (Red Hat 4.4.7-17)]
2.獲取模塊的幫助
這里我們使用ansible-doc獲取下command模塊的使用方式。
[root@localhost ~]# ansible-doc command
3.1 command模塊
command :作為ansible的默認模塊,可以允許遠程主機范圍內的所有shell命令。
注意: 在command的命令中含有像`$ HOME'這樣的變量和像``<“',`”>“, `“”“”,“”;“”和“”&“'將無法正常工作(如果需要這些功能,請使用[shell]模塊)
[root@localhost ~]# ansible 192.168.168.11* -m command -a 'ip addr show dev eth0' 192.168.168.115 | SUCCESS | rc=0 >> 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:50:56:29:8d:e2 brd ff:ff:ff:ff:ff:ff inet 192.168.168.115/24 brd 192.168.168.255 scope global eth0 inet6 fe80::250:56ff:fe29:8de2/64 scope link valid_lft forever preferred_lft forever 192.168.168.111 | SUCCESS | rc=0 >> 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:77:77:91 brd ff:ff:ff:ff:ff:ff inet 192.168.168.111/24 brd 192.168.168.255 scope global eth0 inet6 fe80::20c:29ff:fe77:7791/64 scope link valid_lft forever preferred_lft forever
3.2 script模塊
功能:在遠程主機上執行主控端的腳本,相當於scp+shell組合。
[root@localhost ~]# ansible all -m script -a "/home/test.sh 12 34"
3.3 shell模塊
功能:執行遠程主機的shell腳本文件
[root@localhost ~]# ansible all -m shell -a "/home/test.sh"
shell替代command執行
[root@localhost ~]# ansible 192.168.168.11* -m shell -a 'ip addr show dev eth0' 192.168.168.111 | SUCCESS | rc=0 >> 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:77:77:91 brd ff:ff:ff:ff:ff:ff inet 192.168.168.111/24 brd 192.168.168.255 scope global eth0 inet6 fe80::20c:29ff:fe77:7791/64 scope link valid_lft forever preferred_lft forever 192.168.168.115 | SUCCESS | rc=0 >> 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:50:56:29:8d:e2 brd ff:ff:ff:ff:ff:ff inet 192.168.168.115/24 brd 192.168.168.255 scope global eth0 inet6 fe80::250:56ff:fe29:8de2/64 scope link valid_lft forever preferred_lft forever
3.4 copy模塊
功能: 實現主控端向目標主機copy文件。
[root@localhost ~]# ansible all -m copy -a "src=/home/test.sh dest=/tmp/ owner=root group=root mode=0755"
#src 主控端文件位置
#dest 被控端目標位置
#owner 文件復制過去后的所有者
#group 文件復制過去后的所屬組
#mode 文件的權限設定,執行a+x這種方式
3.5 stat模塊
功能: 獲取遠程文件的狀態信息,包括atime,ctime,mtime,md5,uid,gid等信息。
[root@localhost ~]# ansible all -m stat -a "path=/etc/sysctl.conf"
3.6 yum模塊
功能: 安裝軟件包。
[root@localhost ~]# ansible all -m yum -a "name=httpd state=latest disable_gpg_check=yes enablerepo=epel"
#name 包名
#state (Choices: present, installed, latest, absent, removed)[Default: present]
#disable_gpg_check:禁止gpg檢查
#enablerepo:只啟動指定的repo
3.7 cron模塊
功能:遠程主機crontab配置
[root@localhost ~]# ansible all -m cron -a "name='test' hour='2-5' minute='*/5' day='1' month='3,4' weekday='1' job='ls -l' user=tom"
192.168.168.115 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": [
"test"
]
}
192.168.168.111 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": [
"test"
]
}
我們去被控主機看下生成的crontab作業
[root@localhost ~]# crontab -l -u tom #Ansible: test */5 2-5 1 3,4 1 ls -l
刪除指定crontab
[root@localhost ~]# ansible all -m cron -a "name=test state=absent"
3.8 mount模塊
功能: 掛載文件系統
[root@localhost ~]# ansible 192.168.168.111 -m mount -a "path=/mnt/data src=/dev/sd0 fstype=ext3 ots=ro state=present"
注:mount已經使用path代替了原來的name參數,但是name參數還是可以使用的。
3.9 service模塊
功能: 服務管理
[root@localhost ~]# ansible all -m service -a "name=httpd state=restarted" #啟動服務 [root@localhost ~]# ansible all -m service -a "name=httpd state=running" #查看服務狀態 [root@localhost ~]# ansible all -m service -a "name=httpd state=stoped" #停止服務
3.10 synchronize模塊
– 使用rsync同步文件,將主控方目錄推送到指定節點的目錄下,其參數如下:
– delete: 刪除不存在的文件,delete=yes 使兩邊的內容一樣(即以推送方為主),默認no
– src: 要同步到目的地的源主機上的路徑; 路徑可以是絕對的或相對的。如果路徑使用”/”來結尾,則只復制目錄里的內容,如果沒有使用”/”來結尾,則包含目錄在內的整個內容全部復制
– dest:目的地主機上將與源同步的路徑; 路徑可以是絕對的或相對的。
– dest_port:默認目錄主機上的端口 ,默認是22,走的ssh協議。
– mode: push或pull,默認push,一般用於從本機向遠程主機上傳文件,pull 模式用於從遠程主機上取文件。
– rsync_opts:通過傳遞數組來指定其他rsync選項。
# 將控制機器上的src同步到遠程主機上 - synchronize: src: some/relative/path dest: /some/absolute/path # 同步傳遞額外的rsync選項 - synchronize: src: /tmp/helloworld dest: /var/www/helloworld rsync_opts: - "--no-motd" - "--exclude=.git"
3.11、template模塊
基於模板方式生成一個文件復制到遠程主機(template使用Jinjia2格式作為文件模版,進行文檔內變量的替換的模塊。它的每次使用都會被ansible標記為”changed”狀態。)
– backup: 如果原目標文件存在,則先備份目標文件
– src:在ansible控制器上的Jinja2格式化模板的路徑。 這可以是相對或絕對的路徑。
– dest:將模板渲染到遠程機器上的位置。
force:是否強制覆蓋,默認為yes
– owner:目標文件屬主
– group:目標文件屬組
– mode:目標文件的權限模式,模式可以被指定為符號模式(例如,u + rwx或u = rw,g = r,o = r)。
# Example from Ansible Playbooks - template: src: /mytemplates/foo.j2 dest: /etc/file.conf owner: bin group: wheel mode: 0644 # 同樣的例子,但使用等效於0644的符號模式 - template: src: /mytemplates/foo.j2 dest: /etc/file.conf owner: bin group: wheel mode: "u=rw,g=r,o=r"
3.12、get_url 模塊
該模塊主要用於從http、ftp、https服務器上下載文件(類似於wget),主要有如下選項:
– sha256sum:下載完成后進行sha256 check;
– timeout:下載超時時間,默認10s
– url:下載的URL
– url_password、url_username:主要用於需要用戶名密碼進行驗證的情況
– dest:將文件下載到哪里的絕對路徑。如果dest是目錄,則使用服務器提供的文件名,或者如果沒有提供,將使用遠程服務器上的URL的基本名稱。
– headers:以格式“key:value,key:value”為請求添加自定義HTTP標頭。
- name: Download foo.conf get_url: url: http://example.com/path/file.conf dest: /etc/foo.conf mode: 0440 - name: Download file with custom HTTP headers get_url: url: http://example.com/path/file.conf dest: /etc/foo.conf headers: 'key:value,key:value' - name: Download file with check (sha256) get_url: url: http://example.com/path/file.conf dest: /etc/foo.conf checksum: sha256:b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c
13、file模塊
file模塊主要用於遠程主機上的文件操作,file模塊包含如下選項:
– force:需要在兩種情況下強制創建軟鏈接,一種是源文件不存在但之后會建立的情況下;另一種是目標軟鏈接已存在,需要先取消之前的軟鏈,然后創建新的軟鏈,有兩個選項:yes|no
– group:定義文件/目錄的屬組
– mode:定義文件/目錄的權限
– owner:定義文件/目錄的屬主
– path:必選項,定義文件/目錄的路徑
– recurse:遞歸的設置文件的屬性,只對目錄有效
– src:要被鏈接的源文件的路徑,只應用於state=link的情況
– dest:被鏈接到的路徑,只應用於state=link的情況
– state:
directory:如果目錄不存在,創建目錄
file:即使文件不存在,也不會被創建
link:創建軟鏈接
hard:創建硬鏈接
touch:如果文件不存在,則會創建一個新的文件,如果文件或目錄已存在,則更新其最后修改時間
absent:刪除目錄、文件或者取消鏈接文件
# 更改文件所有權,組和模式。 當使用八進制數指定模式時,第一個數字應始終為0。 - file: path: /etc/foo.conf owner: foo group: foo mode: 0644 # touch創建文件,使用符號模式設置權限(相當於0644) - file: path: /etc/foo.conf state: touch mode: "u=rw,g=r,o=r" # touch創建文件,添加/刪除一些權限 - file: path: /etc/foo.conf state: touch mode: "u+rw,g-wx,o-rwx" # 創建一個目錄,如果它不存在 - file: path: /etc/some_directory state: directory mode: 0755
14、set_fact
set_fact模塊可以自定義facts,這些自定義的facts可以通過template或者變量的方式在playbook中使用。如果你想要獲取一個進程使用的內存的百分比,則必須通過set_fact來進行計算之后得出其值,並將其值在playbook中引用。
下面是一個配置mysql innodb buffer size的示例:
- name: Configure MySQL hosts: mysqlservers tasks: - name: install MySql yum: name=mysql-server state=installed - name: Calculate InnoDB buffer pool size set_fact: innodb_buffer_pool_size_mb="{{ ansible_memtotal_mb / 2 }}" - name: Configure MySQL template: src=templates/my.cnf dest=/etc/my.cnf owner=root group=root mode=0644 notify: restart mysql - name: Start MySQL service: name=mysqld state=started enabled=yes handlers: - name: restart mysql service: name=mysqld state=restarted my.cnf的配置示例: # ` ansible_managed ` [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # Configure the buffer pool innodb_buffer_pool_size = {{ innodb_buffer_pool_size_mb|int }}M [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid
3.13 user模塊
功能: 遠程主機的用戶管理
[root@localhost ~]# ansible all -m user -a "name=jerry comment=' doubi jerry'" #添加用戶 詳細參數參考ansible-doc user [root@localhost ~]# ansible all -m user -a "name=jerry state=absent remove=yes" #刪除用戶