安装环境
dnf install -y ansible
查看命令说明
ansible-doc [命令名]
配置文件优先级
>/etc/ansible/ansible.cfg【低】
>>~/.ansible.cfg【中】
>>>./ansible.cfg【高】
ansible.cfg自定义配置文件
[defaults]
inventory=清单文件名【inventory】
remote_user=远程登录用户身份【alice】
roles_path=登录角色组【roles】
[privilege_escalation]
become=True
become_method=sudo
become_user=root
become_ask_pass=False
管理清单文件
[test01]
node1
[test02]
node2
[web]
node3
node4
[test05]
node5
[webtest:children]
web
简单命令推送
#不支持管道、重定向等
ansible [管理节点] -m command -a 'ls /root'
#支持管道、重定向等语法
ansible [管理节点] -m shell -a 'ip addr | grep 172'
脚本命令推送
#生成脚本文件
echo "
[base]
name=base
baseurl=http://server1.net0.example.com/rhel8/BaseOS
gpgcheck=0
" > a.sh
#授予执行权
chmod +x a.sh
#推送
ansible [管理节点] -m script -a './a.sh'
配置yum源
ansible [管理节点] -m yum-resposity -a 'name=BASE baseurl=? description=? gpgcheck=yes gpgkey=? enabled=yes'
shell命令方式替换文件内容
#行替换
command: sed -i /^SELINUX=/cSELINUX=Permissive' /etc/selinux/config
#关键字替换
command: sed -i s/=enforcing/=Permissive/g' /etc/selinux/config
自定义角色tasks/main.yml
- yum: name=httpd state=persent
- template: src=index.html.j2 dest/var/www/html/index.html
- service: name=firewalld state=restarted enabled=yes
- service: name=httpd state=restarted enabled=yes
- firewalld: service=http state=enabled immediate=yes permanent=yes
配置selinux访问httpd服务权限
- yum: name=policycoreutils-python-utils state=present
- command: chcon -R -t httpd_sys_content_t [目标目录]
通过galaxy安装角色
- name: haproxy
- src: [http://rolename.tar]