ansible学习笔记
ansible的安装
注: ansible的安装需要使用到epl源,我们先使用yum安装epel-release,然后在安装ansible
[root@Ansible ~]# yum install -y epel-release // 安装rpel源 ..... ---> 软件包 epel-release.noarch.0.7-11 将被 安装 [root@Ansible ~]# yum install -y ansible //安装ansible 注:ansible是Python开发的,需要依赖于Python,所有在安装ansible的时候机器上没有安装Python,也会一起安装上的,一般都安装好了。
查看版本信息
[root@Ansible ~]# ansible --version // ansible版本信息 ansible 2.9.10 [root@Ansible ~]# python --version // python 版本信息 Python 2.7.5
配置文件说明1
配置文件存在的不同的位置,但是只有一个可用
在下列列表中,anseble从上往下依次检查,检查到那个可用就用那个。
1、ANSIBLE_CONFIG环境变量,可以定义配置文件的位置
2、./ansible.cfg存在于当前工作目录
3、~/.ansible.cfg存在于当前用户的家目录
4、/etc/ansible/ansible.cfg 默认目录
配置文件说明2
1、/etc/ansible/ansible.cfg 主配置文件
2、/etc/ansible/hosts 主机清单文件
3、/etc/ansible/roles/ 存放角色的目录
主配置文件说明名
[root@vip ansible]# vim /etc/ansible/ansible.cfg //配置文件位置(一般保持默认)
[defaults]
#inventory = /etc/ansible/hosts #主机列表配置文件
#library = /usr/share/my_modules/ #库存文件存放
#remote_tmp = ~/.ansible/tmp #临时py命令文件存放在远程主机目录
#local_tmp = ~/.ansible/tmp #本机的临时命令执行目录
#forks = 5 #默认并发数
#sudo_user = root #默认sudo用户
#ask_sudo_pass = True #每次执行ansible命令会否询问ssh密码
#ask_pass = True #密码
#remote_port = 22 #端口
#module_lang = C #语言
#host_key_checking = False #检查对用服务器的host_key,建议取消注释
#log_path = /var/log/ansible.log # 日志文件,建议取消注释
主机清单文件定义方法
主机清单存储位置:
[root@vip ansible]# more /etc/ansible/hosts
方法一:
直接在主机清单文件中写入主机ip地址或主机名(需要能够解析)node #或 192.168200.130
方法二:
在主机清单文件中添加主机分组,然后把主机ip或主机名写入分组内即可。
[web] master #或 [web] 192.168.204.110
实现主机间免密登录
[root@vip ansible]# ssh-keygen -t rsa //在ansible主机生成密钥对 [root@vip ansible]# ssh-copy-id 192.168.204.110 //复制密钥对(公钥)到被控制端
ansible命令基础
#语法: ansible <host-pattern> [options] – host-pattern 主机或定义的分组 -m 使用模块,默认 command 模块 -a or --args 模块参数 -k 使用交亏式登彔密码 -v 详绅信息,-vvvv 开启 debug 模式 [root@vip ~]# ansible all --list //列出要执行的主机 hosts (2): 192.168.200.131 192.168.200.130 ansible的host-pattern 匹配主机的列表 all:表示所有主机 “*”通配符 ansible “*” -m ping anseble 192.158.204.* -m ping 或关系 ansible “web1:web2” -m ping [root@vip ~]# ansible "web1:web2" -m ping //2个组内只要不是重复,都执行 192.168.200.131 | SUCCESS => { "changed": false, "ping": "pong" } 192.168.200.130 | SUCCESS => { "changed": false, "ping": "pong" } 逻辑与 [root@vip ~]# ansible "web1:&web2" -m ping //在web1组并且在web2组中的主机 192.168.204.120 | SUCCESS => { "changed": false, "ping": "pong" } 逻辑非 [root@vip ~]# ansible 'web1:!web2' -m ping //在web1组,但不在web2组 注意:此处为单引号 192.168.204.110 | SUCCESS => { "changed": false, "ping": "pong" } ```
ansible命令执行过程
1、加载自己的配置文件默认/etc/ansible/ansible.cfg
2、加载自己对用的模式文件,如command
3、通过ansible将模块或命令生成对应的临时py文件,并将该文件传输至远程服务器的对用执行用户的$HOME/.ansible/tmp/ansible-tpm-数字/xxx.py文件
4、给文件+x执行权限
5、执行并返回结果
6、删除临时Py文件,sleep 0退出
ansible-doc 帮助模块
[root@vip ~]# ansible-doc 模块名 -a:显示所有模块的文档 -l:列出可用模块 -s:显示指定模块的playbook前段 [root@vip ~]# ansible-doc ping //查看指定模块帮助用法 [root@vip ~]# ansible-doc -l //列出所有模块 [root@vip ~]# ansible-doc -s ping //查看指定模块帮助用法
command 模块使用
功能: 在远程主机执行命令,此模块为默认模块,可忽略-m选项
注意:此命令无法支持"<",">","|",";","&"等符号
参数 | 选项/默认值 | 释义 |
---|---|---|
chdir | 在执行命令前,进入到指定目录中 | |
creates | 判断指定文件是否存在,如果存在,不执行后面的操作 | |
removes | 判断指定文件是否存在,如果存在,执行后面的操作 | |
free_form | 必须要输入一个合理的命令 |
实例: 查看操作系统的版本信息
[root@Ansible ansible]# ansible all -m command -a 'cat /etc/centos-release' 192.168.200.130| SUCCESS | rc=0 >> CentOS Linux release 7.7.1908 (Core) chdir:执行命令之前,进入到指定目录 [root@Ansible ansible]# ansible all -m command -a 'chdir=/etc cat centos-release' 192.168.200.130 | SUCCESS | rc=0 >> CentOS Linux release 7.7.1908 (Core) creates:判断指定文件是否存在,如果存在,不执行后面的操作 [root@Ansible .ssh]# ansible all -m command -a 'chdir=/etc creates=/data/test.txt cat centos-release' 192.168.31.130 | SUCCESS | rc=0 >> CentOS Linux release 7.7.1908 (Core) 192.168.31.131 | SUCCESS | rc=0 >> CentOS Linux release 7.7.1908 (Core) 注:我去131主机把这个文件创建出来,看看是什么效果? [root@Ansible .ssh]# ansible all -m command -a 'chdir=/etc creates=/data/test.txt cat centos-release' 192.168.31.130| SUCCESS | rc=0 >> CentOS Linux release 7.7.1908 (Core) ----------------------------------------很明显131没有执行----------------------- 192.168.31.131 | SUCCESS | rc=0 >> skipped, since /data/test.txt exists removes:判断指定文件是否存在,如果存在,执行后面的操作 [root@Ansible .ssh]# ansible all -m command -a 'chdir=/etc removes=/data/test.txt cat centos-release' -------------------------------------存在的执行了---------------------- 192.168.31.130 | SUCCESS | rc=0 >> CentOS Linux release 7.7.1908 (Core) 192.168.31.131 | SUCCESS | rc=0 >> skipped, since /data/test.txt does not exist
shell模块
功能: 在远程主机执行命令,与command模块使用方法类似,只不过支持管道,重定向,变量符等等。
注:shell模块在远程主机中执行命令时,是调用远程主机的/bin/bash解释器来执行命令,由此可以看出shell模块也是万能模块。
参数 | 选项/默认值 | 释义 |
---|---|---|
chdir | 在执行命令前,进入到指定目录中 | |
creates | 判断指定文件是否存在,如果存在,不执行后面的操作 | |
removes | 判断指定文件是否存在,如果存在,执行后面的操作 | |
free_form | 必须要输入一个合理的命令 |
实例1:查看node节点22端口是否存在
[root@Ansible ansible]# ansible node -m shell -a 'ss-tunlp | grep 22' 192.168.200.130 | CHANGED | rc=0 >> tcp LISTEN 0 128 *:22 *:* users: (("sshd",pid=960,fd=3)) tcp LISTEN 0 128 [::]:22 [::]:* users: (("sshd",pid=960,fd=4))
实例2:进入opt目判断test.txt文件是否存在,不存在创建YHB.txt文件
#进入/opt/目录判断test.txt文件是否存在,不存在创建YHB.txt文件 注:[WARNING]警告信息不是报错,是说有更合适的file模块来创建文件(大概意思) [root@Ansible ansible]# ansible node -m shell -a "chdir=/opt/ creates=test.txt touch YHB.txt" [WARNING]: Consider using the file module with state=touch rather than running 'touch'. If you need to use command because file is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message. 192.168.200.130 | CHANGED | rc=0 >> #使用shell模块查看远程主机/opt/目录下创建的YHB.txt文件是否存在 [root@Ansible ansible]# ansible node -m shell -a "ls /opt/" 192.168.200.130 | CHANGED | rc=0 >> backup YHB.txt
copy模块
功能:实现主控端向目标主机copy文件
注:copy模块在复制数据时,如果数据为软链接文件,会将链接指定源文件进行复制 修改权限时候 需要加0 例如:chmod 0644 0755
参数 | 选项/默认值 | 释义 |
---|---|---|
src | 指定将本地管理主机的数据信息进行远程复制, (必选项) | |
backup | no* yes | 默认数据复制到远程主机,会覆盖原有文件(yes 将源文件进行备份) |
content | 在文件中添加信息 | |
dest(required) | 将数据复制到远程节点的路径信息, (必选项) | |
group | 文件数据复制到远程主机,设置文件属组用户信息 | |
mode | 文件数据复制到远程主机,设置数据的权限 eg 0644 0755 | |
owner | 文件数据复制到远程主机,设置文件属主用户信息 | |
remote_src | no* yes | 如果设置为yes,表示将远程主机上的数据进行移动操作如果设置为no, 表示将管理主机上的数据进行 |
实例1:copy文件到远程主机
src:指定管理机的文件路径 dest:指定目标主机的文件路径 [root@Ansible ~]# ansible node -m copy -a "src=/root/tomcat8080.tar.gz dest=/opt/" 192.168.200.130 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "checksum": "c5142ef2cb507f97de2ca1f4218651a829b71ebd", "dest": "/opt/tomcat8080.tar.gz", "gid": 0, "group": "root", "md5sum": "6568caf3d8f7d3184f81e3dec8e183e0", "mode": "0644", "owner": "root", "secontext": "system_u:object_r:usr_t:s0", "size": 19309435, "src": "/root/.ansible/tmp/ansible-tmp-1599379180.75-2001-87241703197109/source", "state": "file", "uid": 0 }
实例2:copy文件到远程主机并备份文件
backup:在文件覆盖之前,把要替换的文件备份。如果管理机同步的文件和被管理主机的文件内容一致,侧不做任何操作 [root@Ansible ~]# ansible node -m copy -a "src=/etc/yum.repos.d/epel.repo dest=/etc/yum.repos.d/epel.repo backup=yes" 192.168.200.130 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "checksum": "6acc53a98eddeaef23b9d47b641030212331b257", "dest": "/etc/yum.repos.d/epel.repo", "gid": 0, "group": "root", "mode": "0644", "owner": "root", "path": "/etc/yum.repos.d/epel.repo", "secontext": "system_u:object_r:system_conf_t:s0", "size": 951, "state": "file", "uid": 0 }
实例3:copy文件并修改文件所有者、所属组和权限
mode:文件的权限,如:chmod +x group:文件所示组 owner:文件所有者 [root@Ansible ~]# ansible node -m copy -a "src=/etc/yum.repos.d/epel.repo dest=/etc/yum.repos.d/epel.repo group=rsync mode=0755 owner=admin " 192.168.200.130 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "checksum": "6acc53a98eddeaef23b9d47b641030212331b257", "dest": "/etc/yum.repos.d/epel.repo", "gid": 1002, "group": "rsync", "mode": "0755", "owner": "admin", "path": "/etc/yum.repos.d/epel.repo", "secontext": "system_u:object_r:system_conf_t:s0", "size": 951, "state": "file", "uid": 1000 }
file模块
功能:实现创建/删除文件信息 对文件权限进行修改
参数 | 选项/默认值 | 释义 |
---|---|---|
dest/path/name(required) | 将数据复制到远程节点的路径信息 | |
group | 文件数据复制到远程主机,设置文件属组用户信息 | |
mode | 文件数据复制到远程主机,设置数据的权限 eg 0644 0755 | |
owner | 文件数据复制到远程主机,设置文件属主用户信息 | |
src | 指定将本地管理主机的什么数据信息进行远程复制 | |
state | absent | 将数据进行删除 |
= | directory | 创建一个空目录信息 |
= | file | 查看指定目录信息是否存在 |
= | touch | 创建一个空文件信息 |
= | hard/link | 创建链接文件 |
实例1:创建问价、目录和删除文件和目录
path:必选项,指定文件或目录路径 statr: =absent :将数据进行删除 =directory :创建一个空目录信息 =file :查看指定目录信息是否存在 =touch :创建一个空文件信息 =hard :创建硬链接 =link :创建软链接 =touch :创建一个空文件信息 [root@Ansible ~]# ansible node -m file -a "path=/root/test state=touch" 192.168.200.130 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "dest": "/root/test", "gid": 0, "group": "root", "mode": "0644", "owner": "root", "secontext": "unconfined_u:object_r:admin_home_t:s0", "size": 0, "state": "file", "uid": 0 } directory :创建一个空目录信息 [root@Ansible ~]# ansible node -m file -a 'path=/root/admin/ state=directory' 192.168.200.130 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "gid": 0, "group": "root", "mode": "0755", "owner": "root", "path": "/root/admin/", "secontext": "unconfined_u:object_r:admin_home_t:s0", "size": 6, "state": "directory", "uid": 0 } absent :将数据进行删除 [root@Ansible ~]# ansible node -m file -a 'path=/root/admin/ state=absent' 192.168.200.130 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "path": "/root/admin/", "state": "absent" } hard :创建硬链接 [root@Ansible ~]# ansible node -m file -a 'path=/opt/test.hard state=hard src=/root/test' 192.168.200.130 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "dest": "/opt/test.hard", "gid": 0, "group": "root", "mode": "0644", "owner": "root", "secontext": "unconfined_u:object_r:admin_home_t:s0", "size": 0, "src": "/root/test", "state": "hard", "uid": 0 } link :创建软链接 [root@Ansible ~]# ansible node -m file -a 'path=/opt/test.linl src=/root/test state=link' 192.168.200.130 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "dest": "/opt/test.linl", "gid": 0, "group": "root", "mode": "0777", "owner": "root", "secontext": "unconfined_u:object_r:usr_t:s0", "size": 10, "src": "/root/test", "state": "link", "uid": 0 } #查看是否创建软、硬连接 [root@node opt]# ll 总用量 18860 drwx------. 2 rsync rsync 110 8月 29 17:17 backup -rw-r--r--. 2 root root 0 9月 6 17:10 test.hard lrwxrwxrwx. 1 root root 10 9月 6 18:43 test.linl -> /root/test -rw-r--r--. 1 root root 19309435 9月 6 15:59 tomcat8080.tar.gz -rw-r--r--. 1 root root 0 9月 6 13:31 YHB.txt [root@node opt]```
实例2:创建文件时指定文件属主和属组和权限
owner:指定文件属主 group:指定文件数组 mode :指定文件权限 [root@Ansible ~]# ansible node -m file -a 'path=/root/test.txt state=touch owner=admin group=rsync mode=0600' 192.168.200.130 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "dest": "/root/test.txt", "gid": 1002, "group": "rsync", "mode": "0600", "owner": "admin", "secontext": "unconfined_u:object_r:admin_home_t:s0", "size": 0, "state": "file", "uid": 1000 }
实例3:修改已经存在的文件或目录的属组、属主、和权限
注:如果文件或目录已存在,只修改,如果不存则创建文件或目录并指定文件的属主、属组、和权限
[root@Ansible ~]# ansible node -m file -a 'path=/root/test/ state=directory owner=admin group=admin mode=0755' 192.168.200.130 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "gid": 1000, "group": "admin", "mode": "0755", "owner": "admin", "path": "/root/test/", "secontext": "unconfined_u:object_r:admin_home_t:s0", "size": 6, "state": "directory", "uid": 1000 }
实例4:递归修改属主和属组
recurse:递归 相当于 -R [root@Ansible ~]# ansible node -m file -a 'path=/root/test state=directory owner=rsync group=rsync recurse=yes' 192.168.200.130 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "gid": 1002, "group": "rsync", "mode": "0755", "owner": "rsync", "path": "/root/test", "secontext": "unconfined_u:object_r:admin_home_t:s0", "size": 22, "state": "directory", "uid": 1002 } #查看test目录下的文件是不是一起改了属性 [root@node ~]# ls -lh /root/ 总用量 20M -rw-r--r--. 1 root root 20M 8月 16 20:50 production.rar drwxr-xr-x. 2 rsync rsync 22 9月 6 19:01 test -rw-------. 1 admin rsync 0 9月 6 18:51 test.txt [root@node ~]# ls -lh /root/test 总用量 0 -rw-r--r--. 1 rsync rsync 0 9月 6 19:01 test.txt [root@node ~]#