一,安装ansible 主要以python编写
yum list |grep ansible
yum install epel-release
yum -y install ansible
在主节点下只有一台需要安装
关闭防火墙。
主机组的的配置
vi /etc/ansible/hosts
[testhost]
127.0.0.1
192.168.200.10
免密127 和192 在主节点
二。远程执行命令
ansible testhost -m command -a ‘w’
可以批量执行命令,这里的testhost 是主机的组名(可以写ip)-m 后面是模块名字,-a 后面是命令。 模块还可以是shell
再安装 libselinux-python 时安装在从节点。
拷贝文件或目录
ansible 192.168.200.10 -m copy -a ‘src=/test.txt dest=/tmp/123 owner=root group=root mode=0755’
copy 复制模块
src 本地的原地址
dest 目标目录,目录不存在时它自动窗口啊
owner 所属主
group 所属组
mode 权限
远程执行脚本
首先创建一个脚本
ansible 192.168.200.10 -m copy -a ‘src=/tmp/test.sh dest=/tmp/’
ansible testhost -m shell -a ‘/tmp/test.sh’
shell 还支持远程执行命令再加上管道符。
ansible testhost -m shell -a ‘cat /etc/passwd | wc -l’
三,管理任务计划
ansible testhost -m cron -a “name=’test cron ’ job=’/bin/touch /tmp/1212.txt’ weekday 6 ”
weekday 表时间 周 minute 表时间 分钟 hour 表时间小时 day 表时间日期 month 表时间月份
name= 任务的名字
job = 定义任务
crontab -e 查看查看任务是否创建
表示删除任务计划
ansible testhost -m cron -a “name=’test cron’ state=absent”
ansible 安装rpm包
ansible testhost -m yum -a “name=httpd”
ansible testhost -m service -a “name=httpd state=started enabled=yes”
列出systemd服务可以找到name的服务。
systemctl list-unit-files
查看指定模块文档
ansible-doc cron
ansible playbook’ 一般以yml 后缀结尾
相当于把模块写入到配置文件里,
vi /etc/ansible/test.yml
---
- hosts : 192.168.200.10
remote_user: root
tasks:
- name: test_playbook
shell: touch /tmp/ansible_test.txt
第一行需要有三条横杠 ,hosts参数指定了对哪些主机进行操作。多台机器可以用逗号隔开,也可以使用主机组,在/etc/ansible/hosts
user 参数指定了使用什么用户登录远程主机操作
tasks 指定了一个任务,其下面的name 参数同样是对任务的描述,在执行过程中坏打印出来,shell 是ansible 模块名字
执行playbook
ansible-playbook /etc/ansible/test.yml
changed=1 执行成功 =0 执行错误
vi /etc/ansible/create_user.yml
---
- name: create_user
hosts : 92.168.200.10
user: root
gather_facts: false #关闭了访问对方使程序执行的更快。
vars:
- user : “test
tasks:
- name; create user
user :name=”{{ user }}”
name 对playbook 实现的功能做一个概述,后面执行会打印name变量的值,可以省略;gather_facts 参数指定了在以下任务部分执行前,是否执行setup模块获取主机相关信息,后面的tasks会使用setup获取的信息时用到;
vars 指定变量,这里user,变量值要用引号括起来;
user 调用user模块,
name是user模块一个参数,增加的用户名字调用了上面user的值
执行
ansible-playbook /etc/ansible/create_user.yml
ansible playbook循环
四,改变文件权限
先在每个设备创建文件 1.txt 2.txt 3.txt
vi /etc/ansible/while.yml
---
- hosts: testhost
user: root
tasks:
- name: change mode for files
file: path=/tmp/{{ item }} mode =600
with_items:
- 1.txt
- 2.txt
- 3.txt
with_items 为循环的对象。
执行
ansible-playbook while.yml
ansible playbook 条件判断
vi /etc/ansible/whem.yml
---
- hosts: testhost
user: root
gather_facts: True
tasks:
- name: use when
shell: touch /tmp/when.txt
when: ansble_ens33.ipv4.address == “192.168.200.10”
五,when : 表判断相同执行反之不执行。
`ansible 192.168.200.10 -m setup`
查看所有的facter 信息。
ansible playbook 中的 handlers
主要应用, task后,服务器发生变化之后要执行一些操作,比如我们修改了配置文件
需要重新启动服务器,
vi /etc/ansible/handlers.yml
---
- name: handlers test
hosts: 192.168.200.10
user: root
tasks:
- name: copy file
copy: src=/etc/passwd dest=/tmp/aaa.txt
notify: test handlers
handlers:
- name: test handlers
shell: echo “1111” >> /tmp/aaa.txt
只有copy 模块执行后才会调用后面的handlers操作。也就是说1.txt 和 2.txt 文件相同,并不会执行handlers 里的shell命令这种比较适合配置文件发生改变后重启服务器。