特性node
Ansible 主要组层部分python
相关文件nginx
Ansible 文件配置web
ansible 经过 ssh 实现配置管理,应用部署,任务执行等功能,所以,须要实现配置 ansible 端能基于秘钥认证的方式联系各被管理节点shell
ansible 命令用法编程
ansible <host-pattern> [-m module_name] [-a args] --version #显示版本 -m module # 指定模块,默认为 command -v # 详细过程 -vv -vvv 更详细的 --list-hosts # 显示主机列表,可简写 --list -k, --ask-pass # 显示链接密码,默认key验证 -C,--check # 检查,并不执行 -T, --timeout=TIMEOUT # 执行命令的超时时间,默认10s -u, --user = REMOTE_USER # 执行远程执行的用户 -b, --become # 代替旧版的 sudo 切换
主配置文件ansible.cfg 配置缓存
[default] inventory = /etc/ansible/host # 主机列表配置文件 library = /usr/share/my_modules # 哭文件存放目录 remote_tmp = $HOME/.ansible/tmp # 临时py命令文件存放在运城主机目录 local_tmp = $HOME/.ansible/tmp # 本机的临时命令执行目录 forks = 5 # 默认并发数 sudu_user = root # 默认sudo用户 ask_sudo_pass = True # 每次执行 ansible 命令是否询问ssh密码 ask_pass = True remote_port = 22 host_key_checking = False # 检查对应服务器的host_key,建议取消注释
log_path = /var/log/ansible.log # 默认日志 建议开启
主机清单 hosts 配置安全
# 分组配置,一台主机能够再多个组内, 默认 分组 all 所有主机 [web] 172.16.0.40 [db] 172.16.0.40 172.16.0.41 # www[001:006].example.com
ansible 命令执行过程bash
1. 加载本身的配置文件 默认/etc/ansible/ansible.cfg 2. 加载本身对应的模块文件 如: command 3. 经过 ansible 将模块或命令生成对应的临时 py 文件,并将该文件传输至远程服务器的对应执行用户 $HOME/.ansible/tmp/ansible-tmp-数字/xxx.py 文件 4. 给文件 +x 执行权限 5. 执行并返回结果 6. 删除临时 py 文件, sleep 0 退出 # 执行状态 绿色: 执行成功而且不须要作改变的操做 黄色: 执行成功而且对目标主机作变动 红色: 执行失败
模块服务器
# command #在远程主机执行命令,默认模块,可忽略 -m 选项,变量 重定向 管道符等有问题,须要使用 shell模块 - chdir # 改变工做目录 ~]# ansible all -m command -a "chdir=/var/log/ ls" - creates # 若是文件存在 则不执行后续命令 ~]# ansible all -m command -a "creates=/etc/fstab ls /" 172.16.0.40 | SUCCESS | rc=0 >> skipped, since /etc/fstab exists 172.16.0.41 | SUCCESS | rc=0 >> skipped, since /etc/fstab exists - removes # 若是文件存在 则执行后续命令 ~]# ansible all -m command -a "removes=/etc/fstab ls /" # shell # 和 command 类似,用shell 执行命令 命令要用单引号 ~]# ansible all -m shell -a 'echo $HOSTNAME' # Script : 运行脚本, 本机脚本在远程主机上运行 ~]# ansible all -m script -a '/data/test.sh' # copy 从服务器复制文件到客户端 ~]# ansible all -m copy -a 'src=/data/test.sh dest=/data/ backup=yes mode=600 owner=test'
~]# ansible all -m copy -a 'content="df -h\nhostname\nls\n" dest=/data/f1.sh' # src 源文件 # dest 目标文件 # backup=yes 若是存在同名文件是否备份 # mode 文件权限 # owner 全部者
# content 把内容写到某个文件
和 copy 模块相反,不能抓取目录,目录可先tar ~]# ansible all -m fetch -a 'src=/etc/passwd dest=/data' ~]# tree /data/ /data/ ├── 172.16.0.40 │ └── etc │ └── passwd ├── 172.16.0.41 │ └── etc │ └── passwd
# 建立一个空文件 ~]# ansible all -m file -a 'path=/data/testfile state=touch mode=600 owner=test group=test' # 建立链接文件 ~]# ansible all -m file -a 'src=/data/testfile path=/tmp/testfile-link state=link' # 建立空文件夹 ~]# ansible all -m file -a 'path=/data/test1 state=directory' # 删除文件夹 ~]# ansible all -m file -a 'path=/data/test1 state=absent'
~]# ansible 172.16.0.41 -m hostname -a 'name=test-node1'
# 支持时间 minute, hour, day, month, weekday # 周六周日 每隔五分钟 打印一条消息 ~]# ansible all -m cron -a 'minute=*/5 weekday=0,6 job="/usr/bin/wall cron job" name="test cronjob"' # 禁用 定时任务 ~]# ansible all -m cron -a 'disabled=true job="/usr/bin/wall cron job" name="test cronjob"' # 取消禁用 ~]# ansible all -m cron -a 'disabled=no job="/usr/bin/wall cron job" name="test cronjob"' # 删除计划任务 ~]# ansible all -m cron -a 'state=absent job="/usr/bin/wall cron job" name="test cronjob"'
# 安装软件包 ~]# ansible all -m yum -a 'name=httpd state=present' # 删除软件包 ~]# ansible all -m yum -a 'name=httpd state=absent' # 安装最新版本 ~]# ansible all -m yum -a 'name=httpd state=latest' # 临时禁用 gpg 检查 ~]# ansible all -m yum -a 'name=httpd state=present disable_gpg_check=yes' # 更新缓存 ~]# ansible all -m yum -a 'name=httpd,vsftpd state=present disable_gpg_check=yes update_cache=yes'
~]# ansible all -m service -a 'name=httpd state=stopped enabled=yes' ~]# ansible all -m service -a 'name=httpd state=started' ~]# ansible all -m service -a 'name=httpd state=restarted'
# 添加用户,指定: 备注,uid,家目录,主组,附属组 ~]# ansible all -m user -a 'name=test2 comment="test user2" uid=2000 home=/data/test2 group=test groups=root,bin' ~]# ansible all -a 'getent passwd test2' 172.16.0.40 | CHANGED | rc=0 >> test2:x:2000:1001:test user2:/data/test2:/bin/bash # 添加系统用户 ,shell 类型 ~]# ansible all -m user -a 'name=sysuser system=yes shell=/sbin/nologin' # 删除用户 ~]# ansible all -m user -a 'name=sysuser state=absent' # 删除用户同时删除家目录 ~]# ansible all -m user -a 'name=test2 state=absent remove=yes'
# 添加组 ~]# ansible all -m group -a 'name=group1' # 查看组 ~]# ansible all -a 'getent group group1' # 删除组 ~]# ansible all -m group -a 'name=group1 state=absent'
Playbook
ansible-galaxy
ansible-galaxy remove geerlingguy.nginx
ansible-pull
ansible-playbook
ansible-vault
]# ansible-vault encrypt hello.yml #加密
]# ansible-vault view hello.yml # 查看加密文件
]# ansible-vault edit hello.yml # 编辑加密文件
]# ansible-vault create hello.yml # 建立新文件