ansible
控制端、被控端
apt list ansible
yum list ansible
apt install ansible -y
ansible --version
vim /etc/ansible/hosts
[webservers]
11.0.1.48
11.0.1.58
[appservers]
11.0.1.68
11.0.1.78
ansible webservers --list-hosts
Ad-Hoc
ansible-doc -l | wc -l
ansible-doc -l | grep ping
ansible-doc -s ping
ansible webservers -m ping
ansible all -m ping
ansible all -m ping -k
ansible 11.0.1.* -m ping
ansible *servers -m ping
ansible "webservers,appservers" -m ping
ansible 'webservers,!appservers' -m ping
ansible all -m ping -v
ansible all -m ping -vvv
vim /etc/ansible/ansible.cfg
ansible-doc command
ansible-doc shell
ansible-doc script
ansible all -m script -a "test.sh"
ansible-doc copy
ansible webservers -m copy -a "src=/etc/passwd dest=/backup"
ansible-doc fetch
ansible all -m fetch -a 'src=/etc/redhat-release dest=/data/os'
ansible-doc file
ansible all -m file -a 'path=/app/ state=directory'
ansible all -m file -a 'path=/app/text.txt state=touch'
ansible all -m file -a 'src=/etc/passwd path=/app/pass.link state=link'
ansible all -m file -a 'src=/etc/fstab dest=/app/fstab.hard state=hard owner=wang group=root'
ansible-doc stat
ansible-doc unarchive
ansible-doc get_url
ansible-doc archive
ansible-doc hostname
ansible-doc cron
ansible-doc -s yum
ansible webservers -m yum -a 'name=httpd'
ansible webservers -m yum -a 'name=httpd state=absent'
ansible-doc -s apt
ansible appservers -m apt -a 'name=nginx state=absent autoremove=yes purge=yes'
ansible-doc yum_repository
ansible webservvers -m yum_repository -a 'name=test description="test repo" baseurl="https://11.0.1.88/testrepo" gpgcheck=no'
ansible webservers -m yum_repository -a 'name=test state=absent'
ansible webservers -m service -a 'name=nginx state=started enabled=yes'
ansible-doc user
ansible-doc group
ansible-doc lineinfile
ansible-doc replace
ansible-doc selinux
ansible-doc reboot
ansible-doc mount
ansible-doc setup
ansible webservers -m setup -a 'filter="ansible_default_ipv4"'
ansible webservers -m setup | grep ansible_default_ipv4
ansible-doc debug
ansible-doc sysctl
ansible-doc apt_repository
playbook-----> yaml
XML
JSON
Yaml
---
- hosts: webservers
tasks:
- name: test ping
ping:
- name: shell cmd
shell: 'hostname -I'
- hosts: appservers
remote_user: root
tasks:
- name: yum
yum:
name: nginx
- name: service
service:
name: nginx
state: started
enabled: yes
hosts
tasks
remote_user
action
handlers
vars
vars_files
ansible-playbook test.yaml
ansible-playbook --syntax-check test.yaml
ansible-playbook --limit 11.0.1.48 test.yaml
ansible-playbook -i inventory --list-hosts test.yaml
ANSIBLE_CONFIG------> ./ansible.cfg -------> ~/.ansible.cfg-------> /etc/ansible/ansible.cfg
ignore_errors: yes
......
notify: restart nginx
......
handlers:
name: restart nginx
service:
name: nginx
state: restarted
......
.....
tags: data
......
ansible-playbook -t data test.yaml
ansible-playbook --list-tags test.yaml
register
template
---
- hosts: webservers
tasks:
- name: test template
template:
src: /tmp/template.conf.j2
dest: /tmp/testtmp.conf
vim /tmp/template.conf.j2
{{ ansible_hostname }}
for
if
untils
loop
with_lines
item
with_items
when
roles
ansible-galaxy