1. 程式人生 > >ansible常用模組整理

ansible常用模組整理

0 command

預設,直接-a   "ifconfig"  

1 shell

用到變數、管道時 ansible s21 -m shell -a "echo pwd | passwd --stdin user1" 此時用command無效  

2 script

將本地指令碼複製到遠端,並執行 ansible s21 -m script  -a "/tmp/test.sh"      

3 debug

ansible s21  -m debug -a "msg={{ 'mypassword' | password_hash('sha512', 'mysecretsalt') }}"  

4  cron

state:    present    absent     ansible s21  -m cron  -a 'minute="*/10" job="/bin/echo hello" name="test cron job" state=present'            

5 user

5.1 playbook直接設定方式
---   - hosts: s21     gather_facts: false     tasks:     - name: change user passwd       user: name={{ item.name }} password={{ item.chpass | password_hash('sha512') }}  update_password=always       with_items:            - { name: 'u1', chpass: 'abc' }            - { name: 'u2', chpass: 'bcd' } ansible-playbook  user.yml   5.2 playbook傳引數方式
---     - hosts: s21       gather_facts: false       tasks:       - name: Change password         user: name={{ name1 }}  password={{ chpass | password_hash('sha512') }}  update_password=always ansible-playbook  user-param.yml  -e "name1=u1 chpass=aaa"     5.3 python手動加密 >>> import crypt >>> crypt.crypt('abc') '$6$1JV5iq/jgGos6Bwe$yIuouAmby/zljZFmayyVb.rvSaqRihi9oWYj25OsIruhwQqeBhO5Om78pNFOWu1Q1E58mfq1wBZiS0B6x7xMu0'   ansible s21 -m user -a 'name=u1 password=$6$1JV5iq/jgGos6Bwe$yIuouAmby/zljZFmayyVb.rvSaqRihi9oWYj25OsIruhwQqeBhO5Om78pNFOWu1Q1E58mfq1wBZiS0B6x7xMu0 state=present'     5.4 shell模式 ansible s21 -m shell -a "echo pwd | passwd --stdin user1"    

6 group

ansible s21 -m group -a 'name=mysql system=yes' ansible s21 -m user -a 'name=mysql system=yes group=mysql'      

7 copy

有源拷貝
ansible s21  -m copy -a "src=user.yml dest=/tmp/ owner=u1 mode=600"   無源建立 ansible s225 -m copy -a "content='aaa\r\n' dest=/tmp/test/abc"    

8 file

許可權 ansible s21 -m file  -a 'owner=mysql group=mysql mode=664 path=/tmp/a.txt' 軟連結 ansible s225 -m file  -a  'path=/tmp/test/d  src=/tmp/test/abc state=link'   等同ln -s abc d    

9 service

ansible s21 -m service  -a "name=httpd state=started enabled=true"   # started stoped restarted    

10 yum

ansible s21 -m yum -a 'name=lrzsz state=present' # present latest absent  版本號  

11 setup 收集資訊

ansible s12 -m setup    

12 lineinfile

ansible s225 -m lineinfile -a "path=/tmp/test/abc line=aaa" 匹配確保一行內容存在,否則新增。   ansible s225 -m lineinfile -a "path=/tmp/test/abc  regexp="^aaa" line=AAA" 替換,多行匹配僅對最後次。未匹配到時新增   ansible s225 -m lineinfile -a "path=/tmp/test/abc  regexp="^aadfsf" line=ZZZ backrefs=yes" 未匹配到不新增   ansible s225 -m lineinfile -a "path=/tmp/test/abc  regexp="^aaa" state=absent"   刪除所有匹配行。 (以便刪除重複再新增等操作)