Ansible部分实操记录

ansible配置

  • ansible设置hosts
1
2
3
4
5
6
7
8
9
vim /etc/ansible/hosts

[idc1]
10.230.11.11 ansible_ssh_user=ansible
10.230.11.12 ansible_ssh_user=ansible

[idc2]
10.230.12.11 ansible_ssh_user=ansible
10.230.13.9 ansible_ssh_user=ansible
  • ansible公钥copy到客户端
1
ssh-copy-id -i /root/.ssh/id_rsa.pub -p 22 ansible@10.230.11.11
  • ansible设置免sudo
1
2
3
4
5
6
7
vim /etc/ansible/ansible.cfg

[privilege_escalation]
become=True
become_method=sudo
become_user=root
become_ask_pass=False
  • 客户端添加sudoer
1
2
3
vim /etc/sudoers

ansible ALL=(ALL) NOPASSWD:ALL

ansible操作

  • 执行命令
1
2
ansible all -a 'systemctl restart sshd'
ansible idc1 -a 'date'
  • copy文件
1
ansible idc1 -m copy -a "src=/data/1.txt dest=/opt/"
  • 批量修改文件(sed)
1
2
3
4
5
# 'Server='行后追加内容
ansible idc1 -a "sed -i 's/Server=.*/&,10.228.13.53/' /data/zabbix_agent/conf/zabbix_agentd.conf"

# 'ServerActive='的下一行添加内容
ansible idc2 -a "sed -i '/^ServerActive=/a\ServerActive=10.228.13.53' /data/zabbix_agent/conf/zabbix_agentd.conf"
  • 修改密码(playbook)
1
2
3
4
5
6
7
8
9
ansible-playbook passwd.yml -e "name1=tencentadm2 chpass='WoBusys#07' "

# passwd.yml
- hosts: all
gather_facts: false
remote_user: root
tasks:
- name: Change password
user: name={{name1}} password={{ chpass | password_hash('sha512') }} update_password=always
-------------本文结束感谢您的阅读-------------
原创技术分享,感谢您的支持。