ansible 常用模块、scp、file、apt、service

文件传输

scp:
Ansible 能够以并行的方式同时 SCP 大量的文件到多台机器. 命令如下:

user1@master:~$ ansible host -i ansible_host -m copy -a "src=/home/user1/scptest.tar.gz dest=/home/user1/scptest.tar.gz"

file 文件模块:

修改文件权限

    user1@master:~$ ansible host -i ansible_host -m file -a "dest=/home/user1/bb mode=600"
        192.168.108.22 | CHANGED => {
            "ansible_facts": {
                "discovered_interpreter_python": "/usr/bin/python3"
            },
            "changed": true,
            "gid": 1001,
            "group": "user1",
            "mode": "0600",
            "owner": "user1",
            "path": "/home/user1/bb",
            "size": 0,
            "state": "file",
            "uid": 1001
        }

同时可以修改文件的权限,所有者与组信息;继续添加参数如:owner=user1 group=user1 ;

递归删除:

        user1@master:~$ ansible host -i ansible_host -m file -a "dest=/home/user1/aa state=absent"
            192.168.108.22 | CHANGED => {
                "ansible_facts": {
                    "discovered_interpreter_python": "/usr/bin/python3"
                },
                "changed": true,
                "path": "/home/user1/aa",
                "state": "absent"
            }
            192.168.108.23 | CHANGED => {
                "ansible_facts": {
                    "discovered_interpreter_python": "/usr/bin/python3"
                },
                "changed": true,
                "path": "/home/user1/aa",
                "state": "absent"
            }

软件包管理

参数介绍:

  -m : apt 或 yum
  name参数:软件包名称,比如:nginx
  state参数:软件包状态,present表示安装,absent表示删除
  update_cache参数:安装软件前是否更新缓存,如果update_cache=true表示安装前更新,默认是不更新


apt 安装相关包:

root@master:/home/user1# ansible host -i ansible_host -m apt -a "name=nginx state=present"
                192.168.108.23 | CHANGED => {
                    "ansible_facts": {
                        "discovered_interpreter_python": "/usr/bin/python3"
                    },
                    "cache_update_time": 1679452175,
                    "cache_updated": false,
                    "changed": true,
                    "stderr": "",
                    "stderr_lines": [],

apt 删除相关包:

root@master:/home/user1# ansible host -i ansible_host -m apt -a "name=nginx state=msg"
                192.168.108.22 | CHANGED => {
                    "ansible_facts": {
                        "discovered_interpreter_python": "/usr/bin/python3"
                    },
                    "changed": true,
                    "stderr": "",
                    "stderr_lines": [],

Service 服务模块: - enabled


Whether the service should start on boot.
*At least one of state and enabled are required.*
[Default: (null)]
type: bool
-name
Name of the service.
- state
`started'/`stopped' are idempotent actions that will not run commands unless necessary.
`restarted' will always bounce the service.
`reloaded' will always reload.
*At least one of state and enabled are required.*
Note that reloaded will start the service if it is not already started, even if your chosen init system wouldn't normally.
(Choices: reloaded, restarted, started, stopped)[Default: (null)]
如启动防火墙:

    root@master:/home/user1# ansible host -i ansible_host -m service -a "name=ufw state=started"
        192.168.108.23 | CHANGED => {
            "ansible_facts": {
                "discovered_interpreter_python": "/usr/bin/python3"
            },
            "changed": true,
            "name": "ufw",
            "state": "started",

关闭与重启直接修改state 参数的值即可;

posted @ 2023-03-24 17:30  橘子飞飞  阅读(848)  评论(0编辑  收藏  举报