首先須要給s-linuxad 的sudo配置文件中添加linux
s-linuxad ALL=(ALL) NOPASSWD:ALLios
若是沒有添加,確認服務器的/etc/sudoers的配置文件中包含以上配置nginx
#!/bin/bash openssl rand -base64 8 > ~/openssl PASS=`cat ~/.openssl` echo $PASS |sudo passwd --stdin root 1.1.1配置ansible 的配置文件/etc/ansible/hosts [s-linuxad@T-Ansible-v-szzb ansible]$ pwd /etc/ansible [s-linuxad@T-Ansible-v-szzb ansible]$ cat hosts [nginx] 10.0.40.156 10.0.40.143 10.0.40.235 10.0.40.61 10.0.40.87 10.0.40.95 10.0.40.224
推送腳本到各個服務器中執行:shell
ansible 'nginx' -m script -a '/home/s-linuxad/Carlton/cc.sh'api
上述命令中nginx 爲ansible的模塊,能夠本身命名的,好比命名爲test 均可以,若是命令爲test,那麼命令將會變動爲bash
ansible 'test' -m script -a '/home/s-linuxad/Carlton/cc.sh'服務器
程序運行的結果會有成功(SUCCESS)和不成功的,若是是成功的則能夠直接修改,若是是不成功的,則須要進一步處理ssh
10.0.2.198 | UNREACHABLE! => { "changed": false, "msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).\r\n", "unreachable": true } 10.0.2.196 | SUCCESS => { "changed": false, "ping": "pong" } 10.0.2.195 | SUCCESS => { "changed": false, "ping": "pong" }
查看生成的文件是密碼並能夠驗證密碼fetch
ansible nginx -m shell -a "cat ~/.openssl"spa
能夠將以上命令運行的結果追加到另外一個文件夾中
ansible nginx -m shell -a "cat ~/.openssl" >/tmp/file.txt
[s-linuxad@T-Ansible-v-szzb ~]$ cat fetch-file.yml --- - hosts: '{{myhosts}}' remote_user: s-linuxad tasks: - name: fetch file fetch: src: ~/.openssl dest: ~/fetch/openssl-{{ inventory_hostname }} flat: yes
yml 語句的格式:
- hosts: all remote_user: root tasks: - name: yum install screen shell: yum install screen –y
文件解析:-hosts:all指定對全部hosts生效,remote_user表示遠程root,tasks表示須要執行的任務;name顯示的名稱,shell後面接:須要在遠程客戶端執行的命令。能夠寫多個命令,以;分號隔開便可,例如 shell: yum install screen -y ;mkdir /tmp/`date +%Y%m%d`
ansible-playbook fetch-file.yml -e "myhosts=nginx"
批量回收文件密碼到家目錄下的fetch 目錄中
ansible nginx -m shell -a "rm -f ~/.openssl"
批量修改的前提必須是無密鑰創建成功
[s-linuxad@T-Ansible-v-szzb Carlton]$ cat s-linuxad.sh #!/bin/bash PASS=123456 #SUBPASS=`echo ${PASS:0:16}` echo $PASS |sudo passwd --stdin s-linuxad [s-linuxad@T-Ansible-v-szzb Carlton]$ cat nagios.sh #!/bin/bash PASS=12345676 #SUBPASS=`echo ${PASS:0:16}` echo $PASS |sudo passwd --stdin nagios [s-linuxad@T-Ansible-v-szzb Carlton]$ ansible 'nginx' -m script -a '/home/s-linuxad/Carlton/nagios.sh'