深入掌握Ansible临时命令:实时控制服务器的最佳实践
创始人
2024-12-20 12:35:42
0

在这里插入图片描述

Ansible 运行临时命令详解

使用 ansible 命令运行临时命令
ansible  -m  [-a ''] [-i ] 
参数说明
  1. host-pattern:指定应在其上运行临时命令的受管主机。它可以是清单中的特定受管主机或主机组。例如,可以使用 --list-hosts 选项结合主机组来显示通过特定主机模式匹配的主机列表。

  2. -m 选项:指定在目标主机上运行的模块的名称。模块是为了实现特定任务而执行的小程序。

  3. -a 选项:传递给模块的参数,可以带引号来传递这些参数的列表。

  4. -i 选项:指定其他清单位置,取代当前 Ansible 配置文件中的默认位置。

示例

一个最简单的临时命令是使用 ping 模块。此模块不执行 ICMP ping,而是检查能否在受管主机上运行基于 Python 的模块。

ansible all -m ping 
servera.lab.example.com | SUCCESS => {     "ansible_facts": {         "discovered_interpreter_python": "/usr/libexec/platform-python"     },     "changed": false,     "ping": "pong" } 

Ansible 模块详解

常用 Ansible 模块分类
  1. 文件模块

    • copy:将本地文件复制到受管主机。
    • file:设置文件的权限和其他属性。
    • lineinfile:确保特定行是否存在于文件中。
    • synchronize:使用 rsync 同步内容。
  2. 软件包模块

    • package:使用操作系统的包管理器自动检测包管理器。
    • yum:使用 YUM 软件包管理器管理软件包。
    • apt:使用 APT 软件包管理器管理软件包。
    • dnf:使用 DNF 软件包管理器管理软件包。
    • gem:管理 Ruby gem。
    • pip:从 PyPI 管理 Python 软件包。
  3. 系统模块

    • firewalld:使用 firewalld 管理任意端口和服务。
    • reboot:重新启动计算机。
    • service:管理服务。
    • user:添加、删除和管理用户帐户。
  4. 网络工具模块

    • get_url:通过 HTTP、HTTPS 或 FTP 下载文件。
    • nmcli:管理网络。
    • uri:与 Web 服务交互。

常用模块示例

文件模块
blockinfile 模块

插入、更新或删除由标记包围的多行文本块。

- name: Insert/Update/Delete text block   blockinfile:     path: /etc/httpd/conf/httpd.conf     block: |                Options Indexes FollowSymLinks         AllowOverride None         Require all granted        
copy 模块

将文件从本地或远程主机复制到目标主机。

- name: Copy file with owner and permissions   copy:     src: /srv/myfile     dest: /etc/myfile     owner: foo     group: foo     mode: '0644' 
fetch 模块

从远程主机获取文件并将其存储在本地主机上。

- name: Fetch file from remote host   fetch:     src: /tmp/file.conf     dest: /local/path/file.conf     flat: yes 
file 模块

设置文件的权限、所有权和其他属性。

- name: Set permissions on /etc/foo.conf   file:     path: /etc/foo.conf     owner: foo     group: foo     mode: '0644' 
lineinfile 模块

确保特定行是否存在于文件中,如果不存在则插入该行。

- name: Ensure a line in file   lineinfile:     path: /etc/hosts     line: '127.0.0.1 localhost' 
synchronize 模块

使用 rsync 命令同步文件和目录。

- name: Synchronize directories   synchronize:     src: /src/dir/     dest: /dest/dir/     delete: yes     recursive: yes 
软件包模块
yum 模块

使用 YUM 管理软件包。

- name: Ensure latest version of a package is installed   yum:     name: httpd     state: latest 
apt 模块

使用 APT 管理软件包。

- name: Ensure a package is installed   apt:     name: apache2     state: present 
dnf 模块

使用 DNF 管理软件包。

- name: Ensure a package is installed using DNF   dnf:     name: nginx     state: present 
gem 模块

管理 Ruby gem。

- name: Install a gem   gem:     name: bundler     state: present 
pip 模块

从 PyPI 管理 Python 软件包。

- name: Install a Python package   pip:     name: django     state: present 
系统模块
firewalld 模块

管理 firewalld 服务。

- name: Add a service to the public zone   firewalld:     service: http     zone: public     permanent: yes     state: enabled 
reboot 模块

重启计算机。

- name: Reboot the machine   reboot:     msg: "Reboot initiated by Ansible"     connect_timeout: 5     reboot_timeout: 600 
service 模块

管理系统服务。

- name: Ensure a service is running   service:     name: httpd     state: started 
user 模块

管理用户帐户。

- name: Ensure a user is present   user:     name: jdoe     state: present     groups: wheel     append: yes 
网络工具模块
get_url 模块

通过 HTTP、HTTPS 或 FTP 下载文件。

- name: Download a file   get_url:     url: http://example.com/path/file.conf     dest: /etc/file.conf 
nmcli 模块

管理网络连接。

- name: Ensure a connection profile is present   nmcli:     conn_name: eth0     ifname: eth0     type: ethernet     ip4: 192.168.1.100/24     gw4: 192.168.1.1     dns4: 8.8.8.8 
uri 模块

与 Web 服务交互。

- name: Interact with a web service   uri:     url: http://example.com/api/     method: POST     body: '{ "key": "value" }'     body_format: json     headers:       Content-Type: "application/json" 

Ansible 模块示例

  1. 使用 command 模块运行简单命令

    ansible web -m command -a 'date' 
  2. 使用 service 模块管理服务

    启动 httpd 服务,并将其设置为随机器启动:

    ansible web -m service -a 'enabled=true name=httpd state=started' 
  3. 使用 shell 模块运行复杂命令

    用到管道复杂命令功能时建议用 shell

    ansible web -m shell -a 'echo 123 | passwd --stdin user1' 
  4. 使用 yum 模块安装软件包

    安装软件包:

    ansible web -m yum -a 'name=vsftpd state=present' 
  5. 使用 file 模块设置文件属性

    修改文件所属的用户、组、权限:

    ansible web -m file -a 'owner=test group=test mode=644 path=/tmp/1.file' 
  6. 使用 copy 模块进行文件复制

    利用 copy 模块进行文件的生成和拷贝:

    ansible web -m copy -a 'content=12121212 dest=/tmp/1 owner=root mode=660' ansible web -m copy -a 'src=/tmp/1 dest=/tmp/  

2 remote_src=yes owner=root mode=660’
```

  1. 使用 user 模块管理用户

    创建新用户:

    ansible web -m user -a 'name="user1"' ansible web -m command -a 'id user1' 

    可以通过状态 state=presentabsent 来进行用户的创建和删除。

  2. 使用 cron 模块建立周期性任务

    ansible web -m cron -a 'minute="*/10" job="/bin/echo hello" name="test cron job"' 

详细举例

lineinfile 模块
  1. 确保指定的一行文本存在于文件中,如果指定的文本本来就存在于文件中,则不做任何操作,如果不存在,默认在文件的末尾插入这行文本。
  2. 如果不止一行能够匹配正则,那么只有最后一个匹配正则的行才会替换,被匹配行会被替换成 line 参数指定的内容。

例如,受管机器上有个文件 /tmp/1.file,内容如下:

123 124 456 789 

增加一行内容:

ansible web -m lineinfile -a 'path=/tmp/1.file line="0000"' 

按正则来匹配查找:

ansible web -m lineinfile -a 'path=/tmp/1.file regexp="^12" line="#12"' 

删除指定行:

ansible web -m lineinfile -a 'path=/tmp/1.file regexp="^000" state=absent' 
synchronize 模块

使用 rsync 进行文件同步:

  • 将主控机上的 /tmp/1 文件传输到(push 推)web 组机器下的 /tmp 目录,默认是 push:

    ansible web -m synchronize -a 'src=/tmp/1 dest=/tmp' 
  • 拉的操作,这个时候,src 是在 web 组的机器上,dest 是在主控机上:

    ansible web -m synchronize -a 'mode=pull src=/tmp/1 dest=/usr/local/src' 
  • 将本地 /tmp 目录内 .txt 结尾的数据无差异且保持属性的同步到 /mnt 目录无差异化:

    ansible web -m synchronize -a 'src=/tmp dest=/mnt archive=yes delete=yes rsync_opts=--exclude=*.txt' 

相关内容

热门资讯

透视脚本!德普之星怎么作弊,w... 透视脚本!德普之星怎么作弊,wpk辅助购买,教你攻略(有挂揭秘);一、德普之星怎么作弊AI软件牌型概...
透视辅助!禅游游戏辅助脚本(辅... 透视辅助!禅游游戏辅助脚本(辅助挂)竟然真的有挂(详细辅助切实教程);1、在禅游游戏辅助脚本ai机器...
智星菠萝辅助怎么买!hhpok... 智星菠萝辅助怎么买!hhpoker德州机器人(透视)一直是真的有挂(解密教程)1、进入游戏-大厅左侧...
透视规律!wpk脚本辅助器,a... 透视规律!wpk脚本辅助器,aapoker透视脚本,解密教程(有挂黑科技)1、每一步都需要思考,不同...
透视辅助!泸州大二辅助(辅助挂... 透视辅助!泸州大二辅助(辅助挂)真是真的有挂(详细辅助攻略教程)1、在泸州大二辅助ai机器人技巧中,...
wepoker辅助下载!hhp... wepoker辅助下载!hhpoker辅助软件(透视)好像是有挂(新2025版);1、玩家可以在hh...
透视教学!德普之星辅助器,德普... 透视教学!德普之星辅助器,德普之星辅助功能如何打开,我来教教你(有挂辅助)进入游戏-大厅左侧-新手福...
透视辅助!广东雀神麻雀控制器(... 透视辅助!广东雀神麻雀控制器(辅助挂)本来是真的有挂(详细辅助安装教程);1、广东雀神麻雀控制器透视...
wpk透视辅助方法!wepok... 您好,wpk透视辅助方法这款游戏可以开挂的,确实是有挂的,需要了解加去Q群【1067239143】很...
透视ai代打!wpk辅助工具下... 透视ai代打!wpk辅助工具下载,wpk透视脚本下载,wpk教程(有挂攻略)1.wpk透视脚本下载 ...