Ansible 常用模块使用

Ansible Common Modules Use

Posted by BlueFat on Saturday, June 9, 2018

安装

CentOS/RHEL

在 CentOS 、RHEL、Alibaba Cloud Linux 等使用 RPM 包管理器的发行版中,可以使用 如下命令安装 Ansible:

sudo yum install ansible 

Ubuntu

在 Ubuntu、Debian 等使用 DEB 包管理器的发行版中,可以使用如下命令安装 Ansible:

sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository --yes --update ppa:ansible/ansible sudo apt install ansible 

Python

除了使用系统自带的包管理器以外,也可以使用 Python 的包管理器 pip 来安装 Ansible。

python -m pip install --user ansible 
#或
pip install --user ansible 

相关文件

配置文件 /etc/ansible/ansible.cfg 主配置文件,配置ansible工作特性 /etc/ansible/hosts 主机清单 /etc/ansible/roles/ 存放角色的目录

程序

/usr/bin/ansible 主程序,临时命令执行工具 /usr/bin/ansible-doc 查看配置文档,模块功能查看工具 /usr/bin/ansible-galaxy 下载/上传优秀代码或Roles模块的官网平台 /usr/bin/ansible-playbook 定制自动化任务,编排剧本工具 /usr/bin/ansible- pull 远程执行命令的工具 /usr/bin/ansible-vault 文件加密工具 /usr/bin/ansible-console 基于Console界面与用户交互的执行工具

ansible 配置文件

Ansible 配置文件/etc/ansible/ansible.cfg (一般保持默认) [defaults]
#inventory = /etc/ansible/hosts # 主机列表配置文件
#library = /usr/share/my_modules/ # 库文件存放目录
#remote_tmp = $HOME/.ansible/tmp #临时py命令文件存放在远程主机目录
#forks = 5 # 默认并发数
#sudo_user = root # 默认sudo 用户
#ask_sudo_pass = True # 每次执行ansible命令是否询问ssh密码 
#ask_pass = True
#local_tmp= $HOME/.ansible/tmp # 本机的临时命令执行目录
#remote_port = 22
#host_key_checking = False # 检查对应服务器的host_key,建议取消注释 #log_path=/var/log/ansible.log #日志文件

Ansible系列命令

ansible-doc

ansible-doc: 显示模块帮助

 ansible-doc [options] [module...]
 -a 显示所有模块的文档
 -l, --list 列出可用模块
 -s, --snippet显示指定模块的playbook片段 

示例:
 ansible-doc –l 列出所有模块
 ansible-doc ping 查看指定模块帮助用法
 ansible-doc –s ping 查看指定模块帮助用法 

ansible-galaxy

下载相应的roles https://galaxy.ansible.com

列出所有已安装的galaxy
ansible-galaxy list
安装galaxy
ansible-galaxy install geerlingguy.redis
删除galaxy
ansible-galaxy remove geerlingguy.redis

Ansible-vault

加密解密yml文件

ansible-vault [create|decrypt|edit|encrypt|rekey|view] 
ansible-vault encrypt hello.yml #加密
ansible-vault decrypt hello.yml #解密
ansible-vault view hello.yml #查看
ansible-vault edit hello.yml #编辑加密文件 
ansible-vault rekey hello.yml #修改口令 
ansible-vault create new.yml #创建新文件

ansible-console

ansible-console:2.0+新增,可交互执行命令,支持tab

root@test (2)[f:10] $
执行用户@当前操作的主机组 (当前组的主机数量)[f:并发数]$
设置并发数: forks n 
切换组: cd 主机组  
列出当前组主机列表: list
列出所有的内置命令: ?或help
root@all (2)[f:5]$ list
root@all (2)[f:5]$ cd appsrvs
root@appsrvs (2)[f:5]$ list
root@appsrvs (2)[f:5]$ yum name=httpd state=present root@appsrvs (2)[f:5]$ service name=httpd state=started

ansible

ansible: 通过ssh实现配置管理、应用部署、任务执行等功能,建议配置密钥管理节点

ansible <host-pattern> [-m module_name] [-a args] --version 显示版本
-m module 指定模块,默认为command
-v 详细过程 –vv -vvv更详细
--list-hosts 显示主机列表,可简写 --list
-k, --ask-pass 提示输入ssh连接密码,默认Key验证 -K, --ask-become-pass 提示输入sudo时的口令
-C, --check 检查,并不执行
-T, --timeout=TIMEOUT 执行命令的超时时间,默认10s -u, --user=REMOTE_USER 执行远程执行的用户
-b, --become 代替旧版的sudo 切换

ansible 主机匹配

All: 表示所有Inventory中的所有主机 
ansible all –m ping 

*: 通配符
 ansible "*" -m ping
 ansible 192.168.1.* -m ping 

或关系
 ansible "websrvs:appsrvs" -m ping
 ansible "192.168.1.10:192.168.1.20" -m ping 
 
逻辑与
在websrvs组并且在dbsrvs组中的主机
ansible "websrvs:&dbsrvs" –m ping 

逻辑非
在websrvs组,但不在dbsrvs组中的主机 注意:此处为单引号
ansible 'websrvs:!dbsrvs' –m ping 

综合逻辑
ansible 'websrvs:dbsrvs:&appsrvs:!ftpsrvs' –m ping

正则表达式
ansible "websrvs:&dbsrvs" –m ping
ansible "~(web|db).*\.magedu\.com" –m ping

常用模块

Command

https://docs.ansible.com/ansible/2.9/modules/command_module.html Command:在远程主机执行命令,默认模块,可忽略-m选项

ansible srvs -m command -a 'service vsftpd start'
ansible srvs -m command -a 'echo magedu |passwd --stdin wang' #不成功

此命令不支持 $VARNAME < > | ; & 等,用shell模块实现

Shell

https://docs.ansible.com/ansible/2.9/modules/shell_module.html Shell:和command相似,用shell执行命令

ansible srv -m shell -a 'echo magedu |passwd –stdin wang'

复杂命令调用失败解决:使用Script

cat /tmp/stanley.md | awk -F '|'  '{print $1, $2}' &> /tmp/example.txt

Script

https://docs.ansible.com/ansible/2.9/modules/script_module.html Script:运行脚本

ansible websrvs -m script -a f1.sh

Copy

https://docs.ansible.com/ansible/2.9/modules/copy_module.html Copy:从服务器复制文件到客户端

ansible srv -m copy -a “src=/root/f1.sh dest=/tmp/f2.sh
owner=wang mode=600 backup=yes”

如目标存在,默认覆盖,此处指定先备份

ansible srv -m copy -a “content=‘test content\ndest=/tmp/f1.txt” 

利用内容,直接生成目标文件

Fetch

https://docs.ansible.com/ansible/2.9/modules/fetch_module.html Fetch: 从客户端取文件至服务器端,目录则会压缩

ansible srv -m fetch -a ‘src=/root/a.sh dest=/data/scripts’ 

File

https://docs.ansible.com/ansible/2.9/modules/file_module.html File: 设置文件属性

ansible srv -m file -a "path=/root/a.sh owner=wang mode=755“ 
ansible web -m file -a ‘src=/app/testfile dest=/app/testfile-link state=link’ 

lineinfile

替换行 https://docs.ansible.com/ansible/2.9/modules/lineinfile_module.html

- name: Disable SELinux
  lineinfile:
    path: /etc/selinux/config
    regexp: "^SELINUX="
    line: "SELINUX=disabled"

replace

替换内容 https://docs.ansible.com/ansible/2.9/modules/replace_module.html

- name: Cancel ntp server
  replace:
    path: /etc/chrony.conf
    regexp: "^server"
    replace: "#server"

Hostname

https://docs.ansible.com/ansible/2.9/modules/hostname_module.html Hostname: 管理主机名

ansible node1 -m hostname -a “name=websrv”

Cron

https://docs.ansible.com/ansible/2.9/modules/cron_module.html Cron:计划任务 支持时间:minute,hour,day,month,weekday 创建任务

ansible srv -m cron -a “minute=*/5 job=‘/usr/sbin/ntpdate 172.16.0.1 &>/dev/null’ name=Synctime” 

删除任务

ansible srv -m cron -a ‘state=absent name=Synctime

YUM/DNF/APT

dnf https://docs.ansible.com/ansible/2.9/modules/dnf_module.html

ansible srv -m dnf -a ‘name=httpd state=latest’ #安装
ansible srv -m dnf -a ‘name=httpd state=absent’ #删除

yum https://docs.ansible.com/ansible/2.9/modules/yum_module.html

ansible srv -m yum -a ‘name=httpd state=latest’ #安装
ansible srv -m yum -a ‘name=httpd state=absent’ #删除

apt https://docs.ansible.com/ansible/2.9/modules/apt_module.html

ansible srv -m apt -a ‘name=httpd state=latest update_cache=yes’ #安装
ansible srv -m apt -a ‘name=httpd state=absent’ #删除

Service

https://docs.ansible.com/ansible/2.9/modules/service_module.html Service: 管理服务

ansible srv -m service -a 'name=httpd state=stopped' 
ansible srv -m service -a 'name=httpd state=started'
ansible srv –m service –a ‘name=httpd state=reloaded’
ansible srv -m service -a 'name=httpd state=restarted'

User

https://docs.ansible.com/ansible/2.9/modules/user_module.html User: 管理用户

ansible srv -m user -a 'name=user1 comment=“test user” uid=2048 home=/app/user1 group=root‘
ansible srv -m user -a 'name=sysuser1 system=yes home=/app/sysuser1’
ansible srv -m user -a ‘name=user1 state=absent remove=yes‘ #删除用户及家目录等数据

https://docs.ansible.com/ansible/2.9/modules/group_module.html

Group

Group: 管理组

ansible srv -m group -a "name=testgroup system=yes“ 
ansible srv -m group -a "name=testgroup state=absent"

playbook

Playbook核心元素

Hosts 执行的远程主机列表

  • Tasks 任务集
  • Varniables 内置变量或自定义变量在playbook中调用
  • Templates 模板,可替换模板文件中的变量并实现一些简单逻辑的文件
  • Handlers 和notity结合使用,由特定条件触发的操作,满足条件方才执行,否 则不执行
  • tags 标签 指定某条任务执行,用于选择运行playbook中的部分代码。
ansible-playbook –t tagsname useradd.yml 
---
- hosts: sz
  remote_user: username
  become: yes
  tasks:
    - name: install nginx
      yum: name=nginx