本文档中并非直接复制粘贴使用请注意缩进格式。代码供大家复制粘贴学习测试使用。
请根据自己的palybook格式选择合适的片段代码避免改乱自己的配置
yum模块
用于处理 CentOS 系统上的包管理。
命令方式
ansible all -m yum -a "name=package_name state=present"
palybook
- name: Install a package using yum
hosts: all
become: yes
tasks:
- name: Install specific package
yum:
name: package_name
state: present
copy模块
将文件从控制机器复制到远程主机
命令方式
ansible all -m copy -a "src=/path/to/local/file dest=/path/on/remote/host owner=user group=group mode='0644'"
playbook
- name: Copy file to remote host
hosts: all
tasks:
- name: Copy a file from the control machine to the remote host
copy:
src: /path/to/local/file
dest: /path/on/remote/host
owner: user
group: group
mode: '0644'
file模块
管理文件和目录的属性,如创建、删除、修改权限等。
命令方式
ansible all -m file -a "path=/path/to/directory state=directory owner=username group=groupname mode='0755'"
palybook
- name: Manage file or directory attributes
hosts: all
tasks:
- name: Ensure a directory exists and has correct permissions
file:
path: /path/to/directory
state: directory
owner: username
group: groupname
mode: '0755'
lineinfile模块
添加符合特定模式的第一行。
命令方式(添加)
ansible all -m lineinfile -a "path=/path/to/file line='Content to append' state=present create=yes"
palybook(添加)
- name: Append a line to a file
hosts: all
tasks:
- name: Append content to the end of the file if not already present
lineinfile:
path: /path/to/file
line: 'Content to append'
state: present
create: yes
when: not ansible_check_mode
以下代码意思将test-1替换成test-01
命令方式(替换)
ansible all -m lineinfile -a "path=/path/to/file regexp='^.*test-1.*$' line='test-01' state=present"
palybook(替换)
- name: Replace text in a file
hosts: all
tasks:
- name: Replace 'test-1' with 'test-01'
lineinfile:
path: /path/to/file
regexp: '^.*test-1.*$'
line: 'test-01'
state: present
^ 表示行的开始;
.* 是一个通配符,表示任意数量(包括零个)的任意字符;
test-1 是你要匹配的确切文本;
.* 再次表示任意数量的任意字符;
$ 表示行的结束。
'^.*test-1.*$' 匹配的是整个文件中任何包含 test-1 的行。如果一行中包含 test-1,那么这条正则表达式就会匹配成功,并且这一行会被替换为 line 参数所指定的新行内容。
shell/command模块
shell/command - 执行 shell 命令或脚本。command 模块不会使用 shell,而 shell 模块会使用 shell 来执行命令。
命令方式(shell)
ansible all -m shell -a "echo 'Hello World'"
playbook(shell)
- name: Execute a shell command
hosts: all
tasks:
- name: Run a shell command on remote hosts
shell: echo "Hello World"
register: command_result
- debug:
msg: "{{ command_result.stdout }}"
命令方式(command)
ansible all -m command -a "echo 'Hello World'"
palybook(command)
- name: Execute a command
hosts: all
tasks:
- name: Run a command on remote hosts
command: echo "Hello World"
register: command_result
- debug:
msg: "{{ command_result.stdout }}"
service模块
启动、停止或重启服务。
命令方式
ansible all -m service -a "name=nginx state=started enabled=yes" 启动
ansible all -m service -a "name=nginx state=stopped enabled=no" 关闭
palybook
- name: Manage a service
hosts: all
tasks:
- name: Start a service
service:
name: nginx
state: started
enabled: yes #开机自启
- name: Stop a service
service:
name: nginx
state: stopped
enabled: no #永久关闭
- name: Restart a service
service:
name: nginx
state: restarted
user模块
管理系统用户账户。
命令方式
ansible all -m user -a "name=test password=123456 state=present"
ansible all -m user -a "name=mongodb state=present system=True shell=/usr/sbin/nologin" --become #创建系统用户
palybook(普通用户)
- name: Create a user with a password
hosts: all
become: yes
tasks:
- name: Ensure the 'test' user exists with specified password
user:
name: test
password: '123456'
state: present #确保用户存在
shell: /bin/bash
createhome: yes #确保为用户创建家目录
groups: test,root #可以指定多个属组
append: yes #表示如果用户已经存在于其他用户组中,则保留现有的用户组,并追加新的用户组
palybook( 系统用户不可登陆)
- name: Create a system user for MongoDB with nologin
hosts: all
become: yes
tasks:
- name: Ensure the 'mongodb' system user exists with nologin shell
community.general.user:
name: mongodb
state: present
system: yes
shell: /usr/sbin/nologin
createhome: no
synchronize模块(rsync)
synchronize远程同步目录多个文件
注意:
2、加斜杠(/)
src加斜杠:表示同步目录及其内容。
dest加斜杠:表示同步到一个目录。1、不加斜杠(/)
src不加斜杠:表示同步某个具体的文件或目录。
dest不加斜杠:表示同步到一个具体的文件或目录。通俗点讲不加/就是同步一整个目录加/同步目录下的内容
命令方式
ansible all -m synchronize -a "src=/path/to/source dest=/path/to/destination mode=pull delete=yes compress=yes"
palybook
- name: Synchronize files from source to destination
hosts: all
become: yes
tasks:
- name: Synchronize files using synchronize module
synchronize:
src: /usr/local/nginx
dest: /tmp/nginx
mode: pull
delete: yes
compress: yes
cron模块
定时任务模块
命令方式
ansible all -m cron -a "name='Backup task' job='0 2 * * * /path/to/script.sh'"
playbook
- name: Manage Cron Jobs
hosts: all
become: yes
tasks:
- name: Add a Cron job for backup
cron:
name: Backup task
job: '0 2 * * * /path/to/script.sh'
state: present
#删除一个定时任务
- name: Remove a Cron job
cron:
name: Backup task
state: absent
palybook循环
- name: Copy multiple configuration files
hosts: servers
tasks:
- name: Copy configuration files
ansible.builtin.copy:
src: "{{ item }}"
dest: "/etc/config/"
owner: root
group: root
mode: '0644'
loop:
- /local/path/to/file1.conf
- /local/path/to/file2.conf
- /local/path/to/file3.conf
以上copy模块示例不解释可能有点懵还不好理解,要知道循环是什么首先得有一定的认知最起码会写个简单的循环for循环,在playbook中循环又是怎么回事改怎么用呢?
loop 是什么?
在 Ansible 中,loop 是用来在一系列任务中重复执行相同操作的关键字。它可以让你在多个项目上执行相同的任务,而不需要为每个项目单独写一条指令。
item 是什么?
当你使用 loop 时,item 就是你在循环中处理的具体元素。你可以把它想象成循环中的“当前”项目。
在playbook中通俗来讲loop是动作根据循环逻辑,loop是要去遍历。item用来存放数据,而ansible中的copy模块就是他们的执行命令
上面的例子中意思就是loop遍历file1.conf file2.conf file3.conf这些文件,loop遍历完需要有一个地方来存放这些变量存放在item当中。ansible执行copy任务,copy根据指令使用item中的循环变量也就是loop遍历的file们开始进行复制
当然循环还有很多种写法就不一一列举了,感兴趣的可以百度一下查查都有哪些场景使用不同的循环方法