ansible <host-pattern> [options]
options:
-a MODULE_ARGS, --args=MODULE_ARGS #模块的参数
-C, --check # 检查
-f FORKS, --forks=FORKS #用来做高并发的
--list-hosts #列出主机列表
-m MODULE_NAME #模块名称
--syntax-check # 语法检查
-k 输入密码
rpm -ql ansible
/etc/ansible
/etc/ansible/ansible.cfg
/etc/ansible/hosts
/etc/ansible/roles
# This is the default ansible 'hosts' file. # # It should live in /etc/ansible/hosts # # - Comments begin with the '#' character # 用#来表示注释 # - Blank lines are ignored # 空白行被忽略 # - Groups of hosts are delimited by [header] elements # 主机组 需要在【】下面 # - You can enter hostnames or ip addresses #可以写主机名或者ip地址 # - A hostname/ip can be a member of multiple groups # 一台主机可以在多个组里面 www[001:006].example.com #表示从www001到www006的机器
ansible-doc [-l|-F|-s] [options] [-t <plugin type> ] [plugin]
-j # 以json的方式返回ansible的所有模块
-l, --list # 列出所有的ansible的模块
-s # 以片段式显示ansible的帮助信息
ansible web -a 'ls /'#第一个命令 ansible web -a 'pwd' ansible web -a 'chdir=/tmp pwd' # 切换目录执行命令,使用场景是编译安装时使用 ansible web -a 'creates=/tmp pwd' # 用来判断/tmp目录是否存在,存在就不执行后面的操作 ansible web -a 'creates=/data pwd' # 因为data不存在,所有才会执行pwd命令 ansible web -a 'removes=/tmp pwd' # 用来判断tmp目录是否存在,存在就执行后面的操作 ansible web -a 'removes=/data pwd' # 因为data不存在,所以不会执行pwd命令
tail -1 /etc/passwd tail -1 /etc/shadow id alex echo '123' | passwd --stdin alex #设置密码
ansible web -m shell -a 'echo "123" | passwd --stdin alex' # 批量创建密码 ansible 192.168.107.131 -m shell -a 'bash a.sh' # 执行远程文件方式一 ansible 192.168.107.131 -m shell -a '/root/a.sh' # 执行远程文件方式二,文件必须有执行权限 ansible 192.168.107.131 -m shell -a '/root/a.py' # 执行远端的Python脚本
ansible web -m script -a '/root/m.sh' # 执行本地的文件,执行管控机上的文件 ansible web -m script -a 'removes=/root/m.sh /root/m.sh' # 用来判断被管控机上是不是存在文件,如果存在,存在就执行,不存在就不执行 ansible web -m script -a 'creates=/root/a.sh /root/m.sh' # 用来判断被管控机上是不是存在文件,如果存在,就不执行
2. 文件相关模块
copy模块参数:backup、src、dest、group、mode、owner、content
backup 备份,以时间戳结尾 dest 目的地址 group 文件的属组 mode 文件的权限 r 4 w 2 x 1 owner 文件的属主 src 源文件 # 通过md5码来判断是否需要复制 ansible db -m copy -a 'src=/root/m.sh dest=/tmp/a.sh' # 复制本地文件的到远程主机 ansible db -m copy -a 'src=/root/m.sh dest=/tmp/a.sh mode=755' # 修改文件的权限 ansible web -m copy -a 'src=/root/m.sh dest=/tmp/a.sh mode=755 owner=alex' 修改文件的属主 ansible web -m copy -a 'src=/etc/init.d dest=/tmp/ mode=755 owner=alex' # 复制本地目录到远程主机,如果改变文件的属性,则文件夹内的文件也会被改变 ansible web -m copy -a 'src=/etc/init.d/ dest=/tmp/ mode=755 owner=alex' # 复制本地目录内的所有文件到远程主机 ansible web -m copy -a "content='大弦嘈嘈如急雨,小弦切切如私语\n' dest=/tmp/b.txt" # 直接将文本内容注入到远程主机的文件中
ansible db -m file -a 'path=/lzmly2 state=directory' #在远程机器上创建文件夹 ansible db -m file -a 'path=/root/q.txt state=touch' #用来在远程机器上创建文件 ansible db -m file -a 'path=/tmp/f src=/etc/fstab state=link' #创建软连接src是源地址,path是目标地址 ansible db -m file -a 'path=/tmp/f state=absent' #用来删除文件或者文件夹
inode 硬盘的地址 id 获取到的是内存的地址 ln -s a.py b.py 创建软连接 ln a.py c.py 创建硬链接 当 源文件变化时,软连接和硬链接文件都会跟着变化
dest 目的地址 src 源地址 ansible web -m fetch -a 'src=/var/log/cron dest=/tmp' # 下载被控节点的文件,会自动为每台机器创建一个文件夹,并保留原来的目录结构
参数: disablerepo #禁用源 enablerepo #启用源 name #包名 state install (`present' or `installed', `latest'), or remove (`absent' or `removed') 示例: ansible web -m yum -a 'name=wget' # 安装wget ansible web -m yum -a 'name=python2-pip' # 安装python2-pip ansible web -m yum -a 'name=wget state=absent' # 卸载软件包 ansible web -m yum -a 'name="@Development Tools"' # 安装包组
rpm 和 yum 的区别 rpm:redhat package manager yum可以解决依赖关系 yum 源配置 [epel] name=Extra Packages for Enterprise Linux 7 - $basearch #名字 baseurl=http://mirrors.aliyun.com/epel/7/$basearch #rpm源的地址,可以写http,https,ftp,Samba,file: failovermethod=priority enabled=1 # 是否开启,1代表开启,0表示关闭 gpgcheck=0 #是否校验签名,1代表校验,0表示校验 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7 yum 安装包组 yum grouplist # 查看包组信息 yum groupinstall # 安装包组
ansible web -m pip -a 'name=flask' # 在被管控机上安装flask模块
pip install 安装包 pip freeze > a.txt 将python的环境打包到文件中 pip install -r a.txt 安装文件中的包 pip list 查看所有的已安装成功的包
ansible web -m service -a 'name=nginx state=started' # 启动nginx ansible web -m service -a 'name=nginx state=stopped' # 关闭nginx
ps -ef|grep nginx #查看进程 ss -tnlp # 查看端口信息 systemctl start nginx # centos7 service nginx start # centos6 systemctl enabled nginx # centos7 开机自启动 chkconfig nginx on # centos6开机自启动
4.计划任务模块cron
参数: day 天 disabled 禁用 hour 小时 job 任务 minute 分钟 month 月 name 任务名字 weekday 周 示例: ansible db -m cron -a 'minute=26 job="touch /tmp/xzmly.txt" name=touchfile' # 新建一个计划任务 ansible db -m cron -a 'name=touchfile state=absent' # 删除一个计划任务 ansible db -m cron -a 'minute=26 job="touch /tmp/xzmly.txt" name=touchfile disabled=yes' # 禁用计划任务,以#表示禁用
* * * * * job 分 时 日 月 周 任务 0 */2 * * * job 每隔两个小时 0 12,13 * * * job 12点和13点 0 12-17 * * * job 12点到17点 0 12-17/2 * * 1,3,6,0 周1,周3,周6,周7 12点到17点每隔两个小时 crontab -e # 编辑计划任务 crontab -l # 查看计划任务 crontab -r # 删除计划任务
参数: group 组 groups 附加组 home 家目录 name 用户名 password 密码 remove ? shell 用户登录后使用的shell system 创建一个系统用户 uid 用来指定用户的id state 状态 示例: ansible db -m user -a 'name=wulaoshi uid=4000 home=/opt/wulaoshi groups=root shell=/sbin/nologin' #创建一个用户,并指定用户的id,用户的家目录,用户的附加组,用户的shell ansible db -m user -a 'name=wulaoshi state=absent' #删除用户但是不删除用户的家目录 ansible db -m user -a 'name=wulaoshi3 state=absent remove=yes' # 删除用户并删除用户的家目录
用户: 管理员 root 0 普通用户 系统用户 不能登录 1-999 centos7 1-499 centos6 登录用户 可以登录 1000-65535 centos7 500-65535 centos6 用户组: 管理员组 root 0 系统用户组 1-999 centos7 1-499 centos6 登录用户组 1000-65535 centos7 500-65535 centos6 -d 指定用户的家目录 -g 指定用户的组 -G 执行用户的附加组 -s 指定登录后使用的shell -r 创建一个系统组 useradd -r wusir 创建系统用户, 从999倒序 useradd -s /sbin/nologin alexsb 创建的是普通用户,从1000开始升序 useradd -d /opt/alexsb2 alexsb2 创建用户时指定用户的家目录 useradd -u 3000 alexsb6 # 创建用户并指定用户的uid userdel alex 删除用户 userdel -r alexsb2 删除用户并删除用户的家目录
5.2 group
参数: gid 组的id name 组名 system 系统组 state 示例: ansible db -m group -a 'name=wulaoshi system=yes' #创建系统组 ansible db -m group -a 'name=wulaoshi state=absent' # 删除组
groupadd yuchao 创建用户组
groupdel yuchao 删除用户组
给web组的每台机器都执行以下操作 创建一个用户组alex10 ansible web -m group -a 'name=alex10' 创建一个用户wusir10 ansible web -m user -a 'name=wusir10' 把/etc/fstab文件复制到远程主机上/tmp/f ansible web -m copy -a 'src=/etc/fstab dest=/tmp/f' 安装nginx,并启动,设置开机自启动 ansible web -m yum -a 'name=nginx' ansible web -m service -a 'name=nginx enabled=yes'
字典: key: value 列表: [] - 后缀名 yaml yml 注意:yaml语言非常严格 冒号后面必须有空格 等号后面不能有空格 - 后面也要有空格 严格对齐
ansible-playbook [options] playbook.yml [playbook2 ...]
-C, --check # 检查,白跑,干跑
-f FORKS, --forks=FORKS #用来做并发
--list-hosts # 列出主机列表
--syntax-check # 语法检查
- hosts: web
tasks:
- name: creategroup
group: name=alex10
- name: cretaeuser
user: name=wusir10
第一种方式(-e) ansible-playbook -e 'user=alexsb10' p2.yml 第二种方式(hosts文件 主机列表后面) [db] 192.168.107.132 user=alexsb11 192.168.107.133 user=alexsb12 第三种方式(hosts文件[groupname:vars]) [db:vars] #表示组的参数 user=alexsb13 第四种方式(playbook里面写vars) - hosts: db vars: - user: alexsb14 tasks: - name: create{{ user }} user: name={{ user}} 第五种传参方式(register) - hosts: db tasks: - name: sum shell: echo 7+8|bc register: user - name: createuser user: name={{user.stdout}} 传参方式的优先级 -e > playbook vars > hosts文件
6.5 ansible-playbook 条件判断 when
为何要用条件判断:因为可以针对不同的系统、不同的环境、不同的版本、不同的用户进行条件判断
- hosts: db remote_user: root tasks: - name: createfile copy: content="大弦嘈嘈如急雨" dest=/tmp/a.txt when: a=="3" - name: cratefile copy: content="小弦切切如私语" dest=/tmp/a.txt when: a=="4" 可以通过传参来进行条件判断该执行哪个任务,如: ansible-playbook -e 'a="3"' a.yml
6.6 tags
标签 tags -t 来传递标签
- hosts: web tasks: - name: installnginx yum: name=nginx - name: copyfile copy: src=/etc/nginx/nginx.conf dest=/etc/nginx/nginx.conf tags: copyfile - name: start service: name=nginx state=started 执行: ansible-playbook -t copyfile p7.yml
6.7
示例1: - hosts: web tasks: - name: createuser user: name={{item}} with_items: - alex20 - alex21 - alex22 示例2: - hosts: web tasks: - name: createuser user: name={{item}} with_items: - alex30 - alex31 - alex32 - name: creategroup group: name={{item}} with_items: - wulaoshi20 - wulaoshi21 - wulaoshi22
- hosts: web tasks: - name: creategroup group: name={{item}} with_items: - wulaoshi30 - wulaoshi31 - wulaoshi32 - name: createuser user: name={{item.name}} group={{item.group}} with_items: - {'name':alex40,'group':wulaoshi30} - {'name':alex41,'group':wulaoshi31} - {'name':alex42,'group':wulaoshi32}
6.8 template
注:copy和tamplate的区别:
copy模块不替代参数
template模块替代参数
1、src处写绝对路径 - hosts: web tasks: - name: installredis yum: name=redis - name: copyfile template: src=/etc/redis.conf dest=/etc/redis.conf - name: start service: name=redis state=started 修改配置文件为: bind {{ ansible_default_ipv4.address }} 2、src处写相对路径 - hosts: web tasks: - name: installredis yum: name=redis - name: copyfile template: src=redis.conf dest=/etc/redis.conf - name: start service: name=redis state=started 注:写相对路径: 在当前目录下新建一个templates目录,然后把文件放在templates目录里面
6.9
- hosts: web tasks: - name: installredis yum: name=redis - name: copyfile template: src=redis.conf dest=/etc/redis.conf tags: copyfile notify: restart - name: start service: name=redis state=started handlers: - name: restart service: name=redis state=restarted
6.10 roles
目录清晰
可以互相调用
roles文件夹 文件夹里面是要创建的每一个角色,每一个角色一个文件夹 每一个角色里面都有tasks(必须的),templates,files,handlers,vars目录 每个目录都要有main.yml文件,通过import_tasks来调用 - import_tasks: xxx.yml ... 其中templates文件夹中的文件可以通过相对路径来调用 其中files文件夹中的文件是否可以通过相对路径来调用?
7.
ansible_all_ipv4_addresses # ipv4的所有地址 ansible_all_ipv6_addresses # ipv6的所有地址 ansible_date_time # 获取到控制节点时间 ansible_default_ipv4 # 默认的ipv4地址 ansible_distribution # 系统 ansible_distribution_major_version # 系统的大版本 ansible_distribution_version # 系统的版本号 ansible_domain #系统所在的域 ansible_env #系统的环境变量 ansible_hostname #系统的主机名 ansible_fqdn #系统的全名 ansible_machine #系统的架构 ansible_memory_mb #系统的内存信息 ansible_os_family # 系统的家族 ansible_pkg_mgr # 系统的包管理工具 ansible_processor_cores #系统的cpu的核数(每颗) ansible_processor_count #系统cpu的颗数 ansible_processor_vcpus #系统cpu的总个数=cpu的颗数*CPU的核数 ansible_python # 系统上的python ansible cache -m setup -a 'filter=*processor*' # 用来搜索