寫ping的yml文件html
[root@ansible ~]# vim ping.yml
---
- hosts: all
remote_user: root
tasks:
- ping: web
[root@ansible ~]# ls
ping.ymlshell
[root@ansible ~]# ansible-playbook ping.yml
顯示以下:
PLAY [all] **********************************************************************************************apache
TASK [Gathering Facts] **********************************************************************************
ok: [web2]
ok: [db1]
ok: [web1]
ok: [cache]
ok: [db2]vim
TASK [ping] *********************************************************************************************
ok: [web2]
ok: [web1]
ok: [db1]
ok: [db2]
ok: [cache]ssh
PLAY RECAP **********************************************************************************************
cache : ok=2 changed=0 unreachable=0 failed=0
db1 : ok=2 changed=0 unreachable=0 failed=0
db2 : ok=2 changed=0 unreachable=0 failed=0
web1 : ok=2 changed=0 unreachable=0 failed=0
web2 : ok=2 changed=0 unreachable=0 failed=0
###############################################################################
查找有關user的模塊說明,找到EXAMPLES舉例,看用法curl
[root@ansible ~]# ansible-doc user
... ...
EXAMPLES:
# Add the user 'johnd' with a specific uid and a primary group of 'admin'
- user:
name: johnd
comment: "John Doe"
uid: 1040
group: admin
###############################################################################
查找有關shell的模塊說明,找到EXAMPLES舉例,看用法ui
[root@ansible ~]# ansible-doc shell
... ...
EXAMPLES:
- name: Execute the command in remote shell; stdout goes to the specified file on the remote.
shell: somescript.sh >> somelog.txt
###############################################################################
寫建立用戶plj的yml文件url
[root@ansible ~]# vim user.yml
---
- hosts: db
remote_user: root
tasks:
- user:
name: plj
- shell: echo 123|passwd --stdin plj
- shell: chage -d 0 plj命令行
[root@ansible ~]# ls
ping.yml user.yml
###############################################################################
tasks下面的name是註釋,好比:
[root@ansible ~]# vim abc.yml
---
- hosts: db
remote_user: root
tasks:
- user:
name: plj 這裏的name,在user模塊下,因此不是註釋
- name: aaaaaaa 這裏的name,在tasks模塊下,纔是註釋!
shell: echo 123|passwd --stdin plj
shell: chage -d 0 plj
###############################################################################
[root@ansible ~]# ansible-playbook user.yml
顯示以下:
PLAY [db] ***********************************************************************************************
TASK [Gathering Facts] **********************************************************************************
ok: [db1]
ok: [db2]
TASK [user] *********************************************************************************************
changed: [db1]
changed: [db2]
TASK [command] ******************************************************************************************
changed: [db1]
changed: [db2]
TASK [command] ******************************************************************************************
changed: [db1]
changed: [db2]
PLAY RECAP **********************************************************************************************
db1 : ok=4 changed=3 unreachable=0 failed=0
db2 : ok=4 changed=3 unreachable=0 failed=0
查看是否成功建立用戶plj
[root@ansible ~]# ansible db -m shell -a 'id plj' 在db這個組的全部主機上,查看是否存在用戶plj
顯示以下:
db2 | SUCCESS | rc=0 >>
uid=1000(plj) gid=1000(plj) 組=1000(plj) 主機db1上已經存在用戶plj,說明成功建立了用戶
db1 | SUCCESS | rc=0 >>
uid=1000(plj) gid=1000(plj) 組=1000(plj) 主機db2上已經存在用戶plj,說明成功建立了用戶
##########################################################################################
[root@ansible ~]# vim index.html
hello 1
hello 2
hello 3
[root@ansible ~]# vim http.yml
---
- hosts: web
remote_user: root
tasks:
- service:
name: httpd
state: stopped
- yum:
name: httpd
state: removed
- yum:
name: httpd
state: installed
- lineinfile:
path: /etc/httpd/conf/httpd.conf
regexp: '^Listen'
line: 'Listen 8080'
- lineinfile:
path: /etc/httpd/conf/httpd.conf
regexp: '^#ServerName'
line: 'ServerName localhost'
- command: 'apachectl -t'
- copy:
src: index.html
dest: /var/www/html/
owner: apache
group: apache
mode: 0644
- service:
name: httpd
state: started
enabled: yes
[root@ansible ~]# ansible-playbook http.yml
PLAY [web] ***********************************************************************************************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************************************************************************
ok: [web1]
ok: [web2]
TASK [service] *******************************************************************************************************************************************************************************
changed: [web1]
changed: [web2]
TASK [yum] ***********************************************************************************************************************************************************************************
changed: [web1]
changed: [web2]
TASK [yum] ***********************************************************************************************************************************************************************************
changed: [web1]
changed: [web2]
TASK [lineinfile] ****************************************************************************************************************************************************************************
changed: [web1]
changed: [web2]
TASK [lineinfile] ****************************************************************************************************************************************************************************
changed: [web1]
changed: [web2]
TASK [command] *******************************************************************************************************************************************************************************
changed: [web2]
changed: [web1]
TASK [copy] **********************************************************************************************************************************************************************************
changed: [web2]
changed: [web1]
TASK [service] *******************************************************************************************************************************************************************************
changed: [web1]
changed: [web2]
TASK [service] *******************************************************************************************************************************************************************************
changed: [web2]
changed: [web1]
PLAY RECAP ***********************************************************************************************************************************************************************************
web1 : ok=10 changed=9 unreachable=0 failed=0
web2 : ok=10 changed=9 unreachable=0 failed=0
#################################################################################
[root@ansible ~]# vim user.yml
---
- hosts: db
remote_user: root
vars: 設置一個變量模塊
username: nb 這裏的nb是變量username的值
tasks:
- user:
name: "{{username}}" 引用變量的格式是"{{變量名}}"
- shell: echo 123|passwd --stdin "{{username}}"
- shell: chage -d 0 "{{username}}"
[root@ansible ~]# ansible-playbook user.yml
PLAY [db] **************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************
ok: [db1]
ok: [db2]
TASK [user] ************************************************************************************************************
changed: [db2]
changed: [db1]
TASK [command] *********************************************************************************************************
changed: [db2]
changed: [db1]
TASK [command] *********************************************************************************************************
changed: [db2]
changed: [db1]
PLAY RECAP *************************************************************************************************************
db1 : ok=4 changed=3 unreachable=0 failed=0
db2 : ok=4 changed=3 unreachable=0 failed=0
###################################################################################
下面文件裏沒有給變量username賦值
[root@ansible ~]# vim user.yml
---
- hosts: db
remote_user: root
tasks:
- user:
name: "{{username}}"
- shell: echo 123|passwd --stdin "{{username}}"
- shell: chage -d 0 "{{username}}"
直接在命令行給變量username賦值
[root@ansible ~]# ansible-playbook user.yml -e '{"username": "dd"}'
##########################################################################
[root@ansible ~]# vim user.yml
---
- hosts: db
remote_user: root
vars:
username: tom
tasks:
- user:
name: "{{username}}"
password: "{{'123'|password_hash('sha512')}}"
[root@ansible ~]# ansible-playbook user.yml
PLAY [db] **************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************
ok: [db1]
ok: [db2]
TASK [user] ************************************************************************************************************
changed: [db2]
changed: [db1]
PLAY RECAP *************************************************************************************************************
db1 : ok=2 changed=1 unreachable=0 failed=0
db2 : ok=2 changed=1 unreachable=0 failed=0
[root@ansible ~]# ssh tom@db1
tom@db1's password: 輸入密碼123
[tom@db1 ~]$ 成功以tom身份登錄db1
###########################################################################
[root@ansible ~]# vim name.yml
---
- hosts: db
remote_user: root
tasks:
- user:
name: "{{username}}"
password: "{{'123'|password_hash('sha512')}}"
[root@ansible ~]# vim aa
{
"username": "tom",
"username": "harry",
"username": "jeck"
}
[root@ansible ~]# ansible-playbook name.yml -e '@aa'
############################################################################
[root@ansible ~]# cat /etc/login.defs
... ...
ENCRYPT_METHOD SHA512
[root@ansible ~]# echo 123 |sha512sum
ea2fe56bb8c1fb5ada84963b42ed71b764a74b092d75755173ade06f2f4aada9c00d6c302e185035cbe85fdff31698bca93e8661f0cbcef52cf2ff65864fd742 -
[root@ansible ~]# echo 123 |md5sum
ba1f2511fc30423bdbb183fe33f3dd0f -
############################################################################
[root@ansible ~]# A="abcd"
[root@ansible ~]# echo ${A^^}
ABCD
##############################################################
[root@ansible ~]# vim useradd.yml
---
- hosts: web
remote_user: root
tasks:
- shell: useradd z3
- shell: useradd li4
ignore_errors: True 告訴ansible,遇到這個shell的錯誤就忽略
- shell: echo 123 | passwd --stdin z3
- shell: echo 123 | passwd --stdin li4
[root@ansible ~]# ansible-playbook useradd.yml
PLAY [web] *************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************
ok: [web2]
ok: [web1]
TASK [command] *********************************************************************************************************
changed: [web1]
changed: [web2]
TASK [command] *********************************************************************************************************
fatal: [web2]: FAILED! => {"changed": true, "cmd": "useradd li4", "delta": "0:00:00.004996", "end": "2018-12-29 15:37:28.440544", "msg": "non-zero return code", "rc": 9, "start": "2018-12-29 15:37:28.435548", "stderr": "useradd:用戶「li4」已存在", "stderr_lines": ["useradd:用戶「li4」已存在"], "stdout": "", "stdout_lines": []}
...ignoring
changed: [web1]
TASK [command] *********************************************************************************************************
changed: [web2]
changed: [web1]
TASK [command] *********************************************************************************************************
changed: [web1]
changed: [web2]
PLAY RECAP *************************************************************************************************************
web1 : ok=5 changed=4 unreachable=0 failed=0
web2 : ok=5 changed=4 unreachable=0 failed=0
###################################################################################
[root@ansible ~]# vim http.yml
---
- hosts: web
remote_user: root
tasks:
- service:
name: httpd
state: stopped
- yum:
name: httpd
state: removed
- yum:
name: httpd
state: installed
- lineinfile:
path: /etc/httpd/conf/httpd.conf
regexp: '^Listen'
line: 'Listen 80'
tags: editconf
notify:
- reload apache
- lineinfile:
path: /etc/httpd/conf/httpd.conf
regexp: '^#ServerName'
line: 'ServerName localhost'
- command: 'apachectl -t'
- copy:
src: index.html
dest: /var/www/html/
owner: apache
group: apache
mode: 0644
handlers:
- name: reload apache
service:
name: httpd
state: restarted
enabled: yes
[root@ansible ~]# ansible-playbook http.yml --list-tags
playbook: http.yml
play #1 (web): web TAGS: []
TASK TAGS: [editconf] 標籤TAGS是editconf
[root@ansible ~]# ansible-playbook http.yml --tags editconf 引用標籤TAGS是editconf
PLAY [web] *************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************
ok: [web1]
ok: [web2]
TASK [lineinfile] ******************************************************************************************************
changed: [web1]
changed: [web2]
RUNNING HANDLER [reload apache] ****************************************************************************************
changed: [web2]
changed: [web1]
PLAY RECAP *************************************************************************************************************
web1 : ok=3 changed=2 unreachable=0 failed=0
web2 : ok=3 changed=2 unreachable=0 failed=0
#################################################################################
[root@ansible ~]# vim load.yml
[root@ansible ~]# cat load.yml
---
- hosts: web
remote_user: root
tasks:
- shell: uptime | awk '{printf("%.2f",$(NF-2))}'
register: result
- service:
name: httpd
state: stopped
when: result.stdout | float > 0.7
[root@ansible ~]# ansible-playbook load.yml
PLAY [web] *************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************
ok: [web1]
ok: [web2]
TASK [command] *********************************************************************************************************
changed: [web2]
changed: [web1]
TASK [service] *********************************************************************************************************
skipping: [web1]
skipping: [web2]
PLAY RECAP *************************************************************************************************************
web1 : ok=2 changed=1 unreachable=0 failed=0
web2 : ok=2 changed=1 unreachable=0 failed=0
[root@ansible ~]# curl web1
hello 1
hello 2
hello 3
#####################################################################################
[root@ansible ~]# vim user5.yml
---
- hosts: web
remote_user: root
tasks:
- user:
name: "{{item.name}}"
group: "{{item.group}}"
password: "{{item.pwd}}"
with_items:
-
name: "nb"
group: "users"
pwd: "bb"
-
name: "dd"
group: "bin"
pwd: "xdd"
-
name: "jj"
group: "apache"
pwd: "plj"
[root@ansible ~]# ansible-playbook user5.yml
PLAY [web] *************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************
ok: [web2]
ok: [web1]
TASK [user] ************************************************************************************************************
changed: [web2] => (item={u'pwd': u'bb', u'group': u'users', u'name': u'nb'})
changed: [web1] => (item={u'pwd': u'bb', u'group': u'users', u'name': u'nb'})
changed: [web2] => (item={u'pwd': u'xdd', u'group': u'bin', u'name': u'dd'})
changed: [web1] => (item={u'pwd': u'xdd', u'group': u'bin', u'name': u'dd'})
changed: [web1] => (item={u'pwd': u'plj', u'group': u'apache', u'name': u'jj'})
changed: [web2] => (item={u'pwd': u'plj', u'group': u'apache', u'name': u'jj'})
PLAY RECAP *************************************************************************************************************
web1 : ok=2 changed=1 unreachable=0 failed=0
web2 : ok=2 changed=1 unreachable=0 failed=0
##################################################################
循環(沒啥用,看一下就行)
[root@ansible ~]# vim echo.yml
---
- hosts: web
remote_user: root
vars:
id: [1,2,3]
en: ["a","b","c"]
tasks:
- shell: echo "{{item}}"
with_nested:
- "{{id}}"
- "{{en}}"
[root@ansible ~]# ansible-playbook echo.yml
PLAY [web] ******************************************************************************************
TASK [Gathering Facts] ******************************************************************************
ok: [web2]
ok: [web1]
TASK [command] **************************************************************************************
changed: [web1] => (item=[1, u'a'])
changed: [web2] => (item=[1, u'a'])
changed: [web1] => (item=[1, u'b'])
changed: [web2] => (item=[1, u'b'])
changed: [web1] => (item=[1, u'c'])
changed: [web2] => (item=[1, u'c'])
changed: [web1] => (item=[2, u'a'])
changed: [web2] => (item=[2, u'a'])
changed: [web1] => (item=[2, u'b'])
changed: [web2] => (item=[2, u'b'])
changed: [web1] => (item=[2, u'c'])
changed: [web2] => (item=[2, u'c'])
changed: [web2] => (item=[3, u'a'])
changed: [web1] => (item=[3, u'a'])
changed: [web2] => (item=[3, u'b'])
changed: [web1] => (item=[3, u'b'])
changed: [web2] => (item=[3, u'c'])
changed: [web1] => (item=[3, u'c'])
PLAY RECAP ******************************************************************************************web1 : ok=2 changed=1 unreachable=0 failed=0 web2 : ok=2 changed=1 unreachable=0 failed=0