标签:ref books sed address wiki mask 执行 you pts
通过Yum安装RPMs适用于 EPEL 6, 7, 以及仍在支持中的Fedora发行版.这里我们直接安装就行。
sudo yum install ansible
这里我们需要准备两台主机,我们需要在主机A上生成秘钥
ssh-keygen -t rsa
编辑(或创建)/etc/ansible/hosts 并在其中加入一个或多个远程系统.你的public SSH key必须在这些系统的``authorized_keys``中:
$ ssh-copy-id -i .ssh/id_rsa.pub root@172.16.138.41 #新增系统,并命名为webhost主机组 $ vim /etc/ansible/hosts [webhost] 172.16.138.40 172.16.138.41
ansible all -m ping 172.16.138.40 | SUCCESS => { "changed": false, "ping": "pong" } 172.16.138.41 | SUCCESS => { "changed": false, "ping": "pong" }
我们也可以通过主机组执行命令
$ ansible webhost -m command -a "w" 172.16.138.41 | CHANGED | rc=0 >> 01:35:17 up 29 days, 3:34, 2 users, load average: 0.24, 0.33, 0.27 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root pts/0 172.16.40.86 23:29 41.00s 0.00s 0.00s -bash root pts/1 k8s-master 01:35 0.00s 0.06s 0.00s w 172.16.138.40 | CHANGED | rc=0 >> 01:35:18 up 29 days, 3:34, 3 users, load average: 0.98, 0.88, 0.86 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root pts/0 172.16.40.86 23:06 1:08m 0.29s 0.29s -bash root pts/1 172.16.40.86 01:26 6.00s 1.11s 0.00s ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/2c6989e158 -tt 172.16.138.40 /bin/sh -c ‘/usr/bin/python /root/.ansible/tmp/ansible-tmp-1553146517.04-153785974352870/AnsiballZ_command.py && sleep 0‘ root pts/3 k8s-master 01:35 1.00s 0.10s 0.01s w
这里还有一个shell模块。同样也支持写一个命令。
ansible 172.16.138.41 -m copy -a "src=/etc/ansible dest=/tmp/ansible_test owner=root group=root mode=755" 172.16.138.41 | CHANGED => { "changed": true, "dest": "/tmp/ansible_test/", "src": "/etc/ansible" }
我们看一下目标主机下/tmp目录
[root@node02 tmp]# ls ansible_test [root@node02 tmp]# ls ansible_test/ ansible [root@node02 tmp]#
需要注意的是,如果目标主机没有这个目录会自动创建这个目录,如果拷贝是文件,目标主机指定的名字和源如果不同,并且不是已经存在的目录,相当于copy过去又重命名。但相反,如果目标主机上已经处在的目录,则会直接把文件copy到该目录下。
我们先随便写一个脚本
#!/bin/bash date >> /tmp/data.txt
ansible需要先把脚本copy到对应主机上
$ ansible 172.16.138.41 -m copy -a "src=/tmp/1.sh dest=/tmp/test.sh owner=root group=root mode=755"
172.16.138.41 | CHANGED => {
"changed": true,
"checksum": "a0d6b0777539641b9aab412a0297b1273e836bbb",
"dest": "/tmp/test.sh",
"gid": 0,
"group": "root",
"md5sum": "fc416150a5e218531c550b98e6ea35b6",
"mode": "0755",
"owner": "root",
"size": 36,
"src": "/root/.ansible/tmp/ansible-tmp-1553151731.05-254487603693246/source",
"state": "file",
"uid": 0
}
执行远程脚本
ansible 172.16.138.41 -m shell -a "/tmp/test.sh" 172.16.138.41 | CHANGED | rc=0 >>
ansible 172.16.138.41 -m cron -a "name=‘test cron‘ job=‘touch /tmp/111.txt‘ weekday=6" 172.16.138.41 | CHANGED => { "changed": true, "envs": [], "jobs": [ "test cron" ] }
我们看一下目录主机的定时任务
$ crontab -l #Ansible: test cron * * * * 6 touch /tmp/111.txt
我们看到有个Ansible的表示,下面是定时任务的内容。
我们可以通过state=absent 来删除定时任务
$ ansible 172.16.138.41 -m cron -a "name=‘test cron‘ state=absent" 172.16.138.41 | CHANGED => { "changed": true, "envs": [], "jobs": [] }
playbook就是把一些模块的集合搞到一个文件里。
例如:
--- #表示开始 - hosts: 172.16.138.41 #指定远程主机 remote_user: root #指定远程用户 tasks: #任务 - name: test_playbook #任务名字 shell: touch /tmp/playbook.txt #具体的任务,核心
执行
ansible-playbook /etc/ansible/test.yaml PLAY [172.16.138.41] ****************************************************************************************************************** TASK [Gathering Facts] **************************************************************************************************************** ok: [172.16.138.41] TASK [test_playbook] ****************************************************************************************************************** [WARNING]: Consider using the file module with state=touch rather than running ‘touch‘. If you need to use command because file is insufficient you can add ‘warn: false‘ to this command task or set ‘command_warnings=False‘ in ansible.cfg to get rid of this message. changed: [172.16.138.41] PLAY RECAP **************************************************************************************************************************** 172.16.138.41 : ok=2 changed=1 unreachable=0 failed=0
我们创建一个用户。通过变量传过去
--- - name: create_user hosts: 172.16.138.41 remote_user: root gather_facts: false vars: - user: "zzkk" tasks: - name: create-user user: name="{{ user }}"
执行:
ansible-playbook /etc/ansible/user.yaml PLAY [create_user] ******************************************************************************************************************** TASK [create-user] ******************************************************************************************************************** changed: [172.16.138.41] PLAY RECAP **************************************************************************************************************************** 172.16.138.41 : ok=1 changed=1 unreachable=0 failed=0
创建三个文件,并修复其权限为600
--- - hosts: 172.16.138.41 user: root task: - name: "touch 1 2 3 file and change file mode" file: path=/tmp/{{ item }} state=touch mode=600 with_items: - 1.txt - 2.txt - 3.txt
执行:
ansible-playbook /etc/ansible/while.yaml PLAY [172.16.138.41] ****************************************************************************************************************** TASK [Gathering Facts] **************************************************************************************************************** ok: [172.16.138.41] TASK [touch 1 2 3 file and change file mode] ****************************************************************************************** changed: [172.16.138.41] => (item=1.txt) changed: [172.16.138.41] => (item=2.txt) changed: [172.16.138.41] => (item=3.txt) PLAY RECAP **************************************************************************************************************************** 172.16.138.41 : ok=2 changed=1 unreachable=0 failed=0
我们查看一下gather_facts收集到的信息,来作为我们判断的条件,这里我们要获取IP地址。
ansible 172.16.138.41 -m setup .... "ansible_ens160": { "active": true, "device": "ens160", .... "ipv4": { "address": "172.16.138.41", "broadcast": "172.16.138.255", "netmask": "255.255.255.0", "network": "172.16.138.0" }, ....
我们写一个判断条件 当ansible_ens160.ipv4.address = 172.16.138.41 执行创建文件
--- - hosts: webhost user: root gather_facts: True tasks: - name: stady when shell: touch /tmp/when.txt when: ansible_ens160.ipv4.address == "172.16.138.41"
执行结果:
ansible-playbook /etc/ansible/when.yaml PLAY [webhost] ************************************************************************************************************************ TASK [Gathering Facts] **************************************************************************************************************** ok: [172.16.138.41] ok: [172.16.138.40] TASK [stady when] ********************************************************************************************************************* skipping: [172.16.138.40] [WARNING]: Consider using the file module with state=touch rather than running ‘touch‘. If you need to use command because file is insufficient you can add ‘warn: false‘ to this command task or set ‘command_warnings=False‘ in ansible.cfg to get rid of this message. changed: [172.16.138.41] PLAY RECAP **************************************************************************************************************************** 172.16.138.40 : ok=1 changed=0 unreachable=0 failed=0 172.16.138.41 : ok=2 changed=1 unreachable=0 failed=0
这里我们看到跳过172.16.138.40,在172.16.138.41中执行。
module 具有”幂等”性,所以当远端系统被人改动时,可以重放 playbooks 达到恢复的目的. playbooks 本身可以识别这种改动,并且有一个基本的 event system(事件系统),可以响应这种改动.
(当发生改动时)’notify’ actions 会在 playbook 的每一个 task 结束时被触发,而且即使有多个不同的 task 通知改动的发生, ‘notify’ actions 只会被触发一次.
举例来说,比如多个 resources 指出因为一个配置文件被改动,所以 apache 需要重新启动,但是重新启动的操作只会被执行一次.
样例:
--- - name: handler test hosts: 172.16.138.41 user: root tasks: - name: copy file copy: src=/etc/passwd dest=/tmp/aaaaa.txt notify: test handlers handlers: - name: test handlers shell: echo "1111" >> /tmp/aaaaa.txt
执行:
ansible-playbook /etc/ansible/handler.yaml PLAY [handler test] ******************************************************************************************************************* TASK [Gathering Facts] **************************************************************************************************************** ok: [172.16.138.41] TASK [copy file] ********************************************************************************************************************** changed: [172.16.138.41] RUNNING HANDLER [test handlers] ******************************************************************************************************* changed: [172.16.138.41] PLAY RECAP **************************************************************************************************************************** 172.16.138.41 : ok=3 changed=2 unreachable=0 failed=0
Handlers 最佳的应用场景是用来重启服务,或者触发系统重启操作.除此以外很少用到了.
标签:ref books sed address wiki mask 执行 you pts
原文地址:https://www.cnblogs.com/xzkzzz/p/10573651.html