安装

安装步骤请参考ansible从入门到入土:带你从安装到实现“hello world“

Script使用

编写脚本

脚本内容如下

#!/bin/bash
echo $USER
ip a

然后添加执行权限

chmod +x demo.sh

操作过程

liumou@liumou-PC:~$ vim demo.sh
liumou@liumou-PC:~$ cat demo.sh 
#!/bin/bash
echo $USER
ip a
liumou@liumou-PC:~$ chmod +x demo.sh 
liumou@liumou-PC:~$ 

联通测试

通过ping模块可以测试主机连接

liumou@liumou-PC:~$ ansible demo -m ping
liumou | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
liumou@liumou-PC:~$ 

开始执行

liumou@liumou-PC:~$ ansible demo -m script -a demo.sh 
liumou | CHANGED => {
    "changed": true,
    "rc": 0,
    "stderr": "Shared connection to ssh.liumou.site closed.\r\n",
    "stderr_lines": [
        "Shared connection to ssh.liumou.site closed."
    ],
    "stdout": "root\r\n1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000\r\n    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00\r\n    inet 127.0.0.1/8 scope host lo\r\n       valid_lft forever preferred_lft forever\r\n    inet6 ::1/128 scope host \r\n       valid_lft forever preferred_lft forever\r\n2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000\r\n    link/ether 52:54:00:83:b6:10 brd ff:ff:ff:ff:ff:ff\r\n    inet 10.0.20.5/22 brd 10.0.23.255 scope global eth0\r\n       valid_lft forever preferred_lft forever\r\n    inet6 fe80::5054:ff:fe83:b610/64 scope link \r\n       valid_lft forever preferred_lft forever\r\n3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default \r\n    link/ether 02:42:df:60:b1:00 brd ff:ff:ff:ff:ff:ff\r\n    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0\r\n       valid_lft forever preferred_lft forever\r\n    inet6 fe80::42:dfff:fe60:b100/64 scope link \r\n       valid_lft forever preferred_lft forever\r\n4: br-7d49c9bb1e4c: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default \r\n    link/ether 02:42:85:36:cf:8d brd ff:ff:ff:ff:ff:ff\r\n    inet 172.18.0.1/16 brd 172.18.255.255 scope global br-7d49c9bb1e4c\r\n       valid_lft forever preferred_lft forever\r\n26: veth4b447e0@if25: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default \r\n    link/ether 62:99:d9:f4:8e:9e brd ff:ff:ff:ff:ff:ff link-netnsid 0\r\n    inet6 fe80::6099:d9ff:fef4:8e9e/64 scope link \r\n       valid_lft forever preferred_lft forever\r\n28: vethbfce9b8@if27: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default \r\n    link/ether ce:ed:93:ce:ff:41 brd ff:ff:ff:ff:ff:ff link-netnsid 1\r\n    inet6 fe80::cced:93ff:fece:ff41/64 scope link \r\n       valid_lft forever preferred_lft forever\r\n",    
    "stdout_lines": [
        "root",
        "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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000",
        "    link/ether 52:54:00:83:b6:10 brd ff:ff:ff:ff:ff:ff",
        "    inet 10.0.20.5/22 brd 10.0.23.255 scope global eth0",
        "       valid_lft forever preferred_lft forever",
        "    inet6 fe80::5054:ff:fe83:b610/64 scope link ",
        "       valid_lft forever preferred_lft forever",
        "3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default ",
        "    link/ether 02:42:df:60:b1:00 brd ff:ff:ff:ff:ff:ff",
        "    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0",
        "       valid_lft forever preferred_lft forever",
        "    inet6 fe80::42:dfff:fe60:b100/64 scope link ",
        "       valid_lft forever preferred_lft forever",
        "4: br-7d49c9bb1e4c: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default ",                                                                                                  
        "    link/ether 02:42:85:36:cf:8d brd ff:ff:ff:ff:ff:ff",
        "    inet 172.18.0.1/16 brd 172.18.255.255 scope global br-7d49c9bb1e4c",
        "       valid_lft forever preferred_lft forever",
        "26: veth4b447e0@if25: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default ",                                                                                     
        "    link/ether 62:99:d9:f4:8e:9e brd ff:ff:ff:ff:ff:ff link-netnsid 0",
        "    inet6 fe80::6099:d9ff:fef4:8e9e/64 scope link ",
        "       valid_lft forever preferred_lft forever",
        "28: vethbfce9b8@if27: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default ",                                                                                     
        "    link/ether ce:ed:93:ce:ff:41 brd ff:ff:ff:ff:ff:ff link-netnsid 1",
        "    inet6 fe80::cced:93ff:fece:ff41/64 scope link ",
        "       valid_lft forever preferred_lft forever"
    ]
}
liumou@liumou-PC:~$