文章

ansible常用操作

ansible常用操作

http://sapser.github.io/ansible/2014/07/21/ansible-playbook 教程

https://hoxis.github.io/ansible-facts-cache.html ansible 配置缓存

https://www.cnblogs.com/kevingrace/p/11647338.html 日常运维

ansible 常用参数

  • -u:远程用户(默认 root)
  • -i:指定主机清单(默认 /etc/ansible/hosts
  • -m:指定模块名称(如 command / shell / copy 等)
  • -a:指定模块参数(即命令参数)
  • -k:提示输入远程主机密码
  • -b / --become:提权执行(旧版本也常见 -s
  • -U:become 到哪个用户(默认为 root)
  • -K:提示输入 sudo/become 密码(非 NOPASSWD 时)
  • -C:check 模式(仅测试,不实际执行)
  • -c:连接类型(默认 smart)
  • -f:并发 fork 数(默认 5)
  • -I:对 <host_pattern> 匹配结果再过滤一次
  • --list-hosts:仅打印将执行的主机列表
  • -M:模块路径(默认 /usr/share/ansible
  • -o:压缩输出(摘要输出)
  • --private-key:私钥路径
  • -T:SSH 连接超时(默认 10s)
  • -t:日志输出目录(日志文件名以主机名命名)
  • -v:verbose 输出(可叠加 -vvv

主机清单配置

创建主机清单文件 server.ini

vim server.ini

文件内容示例:

[server] 192.168.255.1[29:36] # 表示 192.168.255.129 到 192.168.255.136 [server:vars] host=hostname

复制文件到远程服务器

使用 copy 模块将本地文件复制到远程服务器:

ansible -i server.ini server -uroot -b -k -m copy -a "src=hosts.sh dest=/root/"

参数说明:

  • -i server.ini:指定主机清单文件
  • -uroot:使用 root 用户连接
  • -b:使用 sudo 提权
  • -k:提示输入 SSH 密码
  • -m copy:使用 copy 模块
  • -a:模块参数

批量执行命令并验证

执行远程脚本:

ansible -i server.ini server -uroot -b -k -m shell -a "sh /root/hosts.sh"

验证执行结果(使用变量):

ansible -i server.ini server -uroot -b -k -m shell -a "{{host}}"

相同 IP 执行不同命令

当需要对同一 IP 地址执行不同命令时,需要在主机清单中配置别名,并为每个别名设置不同的变量:

# 在主机清单中配置别名和变量 [vms_iops] vms01 ansible_ssh_host=172.19.8.65 vms=instance-0000008d vms02 ansible_ssh_host=172.19.8.78 vms=instance-0000008e vms03 ansible_ssh_host=172.19.8.34 vms=instance-000002e9 vms04 ansible_ssh_host=172.19.8.27 vms=instance-000002d4

执行命令(使用变量):

ansible -i bjdz-openstack.ini vms_iops -uinadm -b -k -m shell -a "virsh blkdeviotune {{vms|quote}} vda --read-iops-sec 1000 --write-iops-sec 1000 --read-bytes-sec 52428800 --write-bytes-sec 52428800 --config --live"

常用模块示例

command 模块

执行远程命令(不支持管道和重定向):

ansible testservers -m command -a 'uname -n'

script 模块

在远程主机执行主控端的 shell/python 脚本(使用相对路径):

ansible testservers -m script -a '/etc/ansible/test.sh'

shell 模块

执行远程主机的 shell/python 脚本(支持管道和重定向):

ansible testservers -m shell -a 'bash /root/test.sh'

raw 模块

类似于 command 模块,但支持管道传递:

ansible testservers -m raw -a "ifconfig eth0 |sed -n 2p |awk '{print \$2}' |awk -F: '{print \$2}'"

copy 模块示例

复制文件并设置权限:

ansible -i bjdz-openstack.ini nvme -uinadm -b -k -m copy -a 'src=/opt/wangyf/add_pci_nvme.xml dest=/tmp/ owner=root group=root mode=755 backup=yes'

echo修改远程文件

--- - name: test hosts: controller gather_facts: false vars: hostname: jeff tasks: - name: test echo file ansible.builtin.shell: | cat >> /root/host.txt <<'EOF' line one line two line {{ hostname }} line three EOF

获取远端主机名

--- - name: test hosts: controller gather_facts: false tasks: - name: register test ansible.builtin.shell: hostname register: info - name: display info ansible.builtin.debug: msg: "Hostname is {{ info.stdout }}"

获取IP地址及主机名

https://stackoverflow.com/questions/39819378/ansible-get-current-target-hosts-ip-address Ansible主机相关信息 中的进行修改

--- - name: show host facts hosts: controller gather_facts: true tasks: - name: IPv4 address ansible.builtin.debug: msg: "IPv4 Address is {{ hostvars[inventory_hostname]['ansible_default_ipv4']['address'] }}" - name: Hostname ansible.builtin.debug: msg: "Hostname is {{ hostvars[inventory_hostname]['ansible_hostname'] }}" - name: FQDN ansible.builtin.debug: msg: "FQDN is {{ hostvars[inventory_hostname]['ansible_fqdn'] }}"

导入变量

# mail.yml --- - name: import vars hosts: controller gather_facts: true vars_files: - vars/vars.yml tasks: - name: show vars ansible.builtin.debug: var: vars # vars/vars.yml --- vars: "{{ hostvars[inventory_hostname]['ansible_default_ipv4']['address'] }}"

禁用 Ansible 的 SSH 检查

编辑 Ansible 配置文件,禁用 SSH 主机密钥检查:

vim /etc/ansible/ansible.cfg

取消注释以下行,可以免除第一次 SSH 连接时需要输入 yes:

# uncomment this to disable SSH key host checking host_key_checking = False
本文由作者按照 CC BY 4.0 进行授权