注释:接上一章
[root@pp ~]# ansible up -m shell -a "lvs"
up | CHANGED | rc=0 >>
[root@pp ~]#
[root@pp ~]# ansible up -m lvol -a "vg=vg0 lv=lv0 size=1G"
up | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": ""
}
[root@pp ~]#
[root@pp ~]# ansible up -m shell -a "lvs"
\up | CHANGED | rc=0 >>
lv0 vg0 -wi-a----- 1.00g
[root@pp ~]#
[root@pp ~]# ansible up -m lvol -a "vg=vg0 lv=lvo state=absent force=yes"
up | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"msg": ""
}
[root@pp ~]#
[root@pp ~]# ansible up -m firewalld -a "service=http immediate=yes permanent=yes state=enabled"
up | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"msg": "Permanent and Non-Permanent(immediate) operation"
}
[root@pp ~]#
[root@pp ~]# ansible up -m shell -a "firewall-cmd --list-all"
up | CHANGED | rc=0 >>
public (active)
target: default
icmp-block-inversion: no
interfaces: ens160
sources:
services: cockpit dhcpv6-client http ssh
ports:
protocols:
forward: no
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
[root@pp ~]#
[root@pp ~]# ansible up -m firewalld -a "port=808/tcp immediate=yes permanent=yes state=enabled"
up | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "Permanent and Non-Permanent(immediate) operation, Changed port 808/tcp to enabled"
}
[root@pp ~]#
[root@pp ~]# ansible up -m firewalld -a "port=808/tcp immediate=yes permanent=yes state=disabled"
up | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "Permanent and Non-Permanent(immediate) operation, Changed port 808/tcp to disabled"
}
[root@pp ~]#
[root@up opt]# cat aa.txt
aa=111
bb=222
[root@up opt]#
[root@pp ~]# ansible up -m replace -a "path=/opt/aa.txt regexp=^aa replace=xx=666"
up | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "1 replacements made"
}
[root@pp ~]#
[root@up opt]# cat aa.txt
xx=666=111
bb=222
[root@up opt]#
[root@up opt]# cat aa.txt
xx=666=111
bb=222
[root@pp ~]# ansible up -m lineinfile -a "path=/opt/bb.txt regexp=^aa line=xx=666"
up | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"backup": "",
"changed": true,
"msg": "line replaced"
}
[root@pp ~]#
[root@up opt]# cat bb.txt
xx=666
bb=222
[root@up opt]#
[root@pp ~]# ansible up -m debug -a "msg='hello' anible"
[root@pp ~]# cat test1.sh
#!/bin/bash
hostname
[root@pp ~]#
[root@pp ~]# chmod +x test1.sh
[root@pp ~]# ansible db -m script -a "./test1.sh"
up | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to up closed.\r\n",
"stderr_lines": [
"Shared connection to up closed."
],
"stdout": "up\r\n",
"stdout_lines": [
"up"
]
}
[root@pp ~]#
这样本地脚本直接在所有被管理主机上执行了。
[root@up ~]# grep group1 /etc/group
[root@up ~]#
[root@pp ~]# ansible up -m group -a "name=group1 state=present"
up | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"gid": 1002,
"name": "group1",
"state": "present",
"system": false
}
[root@pp ~]#
[root@up ~]# grep group1 /etc/group
group1:x:1002:
[root@up ~]#
[root@pp ~]# ansible up -m group -a "name=group1 state=absent"
up | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "group1",
"state": "absent"
}
[root@pp ~]#
[root@pp ~]# ansible up -m user -a "name=list group=root password={{'haha001' | password_hash('sha512')}} state=present"
up | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 0,
"home": "/home/list",
"name": "list",
"password": "NOT_LOGGING_PASSWORD",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 1002
}
[root@pp ~]#
[blab@up ~]$ su - list
密码:
[list@up ~]$
[root@pp ~]# ansible up -m user -a "name=list state=absent remove=yes"
[root@pp ~]# ansible up -m get_url -a "url=ftp://ftp.rhce.cc/auto/web.tar.gz dest=/opt/"
up | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"checksum_dest": null,
"checksum_src": "0453d8c9e9ce9bda838b659b901b74a2e709fb15",
"dest": "/opt/web.tar.gz",
"elapsed": 5,
"gid": 0,
"group": "root",
"md5sum": "41c019324f7b8160ce6944102a755de8",
"mode": "0644",
"msg": "OK (1406 bytes)",
"owner": "root",
"secontext": "system_u:object_r:usr_t:s0",
"size": 1406,
"src": "/home/blab/.ansible/tmp/ansible-tmp-1702950653.9560287-82670-130978094325115/tmppx0w93_4",
"state": "file",
"status_code": null,
"uid": 0,
"url": "ftp://ftp.rhce.cc/auto/web.tar.gz"
}
[root@pp ~]#
[root@up ~]# ls /opt/
aa.txt bb.txt web.tar.gz
[root@up ~]#
[root@pp ~]# ansible up -m setup
up | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"192.168.248.23",
"192.168.122.1"
],
"ansible_all_ipv6_addresses": [
"fe80::20c:29ff:fe20:dada"
],
"ansible_apparmor": {
1 键1:值
2 键2:{
3 子键a: 值a
4 子键b: 值b
5 ...
6 }
[root@pp ~]# ansible up -m setup -a "filter=ansible_bios_version"
up | SUCCESS => {
"ansible_facts": {
"ansible_bios_version": "6.00",
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false
}
[root@pp ~]#
[root@pp ~]# ansible up -m setup -a "filter=ansible_default_ipv4"
up | SUCCESS => {
"ansible_facts": {
"ansible_default_ipv4": {
"address": "192.168.248.23",
"alias": "ens160",
"broadcast": "192.168.248.255",
"gateway": "192.168.248.2",
"interface": "ens160",
"macaddress": "00:0c:29:20:da:da",
"mtu": 1500,
"netmask": "255.255.255.0",
"network": "192.168.248.0",
"type": "ether"
},
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false
}
[root@pp ~]#
[root@pp ~]# ansible up -m setup -a "filter=ansible_default_ipv4.address"
up | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false
}
[root@pp ~]#