
ansible 命令运行临时命令ansible -m [-a ''] [-i ] host-pattern:指定应在其上运行临时命令的受管主机。它可以是清单中的特定受管主机或主机组。例如,可以使用 --list-hosts 选项结合主机组来显示通过特定主机模式匹配的主机列表。
-m 选项:指定在目标主机上运行的模块的名称。模块是为了实现特定任务而执行的小程序。
-a 选项:传递给模块的参数,可以带引号来传递这些参数的列表。
-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" } 文件模块:
copy:将本地文件复制到受管主机。file:设置文件的权限和其他属性。lineinfile:确保特定行是否存在于文件中。synchronize:使用 rsync 同步内容。软件包模块:
package:使用操作系统的包管理器自动检测包管理器。yum:使用 YUM 软件包管理器管理软件包。apt:使用 APT 软件包管理器管理软件包。dnf:使用 DNF 软件包管理器管理软件包。gem:管理 Ruby gem。pip:从 PyPI 管理 Python 软件包。系统模块:
firewalld:使用 firewalld 管理任意端口和服务。reboot:重新启动计算机。service:管理服务。user:添加、删除和管理用户帐户。网络工具模块:
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" 使用 command 模块运行简单命令:
ansible web -m command -a 'date' 使用 service 模块管理服务:
启动 httpd 服务,并将其设置为随机器启动:
ansible web -m service -a 'enabled=true name=httpd state=started' 使用 shell 模块运行复杂命令:
用到管道复杂命令功能时建议用 shell:
ansible web -m shell -a 'echo 123 | passwd --stdin user1' 使用 yum 模块安装软件包:
安装软件包:
ansible web -m yum -a 'name=vsftpd state=present' 使用 file 模块设置文件属性:
修改文件所属的用户、组、权限:
ansible web -m file -a 'owner=test group=test mode=644 path=/tmp/1.file' 使用 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’
```
使用 user 模块管理用户:
创建新用户:
ansible web -m user -a 'name="user1"' ansible web -m command -a 'id user1' 可以通过状态 state=present 或 absent 来进行用户的创建和删除。
使用 cron 模块建立周期性任务:
ansible web -m cron -a 'minute="*/10" job="/bin/echo hello" name="test cron job"' lineinfile 模块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'
上一篇:电脑cpu风扇的温控在哪里
下一篇:美图v4怎么刷以前的系统