Ansibe基础操作
CentOS安装ansible
示例:
yum instsll -y epel-release
yum install -y ansible其它系统安装请参考[[Ansible安装]]
Ansible配置文件
/etc/ansible/ansible.cfg # 主配置文件
/etc/ansible/hosts # Inventory定义Inventory
vi /etc/ansible/hosts
[dbserver]
192.168.1.2 ansible_sudo_pass='123456' # ansibble_sudo_pass是当连接的用户不是root时,要使用sudo命令的密码
[webserver]
192.168.1.3 ansible_ssh_user=admin ansible_ssh_pass='12345' # 默认remote_user=root,在ansible.cfg中可以修改
192.168.1.4使用秘钥方式连接服务器
ssh-keygen
ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]Ansible命令基础
ansible <host-pattern> [-f forks] [-m module_name] [-a args]
<host-pattern> 这次命令对哪些主机生效的
inventory group name
ip
all
-f forks 一次处理多少个主机
-m module_name 要使用的模块
-a args 模块特有的参数常见模块
command Ansible的默认模块,可以运行远程权限范围所有的shell命令,不支持变量和管理符。
command Ansible的默认模块,可以运行远程权限范围所有的shell命令,不支持变量和管理符。ansible all -a 'date'cron 计划任务
cron 计划任务month # 月
minute # 分
day # 天
hour # 小时
weekday # 周几
job # 指定任务
state # 表示添加还是删除
present # 添加(默认)
absent # 删除
添加、删除定时任务示例
ansible test -m cron -a 'minute="*/2" job="echo abc >> /tmp/log" name="test cron job"'
ansible test -a 'crontab -l'
ansible test -m cron -a 'minute="*/2" job="echo abc >> /tmp/log" name="test cron job" state=absent'
user 用户账号管理
user 用户账号管理name
uid
state
present # 添加(默认)
absent # 删除
group # 所属组
groups # 附加组
home
createhome # 是否创建家目录
comment # 注释信息
system # 是否为系统用户
示例
ansible all -m user -a 'name="username"'
ansible all -m user -a 'name="username" state=absent'group 用户组管理
group 用户组管理gid
name # 组外
state
present # 添加(默认)
absent # 删除
system # 是否为系统用户
示例
ansible test -m gruop -a 'name=mysql gid=306 system=yes'
ansible test -m user -a 'name=mysql uid=306 system=yes group=mysql'copy 复制文件
copy 复制文件src
dest
owner # 所有者
group # 所有组
mode # 权限
content # 取代src=,直接秀此处的信息生成文件内容
示例
ansible test -m copy -a 'src=./filename dest=/root/ owner=root mode=640'
ansible test -m copy -a 'content="Hello world" dest=/tmp/txtfile'file 设置文件属性
file 设置文件属性path|dest|name # 要操作的文件具体路径
示例
# 修改文件权限
ansible dbserver -m file -a 'owner=mysql group=mysql mode=644 path=/tmp/txtfile'
# 链接文件
ansible dbserver -m file -a 'path=/newpath/txtfile src=/tmp/txtfile state=link'
ping 测试指定主机是否能连接
ping 测试指定主机是否能连接ansible test -m ping service 管理服务运行状态
service 管理服务运行状态enabled # 是否开机启动
name # 服务名
state # 指定服务状态
started # 启动
stoped # 停止
restarted # 重启
arguments # 服务参数
示例
ansible dbserver -m service -a 'enabled=true name=mysqld state=started'shell 在远程主机上运行命令,可以使用管道符或变量等复杂命令
shell 在远程主机上运行命令,可以使用管道符或变量等复杂命令示例
ansible all -m shell -a 'cat /etc/passwd | grep root'在Ansible中,-a 选项和 shell 模块都可以用于在目标主机上执行命令,但它们有一些区别。
-a 选项和 shell 模块都可以用于在目标主机上执行命令,但它们有一些区别。-a 选项:
-a选项用于在命令行中指定Ad-hoc命令,允许直接执行简单的命令。例如:ansible all -i inventory_file -a 'command_to_execute'这种方式适用于简单的任务,但对于复杂的任务,尤其是需要使用管道、重定向或其他高级Shell功能的任务,可能会变得笨拙和不方便。
shell 模块:
shell模块是一个Ansible模块,允许你在目标主机上运行复杂的Shell命令。使用这个模块,你可以在Playbook中以更结构化的方式定义命令。示例:--- - name: Example Playbook with shell module hosts: all tasks: - name: Run complex command with shell module shell: | echo "Hello, world!" mkdir /tmp/example touch /tmp/example/file.txt在这个示例中,
shell模块允许你通过|符号定义多行Shell命令。
总体而言,推荐使用 shell 模块,尤其是在编写更复杂的Playbooks时。它提供了更好的可读性和维护性,而 -a 选项更适合在命令行上执行简单的任务。
script 将本地服务复制到运程主机上运行
script 将本地服务复制到运程主机上运行示例
ansible test -m script -a 'sh/test.sh'yum 安装程序
yum 安装程序name # 包名
state
present|latest # 安装
absent # 卸载
示例
ansible all -m yum -a 'name=ntpd'
# 卸载
ansible all -m yum -a 'name=ntpd state=absent'setup 收集远程主机的信息,如操作系统版本,IP地址等
setup 收集远程主机的信息,如操作系统版本,IP地址等示例
ansible all -m setup 远程用户非root,使用sudo/su执行
ansible 1.9之前的操作
先调整一下/etc/ansible/hosts的配置
[node2]
192.168.1.2 ansible_sudo_pass='123456' # ansibble_sudo_pass是当连接的用户不是root时,要使用sudo命令的密码
[webserver]
192.168.1.3 ansible_ssh_user=admin ansible_ssh_pass='12345' # 默认remote_user=root,在ansible.cfg中可以修改
192.168.1.4在管理机上执行
ansible node2 -a "touch /data/file" --sudo # --sudo,以sudo到root的方式执行
ansible node2 -a "touch /data/file2" -u admin --sudo # -u 指定用户
ansilbe 1.9之后的版本使用become替代了sudo/su
修改配置文件/etc/ansible/ansible.cfg
...
[privilege_escalation]
become=True # 开启
#become_method=sudo # 方式 sudo su
#become_user=root # 切换到的用户,
#become_ask_pass=False # 是否询问密码,如果在hosts文件中配置了,可以配置为False
...修改/etc/ansible/hosts内容
[node1]
192.168.1.2 ansible_become_pass='12345' # sudo换成become
Ansible playbook
结构:
inventory # 应对要操作的主机
modules # 调用哪些模块做操作
ad hoc commadns # 在这些主机上执行哪些命令
playbooks
tasks # 任务,即调用模块完成的操作
variable # 变量
templates # 模板
handlers # 触发器,由某件事件触发执行的操作
roles # 角色
playbook示例
- hosts: 172.23.210.27,172.23.210.28 # 要执行的目标主机或主机组
remote_user: root # 远程用户
vars: # 定义变量
http_port: 8080 # 具体变量
tasks: # 定义一个任务
- name: create new file # 任务名称
file: name=/tmp/playtestfile.txt state=touch # 调用`file`模块,和创建文件操作
- name: install package # 任务名
yum: name=mysqld # 调用`yum`模块,并安装msyqld
- name: config mysqld
template: src=./my.cnf dest=/etc/my.cnf
notify: # 触发器,当条件触发后要做的操作,配合handlers使用
- restart mysqld # 引用的handlers的名字
- name: start mysqld
service: name=mysqld state=started
handlers: # notify定义的触发后要执行的操作
- name: restart mysqld # handlers的名称,跟notify引用的一样
service: name=mysqld state=restarted # 触发要执行的操作运行Ansible playbook ansible-playbook playbook.yaml
playbook变量的赋值与调用
- hosts: all
remote_user: root
vars:
dir_name: testname
tasks:
- name: create new dir
command: mkdir /tmp{{ dir_name }} # 调用变量需要用两个大括号{{}}将其括起来通过playbook 使用yum删除和安装软件
在 Ansible 中,你可以通过 playbook 使用 yum 模块来删除和安装软件。这对于在基于 Red Hat 的系统(如 CentOS, Fedora, RHEL 等)上管理软件包非常有用。下面是如何编写这样的 playbook 的示例。
安装软件包
要安装软件包,你可以使用 yum 模块并设置 state 参数为 present。这会确保指定的软件包已安装。
- name: Install a software package
hosts: all
become: yes # 使用超级用户权限
tasks:
- name: Install Apache
yum:
name: httpd # 软件包名
state: present删除软件包
要删除软件包,将 state 参数设置为 absent。这将确保指定的软件包未安装在系统上。
- name: Remove a software package
hosts: all
become: yes # 使用超级用户权限
tasks:
- name: Remove Apache
yum:
name: httpd # 软件包名
state: absent运行 playbook
保存你的 playbook 为一个文件(例如 manage_packages.yml),然后在包含你的远程主机的 Ansible inventory 的情况下运行它:
ansible-playbook manage_packages.yml主机信息可以写到/etc/ansible/hosts和用-i参数单独引用的区别
将主机信息写入 /etc/ansible/hosts 文件是一种常见的做法,这样你就无需在执行 Ansible 命令时显式指定 -i 参数。Ansible 在默认情况下会在 /etc/ansible/hosts 文件中查找主机清单,因此如果你的主机信息已经在该文件中,你可以直接运行 Ansible 命令而不需要额外的参数。
然而,有时候你可能希望在不同的场景中使用不同的主机清单,或者你可能希望将主机信息存储在不同的文件中以便于管理。在这种情况下,你可以使用 -i 参数来指定不同的主机清单文件。这使得你可以根据需要选择不同的主机清单,而不受默认清单文件的限制。
例如,你可能有一个用于生产环境的清单文件,另一个用于测试环境。在执行 Ansible 命令时,使用 -i 参数可以方便地切换到不同的清单文件,而不必每次都编辑默认的 /etc/ansible/hosts 文件。
-i 参数提供了更大的灵活性,允许你动态选择要使用的主机清单,而不仅限于默认的清单文件路径。
最后更新于