Ansible常用模塊使用詳解


Ansible常用模塊使用詳解

Ansible常用模塊有:

  • ping
  • lineinfile
  • yum
  • template
  • copy
  • user
  • group
  • service
  • raw
  • command
  • shell
  • script

Ansible常用模塊rawcommandshell的區別:

  • shell模塊調用的/bin/sh指令執行
  • command模塊不是調用的shell的指令,所以沒有bash的環境變量
  • raw很多地方和shell類似,更多的地方建議使用shell和command模塊。但是如果是使用老版本python,需要用到raw,又或者是客戶端是路由器,因為沒有安裝python模塊,那就需要使用raw模塊了

ansible常用模塊之ping

ping模塊用於檢查指定節點機器是否連通,用法很簡單,不涉及參數,主機如果在線,則回復pong

[root@yqh1 ~]# ansible yqh2 -m ping
yqh2 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}

ansible常用模塊之lineinfile

lineinfile模塊用於確保特定行是否在文件中。

//將yqh2中selinux設置為enforcing
[root@yqh1 ~]# ansible yqh2 -m lineinfile -a 'path=/etc/selinux/config regexp="^SELINUX=" line=SELINUX=enforcing'
yqh2 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "backup": "",
    "changed": false,
    "msg": ""
}

[root@yqh2 ~]# getenforce 
Enforcing

ansible常用模塊之command

command模塊用於在遠程主機上執行命令,ansible默認就是使用command模塊。

command模塊有一個缺陷就是不能使用管道符和重定向功能。

[root@yqh1 ~]# ansible yqh2 -m command -a 'getenforce'
yqh2 | CHANGED | rc=0 >>
Enforcing

//不加-m默認是command模塊
[root@yqh1 ~]# ansible yqh2 -a 'getenforce'
yqh2 | CHANGED | rc=0 >>
Enforcing

//command模塊不支持管道符,不支持重定向
[root@yqh1 ~]# ansible yqh2 -a 'ps -ef|grep vsftpd'
yqh2 | FAILED | rc=1 >>
error: unsupported SysV option

Usage:
 ps [options]

 Try 'ps --help <simple|list|output|threads|misc|all>'
  or 'ps --help <s|l|o|t|m|a>'
 for additional help text.

For more details see ps(1).non-zero return code

ansible常用模塊之raw

raw模塊用於在遠程主機上執行命令,其支持管道符與重定向

//支持重定向
[root@yqh1 ~]# ansible yqh2 -m raw -a 'echo "hello world" > /root/abc'
yqh2 | CHANGED | rc=0 >>
Shared connection to yqh2 closed.

[root@yqh2 ~]# pwd
/root
[root@yqh2 ~]# cat abc
hello world

//支持管道符
[root@yqh1 ~]# ansible yqh2 -m raw -a 'ps -ef|grep vsftpd'
yqh2 | CHANGED | rc=0 >>
root        2364    2326  0 18:31 pts/1    00:00:00 bash -c ps -ef|grep vsftpd
root        2382    2364  0 18:31 pts/1    00:00:00 grep vsftpd
Shared connection to yqh2 closed.

ansible常用模塊之shell

shell模塊用於在受控機上執行受控機上的腳本,亦可直接在受控機上執行命令。
shell模塊亦支持管道與重定向。

//支持重定向
[root@yqh1 ~]# ansible yqh2 -m shell -a 'echo "hello world" > /root/bcd'
yqh2 | CHANGED | rc=0 >>

[root@yqh2 ~]# pwd
/root
[root@yqh2 ~]# cat bcd
hello world

//支持管道符
[root@yqh1 ~]# ansible yqh2 -m shell -a 'ps -ef|grep vsftpd'
yqh2 | CHANGED | rc=0 >>
root        2482    2481  0 18:33 pts/1    00:00:00 /bin/sh -c ps -ef|grep vsftpd
root        2484    2482  0 18:33 pts/1    00:00:00 grep vsftpd

ansible常用模塊之script

script模塊用於在受控機上執行主控機上的腳本

//寫一個查看ip的腳本
[root@yqh1 ~]# mkdir scripts
[root@yqh1 ~]# vim /root/scripts/ip.sh
#!/bin/bash

ip a > /tmp/ip.txt

//讓yqh2執行腳本
[root@yqh1 ~]# ansible yqh2 -m script -a '/root/scripts/ip.sh'
yqh2 | CHANGED => {
    "changed": true,
    "rc": 0,
    "stderr": "Shared connection to yqh2 closed.\r\n",
    "stderr_lines": [
        "Shared connection to yqh2 closed."
    ],
    "stdout": "",
    "stdout_lines": []
}

//驗證
[root@yqh2 ~]# cat /tmp/ip.txt 
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:bc:a7:b5 brd ff:ff:ff:ff:ff:ff
    inet 192.168.100.2/24 brd 192.168.100.255 scope global noprefixroute ens160
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:febc:a7b5/64 scope link 
       valid_lft forever preferred_lft forever

ansible常用模塊之template

template模塊用於生成一個模板,並可將其傳輸至遠程主機上。

//下載一個阿里雲的yum源文件並開啟此源
[root@yqh1 ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo
[root@yqh1 ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@yqh1 ~]# sed -i 's|$releasever|8|' /etc/yum.repos.d/CentOS-Base.repo

//將設置好的阿里雲源傳到yqh2
[root@yqh1 ~]# ansible yqh2 -m template -a 'src=/etc/yum.repos.d/CentOS-Base.repo dest=/etc/yum.repos.d/CentOS-Base.repo'
yqh2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "e2c5e733b29668ef82633e043e094108e934d4d3",
    "dest": "/etc/yum.repos.d/CentOS-Base.repo",
    "gid": 0,
    "group": "root",
    "md5sum": "021e9bb5a28116f6b3fe608ddb806ebc",
    "mode": "0644",
    "owner": "root",
    "secontext": "system_u:object_r:system_conf_t:s0",
    "size": 1683,
    "src": "/root/.ansible/tmp/ansible-tmp-1609929998.1968453-5009-242589038739465/source",
    "state": "file",
    "uid": 0
}

//查看yqh2上是否有阿里雲源
[root@yqh2 ~]# ls /etc/yum.repos.d/
CentOS-Base.repo

ansible常用模塊之yum

yum模塊用於在指定節點機器上通過yum管理軟件,其支持的參數主要有兩個

  • name:要管理的包名
  • state:要進行的操作

state常用的值:

  • latest:安裝軟件
  • installed:安裝軟件
  • present:安裝軟件
  • removed:卸載軟件
  • absent:卸載軟件

若想使用yum來管理軟件,請確保受控機上的yum源無異常。

//在yqh1上使用yum模塊在yqh2上安裝vsftpd
[root@yqh1 ~]# ansible yqh2 -m yum -a 'name=vsftpd state=present'
yqh2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [
        "Installed: vsftpd-3.0.3-32.el8.x86_64"
    ]
}

//查看yqh2上是否安裝了vsftpd
[root@yqh2 ~]# rpm -qa|grep vsftpd
vsftpd-3.0.3-32.el8.x86_64

ansible常用模塊之copy

copy模塊用於復制文件至遠程受控機。

//將創建的abc文件復制到yqh2上的/tmp/
[root@yqh1 ~]# echo "hello world" > abc
[root@yqh1 ~]# ansible yqh2 -m copy -a 'src=/root/abc dest=/tmp/abc'
yqh2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "22596363b3de40b06f981fb85d82312e8c0ed511",
    "dest": "/tmp/abc",
    "gid": 0,
    "group": "root",
    "md5sum": "6f5902ac237024bdd0c176cb93063dc4",
    "mode": "0644",
    "owner": "root",
    "secontext": "unconfined_u:object_r:admin_home_t:s0",
    "size": 12,
    "src": "/root/.ansible/tmp/ansible-tmp-1609930360.463924-5094-234697478100730/source",
    "state": "file",
    "uid": 0
}

//驗證
[root@yqh2 tmp]# cat /tmp/abc 
hello world

ansible常用模塊之group

group模塊用於在受控機上添加或刪除組。

//在yqh2上創建yuqinghao組
[root@yqh1 ~]# ansible all -m group -a 'name=yuqinghao state=present'
yqh2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "gid": 1001,
    "name": "yuqinghao",
    "state": "present",
    "system": false
}

//驗證
[root@yqh2 ~]# grep yuqinghao /etc/group
yuqinghao:x:1001:

ansible常用模塊之user

user模塊用於管理受控機的用戶帳號。

//在yqh2上創建一個用戶,用戶名為jerry,uid為2000,設置其shell為/sbin/nologin,無家目錄
[root@yqh1 ~]# ansible yqh2 -m user -a 'name=jerry uid=2000 system=true shell=/sbin/nologin create_home=false state=present'
yqh2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "comment": "",
    "create_home": false,
    "group": 991,
    "home": "/home/jerry",
    "name": "jerry",
    "shell": "/sbin/nologin",
    "state": "present",
    "system": true,
    "uid": 2000
}

[root@yqh2 ~]# id jerry
uid=2000(jerry) gid=991(jerry) groups=991(jerry)

ansible常用模塊之service

service模塊用於管理受控機上的服務。

//將yqh2上的httpd服務啟動,並設置為開啟自啟
[root@yqh1 ~]# ansible yqh2 -m service -a 'name=httpd enabled=true state=started'
yqh2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "enabled": true,
    "name": "httpd",
    "state": "started",
    "status": {
······

//驗證
[root@yqh2 ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2021-01-06 19:00:45 CST; 2min 15s ago
     Docs: man:httpd.service(8)
 Main PID: 13104 (httpd)
   Status: "Running, listening on: port 80"
······


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM