上一篇博客中主要介紹了Ansible概述及Ansible部署,詳見如下連接自動化運維之Ansible概述及Ansible部署請添加連接描述
本篇將介紹Ansible命令應用之經常使用模塊,以實例的形式進行演示。mysql
ansible<host-pattern>[-m module_name][-a args] <host-pattern> //對哪些主機生效 [-m module_name] //要使用的模塊 [-a args] //模塊特有參數
Ansible的命令行管理工具都是由一系列模塊、參數所支持的,能夠在命令後面加上-h或--help獲取幫助。如使用ansible-doc工具能夠經過ansible-doc -h或者ansible-doc --help查看其幫助信息。
ansible-doc是用來查看模塊幫助信息的工具,最主要的選項-l用來列出可以使用的模塊,-s用來列出某個模塊的描述信息和使用示列。如列出yum模塊的描述信息和操做動做:web
[root@localhost ansible]# ansible-doc -s yum - name: Manages packages with the `yum' package manager yum: allow_downgrade: # Specify if the named package and version is allowed to downgrade a maybe already installed higher version of that package. Note that setting allow_downgrade=True .....
默認模塊,用於運行系統命令,好比echo hello。不支持shell變量和管道。sql
[root@localhost ansible]# ansible 192.168.88.10 -m command -a 'date' //指定IP執行date命令 192.168.88.10 | SUCCESS | rc=0 >> 2018年 08月 01日 星期三 16:18:29 CST [root@localhost ansible]# ansible webserver -m command -a 'date' //指定組執行date命令 192.168.88.10 | SUCCESS | rc=0 >> 2018年 08月 01日 星期三 16:21:22 CST [root@localhost ansible]# ansible all -a 'date' //全部hosts執行date命令,不加-m默認使用command模塊 192.168.88.10 | SUCCESS | rc=0 >> 2018年 08月 01日 星期三 16:22:44 CST 192.168.88.12 | SUCCESS | rc=0 >> 2018年 08月 01日 星期三 16:22:44 CST
Ansible中的cron模塊用於定義任務計劃。其中有兩種狀態(state):present表示添加(省略狀態時默認使用),absent表示移除。shell
[root@localhost ansible]# ansible-doc -s cron //查看cron模塊信息 - name: Manage cron.d and crontab entries cron: backup: # If set, create a backup of the crontab before it is modified. The location of the backup is returned in the `backup_file' variable by this module. cron_file: # If specified, uses this file instead of an individual user's crontab. If this is a relative path, it [root@localhost ansible]# ansible webserver -m cron -a 'minute="*/1" job="/bin/echo heihei" name="test cron job"' //添加計劃任務 192.168.88.10 | SUCCESS => { "changed": true, "envs": [], "jobs": [ "test cron job" ] } [root@localhost ansible]# ansible webserver -a 'crontab -l' //查看計劃任務 192.168.88.10 | SUCCESS | rc=0 >> #Ansible: test cron job */1 * * * * /bin/echo heihei [root@localhost ansible]# ansible webserver -m cron -a 'name="test cron job" state=absent' //移除計劃任務 192.168.88.10 | SUCCESS => { "changed": true, "envs": [], "jobs": [] }
Ansible中user模塊用於建立新用戶和更改、刪除已存在的用戶。其中name選項用來指明建立的用戶名稱。apache
[root@localhost ansible]# ansible mysql -m user -a 'name="test01"' //添加用戶test01 192.168.88.12 | SUCCESS => { "changed": true, "comment": "", "create_home": true, "group": 1001, "home": "/home/test01", "name": "test01", "shell": "/bin/bash", "state": "present", "system": false, "uid": 1001 } [root@localhost ansible]# ansible mysql -a 'tail /etc/passwd' //查看新添加用戶test01 192.168.88.12 | SUCCESS | rc=0 >> setroubleshoot:x:993:988::/var/lib/setroubleshoot:/sbin/nologin sssd:x:992:987:User for sssd:/:/sbin/nologin gdm:x:42:42::/var/lib/gdm:/sbin/nologin gnome-initial-setup:x:991:986::/run/gnome-initial-setup/:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin admin:x:1000:1000:admin:/home/admin:/bin/bash test01:x:1001:1001::/home/test01:/bin/bash [root@localhost ansible]# ansible mysql -m user -a 'name="test01" state=absent' //刪除test01用戶 192.168.88.12 | SUCCESS => { "changed": true, "force": false, "name": "test01", "remove": false, "state": "absent" }
Ansible中的group模塊用於對用戶組進行管理bash
[root@localhost ansible]# ansible mysql -m group -a 'name=mysql gid=306 system=yes' //建立mysql組 192.168.88.12 | SUCCESS => { "changed": true, "gid": 306, "name": "mysql", "state": "present", "system": true } [root@localhost ansible]# ansible mysql -m user -a 'name=test02 uid=307 system=yes group=mysql' //將test02用戶添加到mysql組中 192.168.88.12 | SUCCESS => { "changed": true, "comment": "", "create_home": true, "group": 306, "home": "/home/test02", "name": "test02", "shell": "/bin/bash", "state": "present", "system": true, "uid": 307 }
Ansible中的copy模塊用於實現文件複製和批量下發文件。其中使用src來定義本地源文件路徑,使用dest定義被管理主機文件路徑,使用content則是經過指定信息內容來生成目標文件。app
[root@localhost ansible]# ansible mysql -m copy -a 'src=/etc/fstab dest=/opt/fstab.back owner=root mode=640' //將本地文件/etc/fstab複製到被管理主機上的/opt/fstab.back,將全部者設置爲root,權限設置爲640 192.168.88.12 | SUCCESS => { "changed": true, "checksum": "d0aba9a4f6cecb55d88d3e71a999cd716f3b0ac5", "dest": "/opt/fstab.back", "gid": 0, "group": "root", "md5sum": "3bc805fb84012b541f89cb823d6ca070", "mode": "0640", "owner": "root", "secontext": "system_u:object_r:usr_t:s0", "size": 689, "src": "/root/.ansible/tmp/ansible-tmp-1533113834.4-74195180796257/source", "state": "file", "uid": 0 } [root@localhost ansible]# ansible mysql -a 'ls -l /opt' //查看複製出的文件 192.168.88.12 | SUCCESS | rc=0 >> 總用量 4 -rw-r-----. 1 root root 689 8月 1 16:57 fstab.back drwxr-xr-x. 2 root root 6 3月 26 2015 rh [root@localhost ansible]# ansible mysql -m copy -a 'content="hello heihei!" dest=/opt/fstab.back' //將「hello heihei!」寫入/opt/fstab.back 192.168.88.12 | SUCCESS => { "changed": true, "checksum": "b783c5c2da963523d21deff007f6e6b97fc625dc", "dest": "/opt/fstab.back", "gid": 0, "group": "root", "md5sum": "0e7a9bdc00d20b6e3e1b03d836095644", "mode": "0640", "owner": "root", "secontext": "system_u:object_r:usr_t:s0", "size": 13, "src": "/root/.ansible/tmp/ansible-tmp-1533113953.5-109792337994377/source", "state": "file", "uid": 0 } [root@localhost ansible]# ansible mysql -a 'cat /opt/fstab.back' //查看寫入結果 192.168.88.12 | SUCCESS | rc=0 >> hello heihei!
在Ansible中使用file模塊來設置文件屬性。其中使用path指定文件路徑,使用src定義源文件路徑,使用name或dest來替換建立文件的符號連接。運維
[root@localhost ansible]# ansible mysql -m file -a 'owner=mysql group=mysql mode=644 path=/opt/fstab.back' //設置/opt/fstab.back的屬主屬組爲mysql,權限爲644 192.168.88.12 | SUCCESS => { "changed": true, "gid": 306, "group": "mysql", "mode": "0644", "owner": "mysql", "path": "/opt/fstab.back", "secontext": "system_u:object_r:usr_t:s0", "size": 13, "state": "file", "uid": 305 } [root@localhost ansible]# ansible mysql -m file -a 'path=/opt/fstab.link src=/opt/fstab.back state=link' //設置文件/optfstab.link爲文件/opt/fstab.back的鏈接文件 192.168.88.12 | SUCCESS => { "changed": true, "dest": "/opt/fstab.link", "gid": 0, "group": "root", "mode": "0777", "owner": "root", "secontext": "unconfined_u:object_r:usr_t:s0", "size": 15, "src": "/opt/fstab.back", "state": "link", "uid": 0 } [root@localhost ansible]# ansible mysql -m file -a "path=/opt/fstab.back state=absent" //刪除一個文件 192.168.88.12 | SUCCESS => { "changed": true, "path": "/opt/fstab.back", "state": "absent" } [root@localhost ansible]# ansible mysql -m file -a "path=/opt/test state=touch" //建立一個文件 192.168.88.12 | SUCCESS => { "changed": true, "dest": "/opt/test", "gid": 0, "group": "root", "mode": "0644", "owner": "root", "secontext": "unconfined_u:object_r:usr_t:s0", "size": 0, "state": "file", "uid": 0 }
在Ansible中使用ping模塊來檢測指定主機的連通性。ssh
[root@localhost ansible]# ansible all -m ping 192.168.88.12 | SUCCESS => { "changed": false, "ping": "pong" } 192.168.88.10 | SUCCESS => { "changed": false, "ping": "pong" }
在Ansible中使用service模塊來控制管理服務的運行狀態。其中,使用enabled表示是否開機自動啓動,取值爲true或者false;使用name定義服務名稱;使用state指定服務狀態,取值分別爲started、stopped、restarted。tcp
[root@localhost ansible]# ansible webserver -a 'systemctl status httpd' //查看webserver組內主機httpd服務狀態 192.168.88.10 | SUCCESS | rc=0 >> ● httpd.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled) Active: active (running) since 三 2018-08-01 17:41:13 CST; 5s ago Docs: man:httpd(8) man:apachectl(8) Main PID: 43797 (httpd) ... [root@localhost ansible]# ansible webserver -m service -a 'enabled=true name=httpd state=stopped' //關閉webserver組內主機httpd服務
Ansible中的shell模塊能夠在被管理主機上運行命令,並支持像管道符等功能的複雜命令。
[root@localhost ansible]# ansible mysql -m shell -a 'echo abc123|passwd --stdin mysql' //建立用戶使用無交互模式給用戶設置密碼 192.168.88.12 | SUCCESS | rc=0 >> 更改用戶 mysql 的密碼 。 passwd:全部的身份驗證令牌已經成功更新。
Ansible中的script模塊能夠將本地腳本複製到被管理主機上進行運行。須要注意的是,使用相對路徑來指定腳本。
[root@localhost opt]# vi test.sh //編寫測試腳本 [root@localhost opt]# chmod +x test.sh //給予執行權限 [root@localhost opt]# ansible mysql -m script -a 'test.sh' //在mysql組中執行test腳本 192.168.88.12 | SUCCESS => { "changed": true, "rc": 0, "stderr": "Shared connection to 192.168.88.12 closed.\r\n", "stderr_lines": [ "Shared connection to 192.168.88.12 closed." ], "stdout": "", "stdout_lines": [] } [root@localhost ~]# cat /opt/script.txt //在mysql組的主機上進行查看 hello ansible from script
Ansible中的yum模塊負責在被管理主機上安裝和卸載軟件包,可是須要提早在每一個節點配置本身的YUM倉庫。其中使用name指定要安裝的軟件包,還須要帶上軟件包的版本號,不然安裝最新的軟件包;使用state指定安裝軟件包的狀態,present、latest用來表示安裝,absent表示卸載。
[root@localhost opt]# ansible mysql -m yum -a 'name=zsh' //在mysql組的主機上安裝zsh軟件包 192.168.88.12 | SUCCESS => { "changed": true, "msg": "", "rc": 0, "results": [ "Loaded plugins: fastestmirror, langpacks\nLoading mirror speeds from cached hostfile\n * base: mirrors.nwsuaf.edu.cn\n * epel: mirrors.ustc.edu.cn\n * extras: mirrors.shu.edu.cn\n * updates: mirrors.shu.edu.cn\nResolving Dependencies\n--> Running transaction check\n---> Package zsh.x86_64 0:5.0.2-28.el7 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nInstalling:\n zsh x86_64 5.0.2-28.el7 base 2.4 M\n\nTransaction Summary\n================================================================================\nInstall 1 Package\n\nTotal download size: 2.4 M\nInstalled size: 5.6 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Installing : zsh-5.0.2-28.el7.x86_64 1/1 \n Verifying : zsh-5.0.2-28.el7.x86_64 1/1 \n\nInstalled:\n zsh.x86_64 0:5.0.2-28.el7 \n\nComplete!\n" ] } [root@localhost ~]# rpm -q zsh //在mysql主機上查看zsh軟件包安裝狀況 zsh-5.0.2-28.el7.x86_64
在Ansible中使用setup模塊收集、查看被管理主機的facts(facts是Ansible採集被管理主機設備信息的一個功能)。每一個被管理主機在接收並運行管理命令以前,都會將本身的相關信息(操做系統版本、IP地址等)發送給控制主機。
[root@localhost opt]# ansible mysql -m setup //查看mysql組主機的facts信息 192.168.88.12 | SUCCESS => { "ansible_facts": { "ansible_all_ipv4_addresses": [ "192.168.122.1", "192.168.88.12" ], "ansible_all_ipv6_addresses": [ "fe80::c3db:eb90:1263:4a4c" ], "ansible_apparmor": { ...
下一篇將介紹Ansible Playbook的使用,敬請期待!