天天看點

企業運維----自動化運維工具-Ansible(二)Ansible中的常用子產品

Ansible(二):Ansible中的常用子產品

      • 1.進行主機連通性測試
      • 2.Command子產品
      • 3.Shell子產品
      • 4.Copy子產品
      • 5.Fetch子產品
      • 6.File子產品
      • 7.Archive與unarchive子產品
      • 8.hostname、Cron子產品
        • hostname子產品 修改主機名稱
        • cron子產品 設定定時任務
      • 9.yum_repository子產品
      • 10.dnf子產品
      • 11.service、firewalld子產品
        • service子產品
      • 12.user、group 子產品
      • 13.lineinfile 子產品
      • 14.replace 子產品
      • 15.setup 子產品
      • 16.debug子產品

1.進行主機連通性測試

[[email protected]_student41 ansible]$ ansible all -m ping
172.25.41.3 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
172.25.41.1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
172.25.41.2 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}

           

主機是連通狀态

2.Command子產品

常用指令

chdir       # 在執行指令之前,先切換到該目錄
executable  # 切換shell來執行指令,需要使用指令的絕對路徑
free_form   # 要執行的Linux指令,一般使用Ansible的-a參數代替。
creates     # 一個檔案名,當這個檔案存在,則該指令不執行,可以用來做判斷
removes     # 一個檔案名,這個檔案不存在,則該指令不執行
           

實驗

[[email protected]_student41 ansible]$ ansible westos -m command -a "touch /mnt/westosfile"
[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.
172.25.41.1 | CHANGED | rc=0 >>

[[email protected] mnt]# ls
westosfile
           
[[email protected]_student41 ansible]$ ansible westos -m command -a "rm -fr  /mnt/westosfile"
[WARNING]: Consider using the file module with state=absent rather than running 'rm'.
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.
172.25.41.1 | CHANGED | rc=0 >>

[[email protected] mnt]# ls
[[email protected] mnt]# 
           

chdir 進入目錄執行操作

[[email protected]_student41 ansible]$ ansible westos -m command -a "chdir=/mnt/ touch westosfile"
[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.
172.25.41.1 | CHANGED | rc=0 >>

           

creates file存在 後面的不執行,removes file存在 後面的執行

[[email protected]_student41 ansible]$ ansible westos -m command -a "creates=/mnt/westosfile touch /mnt/linuxfile"
172.25.41.1 | SUCCESS | rc=0 >>
skipped, since /mnt/westosfile exists
[[email protected]_student41 ansible]$ ansible westos -m command -a "removes=/mnt/westosfile touch /mnt/linuxfile"
[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.
172.25.41.1 | CHANGED | rc=0 >>

[[email protected] mnt]# ls
linuxfile  westosfile
           

顔色代表資訊:

綠色: 執行成功但未對遠端主機做任何更改

黃色: 執行成功并對遠端主機作改變

紅色: 執行失敗

3.Shell子產品

shell子產品可以在遠端主機上調用shell解釋器運作指令,支援shell的各種功能,例如管道等。

executable=bash 指定shell運作

[[email protected]_student41 ansible]$ ansible westos -m shell -a "ps ax |grep $$"
172.25.41.1 | CHANGED | rc=0 >>
   5108 pts/1    S+     0:00 /bin/sh -c ps ax |grep 4322
   5110 pts/1    R+     0:00 grep 4322
[[email protected]_student41 ansible]$ ansible westos -m shell -a "executable=bash ps ax |grep $$"
172.25.41.1 | CHANGED | rc=0 >>
   5293 pts/1    S+     0:00 bash -c ps ax |grep 4322
   5295 pts/1    R+     0:00 grep 4322

           

腳本運作

[[email protected]_student41 ansible]$ cat hostname.sh 
echo hello westos
[[email protected]_student41 ansible]$ ansible westos -m script -a './hostname.sh'
172.25.41.1 | CHANGED => {
    "changed": true,
    "rc": 0,
    "stderr": "Shared connection to 172.25.41.1 closed.\r\n",
    "stderr_lines": [
        "Shared connection to 172.25.41.1 closed."
    ],
    "stdout": "hello westos\r\n",
    "stdout_lines": [
        "hello westos"
    ]
}

           

4.Copy子產品

這個子產品用于将檔案複制到遠端主機,同時支援給定内容生成檔案和修改權限等。

src    #被複制到遠端主機的本地檔案。可以是絕對路徑,也可以是相對路徑。如果路徑是一個目錄,則會遞歸複制,用法類似于"rsync"
    content   #用于替換"src",可以直接指定檔案的值
    dest    #必選項,将源檔案複制到的遠端主機的絕對路徑
    backup   #當檔案内容發生改變後,在覆寫之前把源檔案備份,備份檔案包含時間資訊
    directory_mode    #遞歸設定目錄的權限,預設為系統預設權限
    force    #當目标主機包含該檔案,但内容不同時,設為"yes",表示強制覆寫;設為"no",表示目标主機的目标位置不存在該檔案才複制。預設為"yes"
    others    #所有的 file 子產品中的選項可以在這裡使用
           

複制本地檔案到遠端主機/mnt下 指定權限為1777

[[email protected]_student41 ansible]$ ls
ansible.cfg  inventory  test.yaml
[[email protected]_student41 ansible]$ ansible westos -m copy -a 'src=test.yaml dest=/mnt/test.yaml mode=1777'
172.25.41.1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "d89c19a70cb3e0ea095b90f149bdadc46ebc13cd",
    "dest": "/mnt/test.yaml",
    "gid": 0,
    "group": "root",
    "md5sum": "ad85738e99a3c6e5ec3970c6a4cfac26",
    "mode": "01777",
    "owner": "root",
    "secontext": "system_u:object_r:mnt_t:s0",
    "size": 1150,
    "src": "/home/devops/.ansible/tmp/ansible-tmp-1629959076.6076407-11254-122232893347708/source",
    "state": "file",
    "uid": 0
}

[[email protected] mnt]# ll
total 4
-rw-r--r--. 1 root root    0 Aug 26 14:10 linuxfile
-rwxrwxrwt. 1 root root 1150 Aug 26 14:24 test.yaml
-rw-r--r--. 1 root root    0 Aug 26 14:04 westosfile
           

backup=yes 修改檔案之後可以再copy會将之前的檔案備份

[[email protected]_student41 ansible]$ vim test.yaml 
[[email protected]_student41 ansible]$ ansible westos -m copy -a 'src=test.yaml dest=/mnt/test.yaml mode=1777 owner=westos backup=yes'
172.25.41.1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "backup_file": "/mnt/[email protected]:32:16~",
    "changed": true,
    "checksum": "cea0f065fbb21d3660ed12749f26c3dc1eb00438",
    "dest": "/mnt/test.yaml",
    "gid": 0,
    "group": "root",
    "md5sum": "6bbcf9b0e80d72cd6a77466961a43fea",
    "mode": "01777",
    "owner": "westos",
    "secontext": "system_u:object_r:mnt_t:s0",
    "size": 1156,
    "src": "/home/devops/.ansible/tmp/ansible-tmp-1629959533.6960654-11557-170862689266381/source",
    "state": "file",
    "uid": 1000
}

[[email protected] mnt]# ll
total 8
-rw-r--r--. 1 root   root    0 Aug 26 14:10 linuxfile
-rwxrwxrwt. 1 westos root 1156 Aug 26 14:32 test.yaml
-rwxrwxrwt. 1 westos root 1150 Aug 26 14:24 [email protected]:32:16~
-rw-r--r--. 1 root   root    0 Aug 26 14:04 westosfile
           
[[email protected]_student41 ansible]$ ansible westos -m copy -a 'dest=/mnt/westos  mode=1777 owner=westos backup=yes content="hello westos"'
172.25.41.1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "2ea0e1f1690c797cad50132d7b54dee484d428d4",
    "dest": "/mnt/westos",
    "gid": 0,
    "group": "root",
    "md5sum": "dde5c4df2e17f6693afdd6769dcf4e09",
    "mode": "01777",
    "owner": "westos",
    "secontext": "system_u:object_r:mnt_t:s0",
    "size": 12,
    "src": "/home/devops/.ansible/tmp/ansible-tmp-1629959697.664989-11721-204976592233107/source",
    "state": "file",
    "uid": 1000
}

[[email protected] mnt]# cat westos
hello westos[[email protected] mnt]# 
           

5.Fetch子產品

該子產品用于從遠端某主機擷取(複制)檔案到本地。

 

有兩個選項:

dest:用來存放檔案的目錄
 src:在遠端拉取的檔案,并且必須是一個file,不能是目錄
           

将遠端主機的檔案複制到ansible主機中

[[email protected]_student41 ansible]$ ansible westos -m fetch -a "src=/mnt/westos dest=~/.ansible"
172.25.41.1 | CHANGED => {
    "changed": true,
    "checksum": "2ea0e1f1690c797cad50132d7b54dee484d428d4",
    "dest": "/home/devops/.ansible/172.25.41.1/mnt/westos",
    "md5sum": "dde5c4df2e17f6693afdd6769dcf4e09",
    "remote_checksum": "2ea0e1f1690c797cad50132d7b54dee484d428d4",
    "remote_md5sum": null
}
[[email protected]_student41 ansible]$ cd ..
[[email protected]_student41 ~]$ cd .ansible/
[[email protected]_student41 .ansible]$ ls
172.25.41.1  cp  tmp
[[email protected]_student41 .ansible]$ cd 172.25.41.1/mnt/
[[email protected]_student41 mnt]$ ls
westos
[[email protected]_student41 mnt]$ cat westos 
hello westos[[email protected]_student41 mnt]$ 
           

flat=yes 隻負責負責檔案,但需要給定檔案名稱

[[email protected]_student41 ansible]$ ansible westos -m fetch -a "src=/mnt/westos dest=~/.ansible/westos flat=yes"
172.25.41.1 | CHANGED => {
    "changed": true,
    "checksum": "2ea0e1f1690c797cad50132d7b54dee484d428d4",
    "dest": "/home/devops/.ansible/westos",
    "md5sum": "dde5c4df2e17f6693afdd6769dcf4e09",
    "remote_checksum": "2ea0e1f1690c797cad50132d7b54dee484d428d4",
    "remote_md5sum": null
}
[[email protected]_student41 ansible]$ cd ..
[[email protected]_student41 ~]$ cd .ansible/
[[email protected]_student41 .ansible]$ ls
172.25.41.1  cp  tmp  westos
[[email protected]_student41 .ansible]$ cat westos 
hello westos[[email protected]_student41 .ansible]$ 
           

清理一下westosa的mnt目錄

6.File子產品

該子產品主要用于設定檔案的屬性,比如建立檔案、建立連結檔案、删除檔案等。

常見的指令:

force  #需要在兩種情況下強制建立軟連結,一種是源檔案不存在,但之後會建立的情況下;另一種是目标軟連結已存在,需要先取消之前的軟鍊,然後建立新的軟鍊,有兩個選項:yes|no
    group  #定義檔案/目錄的屬組。後面可以加上mode:定義檔案/目錄的權限
    owner  #定義檔案/目錄的屬主。後面必須跟上path:定義檔案/目錄的路徑
    recurse  #遞歸設定檔案的屬性,隻對目錄有效,後面跟上src:被連結的源檔案路徑,隻應用于state=link的情況
    dest  #被連結到的路徑,隻應用于state=link的情況
    state #狀态,有以下選項:
        directory:如果目錄不存在,就建立目錄
        file:即使檔案不存在,也不會被建立
        link:建立軟連結
        hard:建立硬連結
        touch:如果檔案不存在,則會建立一個新的檔案,如果檔案或目錄已存在,則更新其最後修改時間
        absent:删除目錄、檔案或者取消連結檔案

           

建立檔案

[[email protected]_student41 ansible]$ ansible westos -m file -a 'path=/mnt/westos state=touch'
172.25.41.1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "dest": "/mnt/westos",
    "gid": 0,
    "group": "root",
    "mode": "0644",
    "owner": "root",
    "secontext": "unconfined_u:object_r:mnt_t:s0",
    "size": 0,
    "state": "file",
    "uid": 0
}

[[email protected] mnt]# ls
westos
           

建立目錄

[[email protected]_student41 ansible]$ ansible westos -m file -a 'path=/mnt/westosdir state=directory'
172.25.41.1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "gid": 0,
    "group": "root",
    "mode": "0755",
    "owner": "root",
    "path": "/mnt/westosdir",
    "secontext": "unconfined_u:object_r:mnt_t:s0",
    "size": 6,
    "state": "directory",
    "uid": 0
}
[[email protected] mnt]# ls
westos  westosdir
           

建立軟連接配接

[[email protected]_student41 ansible]$ ansible westos -m file -a 'path=/mnt/westos.link state=link src=/mnt/westos'
172.25.41.1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "dest": "/mnt/westos.link",
    "gid": 0,
    "group": "root",
    "mode": "0777",
    "owner": "root",
    "secontext": "unconfined_u:object_r:mnt_t:s0",
    "size": 11,
    "src": "/mnt/westos",
    "state": "link",
    "uid": 0
}
[[email protected] mnt]# ll
total 0
-rw-r--r--. 1 root root  0 Aug 26 14:54 westos
drwxr-xr-x. 2 root root  6 Aug 26 14:56 westosdir
lrwxrwxrwx. 1 root root 11 Aug 26 14:57 westos.link -> /mnt/westos
           

建立硬連結

[[email protected]_student41 ansible]$ ansible westos -m file -a 'path=/mnt/westos.hard state=hard src=/mnt/westos'

172.25.41.1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "dest": "/mnt/westos.hard",
    "gid": 0,
    "group": "root",
    "mode": "0644",
    "owner": "root",
    "secontext": "unconfined_u:object_r:mnt_t:s0",
    "size": 0,
    "src": "/mnt/westos",
    "state": "hard",
    "uid": 0
}
[[email protected] mnt]# ll
total 0
-rw-r--r--. 2 root root  0 Aug 26 14:54 westos
drwxr-xr-x. 2 root root  6 Aug 26 14:56 westosdir
-rw-r--r--. 2 root root  0 Aug 26 14:54 westos.hard
lrwxrwxrwx. 1 root root 11 Aug 26 14:57 westos.link -> /mnt/westos
           

修改目錄權限

[[email protected]_student41 ansible]$ ansible westos -m file -a 'path=/mnt/westosdir/test state=touch'
172.25.41.1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "dest": "/mnt/westosdir/test",
    "gid": 0,
    "group": "root",
    "mode": "0644",
    "owner": "root",
    "secontext": "unconfined_u:object_r:mnt_t:s0",
    "size": 0,
    "state": "file",
    "uid": 0
}
[[email protected]_student41 ansible]$ ansible westos -m file -a 'path=/mnt/westosdir mode=777'
172.25.41.1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "gid": 0,
    "group": "root",
    "mode": "0777",
    "owner": "root",
    "path": "/mnt/westosdir",
    "secontext": "unconfined_u:object_r:mnt_t:s0",
    "size": 18,
    "state": "directory",
    "uid": 0
}
[[email protected] mnt]# ll
total 0
-rw-r--r--. 2 root root  0 Aug 26 14:54 westos
drwxrwxrwx. 2 root root 18 Aug 26 15:04 westosdir
-rw-r--r--. 2 root root  0 Aug 26 14:54 westos.hard
lrwxrwxrwx. 1 root root 11 Aug 26 14:57 westos.link -> /mnt/westos
[[email protected] mnt]# cd westosdir/
[[email protected] westosdir]# ll
total 0
-rw-r--r--. 1 root root 0 Aug 26 15:04 test
           

recurse=yes 遞歸修改目錄權限和目錄中子檔案的權限

[[email protected]_student41 ansible]$ ansible westos -m file -a 'path=/mnt/westosdir mode=777 recurse=yes'
172.25.41.1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "gid": 0,
    "group": "root",
    "mode": "0777",
    "owner": "root",
    "path": "/mnt/westosdir",
    "secontext": "unconfined_u:object_r:mnt_t:s0",
    "size": 18,
    "state": "directory",
    "uid": 0
}
[[email protected] westosdir]# ll
total 0
-rwxrwxrwx. 1 root root 0 Aug 26 15:04 test
           

7.Archive與unarchive子產品

archive壓縮

[[email protected]_student41 ansible]$ ansible westos -m archive -a 'path=/etc dest=/mnt/etc.tar.gz format=gz owner=westos mode=777'
172.25.41.1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "archived": [
        "/etc/mtab",
        "/etc/fstab",
        "/etc/crypttab",
        ......
        "/etc/smartmontools/smartd.conf",
        "/etc/smartmontools/smartd_warning.sh"
    ],
    "arcroot": "//",
    "changed": true,
    "dest": "/mnt/etc.tar.gz",
    "expanded_exclude_paths": [],
    "expanded_paths": [
        "/etc"
    ],
    "gid": 0,
    "group": "root",
    "missing": [],
    "mode": "0777",
    "owner": "westos",
    "secontext": "unconfined_u:object_r:mnt_t:s0",
    "size": 6795808,
    "state": "file",
    "uid": 1000
}
[[email protected] mnt]# ll
total 6640
-rwxrwxrwx. 1 westos root 6795808 Aug 26 15:13 etc.tar.gz
           

unarchive解壓

[[email protected]_student41 ansible]$ ansible westos -m unarchive -a 'src=/mnt/etc.tar.gz dest=/mnt/ copy=no'
172.25.41.1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "dest": "/mnt/",
    "extract_results": {
        "cmd": [
            "/bin/gtar",
            "--extract",
            "-C",
            "/mnt/",
            "-z",
            "-f",
            "/mnt/etc.tar.gz"
        ],
        "err": "",
        "out": "",
        "rc": 0
    },
    "gid": 0,
    "group": "root",
    "handler": "TgzArchive",
    "mode": "0755",
    "owner": "root",
    "secontext": "system_u:object_r:mnt_t:s0",
    "size": 35,
    "src": "/mnt/etc.tar.gz",
    "state": "directory",
    "uid": 0
}
[[email protected] mnt]# ll
total 6652
drwxr-xr-x. 131 root   root    8192 Aug 26 15:18 etc
-rwxrwxrwx.   1 westos root 6795808 Aug 26 15:13 etc.tar.gz
           

8.hostname、Cron子產品

hostname子產品 修改主機名稱

[[email protected]_student41 ansible]$ ansible westos -m hostname -a 'name=www.westos.com'
172.25.41.1 | CHANGED => {
    "ansible_facts": {
        "ansible_domain": "westos.com",
        "ansible_fqdn": "www.westos.com",
        "ansible_hostname": "www",
        "ansible_nodename": "www.westos.com",
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "name": "www.westos.com"
}
[[email protected]_student41 ansible]$ ansible westos -m shell -a 'hostname'
172.25.41.1 | CHANGED | rc=0 >>
www.westos.com
[[email protected]_student41 ansible]$ ansible westos -m hostname -a 'name=westosa'
172.25.41.1 | CHANGED => {
    "ansible_facts": {
        "ansible_domain": "",
        "ansible_fqdn": "westosa",
        "ansible_hostname": "westosa",
        "ansible_nodename": "westosa",
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "name": "westosa"
}
[[email protected]_student41 ansible]$ ansible westos -m shell -a 'hostname'
172.25.41.1 | CHANGED | rc=0 >>
westosa
           

cron子產品 設定定時任務

該子產品适用于管理cron計劃任務的。

其使用的文法跟我們的crontab檔案中的文法一緻。

常用指令:

day= #日應該運作的工作( 1-31, *, */2, )
    hour= # 小時 ( 0-23, *, */2, )
    minute= #分鐘( 0-59, *, */2, )
    month= # 月( 1-12, *, /2, )
    weekday= # 周 ( 0-6 for Sunday-Saturday,, )
    job= #指明運作的指令是什麼
    name= #定時任務描述
    reboot # 任務在重新開機時運作,不建議使用,建議使用special_time
    special_time #特殊的時間範圍,參數:reboot(重新開機時),annually(每年),monthly(每月),weekly(每周),daily(每天),hourly(每小時)
    state #指定狀态,present表示添加定時任務,也是預設設定,absent表示删除定時任務
    user # 以哪個使用者的身份執行
           

建立定時任務,每周五執行一次

[[email protected]_student41 ansible]$ ansible westos -m cron -a "job='echo hello westos' name=westoscron weekday=5"
172.25.41.1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "envs": [],
    "jobs": [
        "westoscron"
    ]
}
[[email protected]_student41 ansible]$ ansible westos -m shell -a 'cat /var/spool/cron/root'
172.25.41.1 | CHANGED | rc=0 >>
#Ansible: westoscron
* * * * 5 echo hello westos
           

删除定時任務 (注釋任務)

[[email protected]_student41 ansible]$ ansible westos -m cron -a "job='echo hello westos' name=westoscron weekday=5 disabled=yes"
172.25.41.1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "envs": [],
    "jobs": [
        "westoscron"
    ]
}
[[email protected]_student41 ansible]$ ansible westos -m shell -a 'cat /var/spool/cron/root'
172.25.41.1 | CHANGED | rc=0 >>
#Ansible: westoscron
#* * * * 5 echo hello westos
           

9.yum_repository子產品

該子產品用于軟體倉庫的配置。

實驗:配置軟體倉庫檔案 /etc/yum.repos.d/westos.repo

删除之前存在的配置檔案

[[email protected]_student41 ansible]$ ansible westos -m shell -a 'rm -fr /etc/yum.repos.d/*.repo'
[WARNING]: Consider using the file module with state=absent rather than running
'rm'.  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.
172.25.41.1 | CHANGED | rc=0 >>
           
[[email protected]_student41 ansible]$ ansible westos -m yum_repository -a 'name="AppStream" baseurl=http://172.25.41.250/rhel8.2/AppStream description=AppStream file=westos enabled=yes gpgcheck=no'
172.25.41.1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "repo": "AppStream",
    "state": "present"
}
[[email protected]_student41 ansible]$ ansible westos -m yum_repository -a 'name="BaseOS" baseurl=http://172.25.41.250/rhel8.2/BaseOS description=BaseOS file=westos enabled=yes gpgcheck=no'
172.25.41.1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "repo": "BaseOS",
    "state": "present"
}

[[email protected] yum.repos.d]# ls
westos.repo
[[email protected] yum.repos.d]# cat westos.repo 
[AppStream]
baseurl = http://172.25.41.250/rhel8.2/AppStream
enabled = 1
gpgcheck = 0
name = AppStream

[BaseOS]
baseurl = http://172.25.41.250/rhel8.2/BaseOS
enabled = 1
gpgcheck = 0
name = BaseOS
           

10.dnf子產品

該子產品主要用于軟體的安裝。

其指令如下:

name=  #所安裝的包的名稱
    state=  #present--->安裝, latest--->安裝最新的, absent---> 解除安裝軟體。
    update_cache  #強制更新yum的緩存
    conf_file  #指定遠端yum安裝時所依賴的配置檔案(安裝本地已有的包)。
    disable_pgp_check  #是否禁止GPG checking,隻用于presentor latest。
    disablerepo  #臨時禁止使用yum庫。 隻用于安裝或更新時。
    enablerepo  #臨時使用的yum庫。隻用于安裝或更新時。
           

安裝httpd

[[email protected]_student41 ansible]$ ansible westos -m dnf -a 'name=httpd state=present'
172.25.41.1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [
        "Installed: apr-util-openssl-1.6.1-6.el8.x86_64",
        "Installed: httpd-filesystem-2.4.37-21.module+el8.2.0+5008+cca404a3.noarch",
        "Installed: apr-1.6.3-9.el8.x86_64",
        "Installed: apr-util-1.6.1-6.el8.x86_64",
        "Installed: redhat-logos-httpd-81.1-1.el8.noarch",
        "Installed: apr-util-bdb-1.6.1-6.el8.x86_64",
        "Installed: httpd-tools-2.4.37-21.module+el8.2.0+5008+cca404a3.x86_64",
        "Installed: mod_http2-1.11.3-3.module+el8.2.0+4377+dc421495.x86_64",
        "Installed: httpd-2.4.37-21.module+el8.2.0+5008+cca404a3.x86_64"
    ]
}

           

解除安裝httpd

[[email protected]_student41 ansible]$ ansible westos -m dnf -a 'name=httpd state=absent'
172.25.41.1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [
        "Removed: mod_http2-1.11.3-3.module+el8.2.0+4377+dc421495.x86_64",
        "Removed: httpd-2.4.37-21.module+el8.2.0+5008+cca404a3.x86_64"
    ]
}
           

11.service、firewalld子產品

service子產品

該子產品用于服務程式的管理。

其常用指令如下:

arguments #指令行提供額外的參數
    enabled #設定開機啟動。
    name= #服務名稱
    runlevel #開機啟動的級别,一般不用指定。
    sleep #在重新開機服務的過程中,是否等待。如在服務關閉以後等待2秒再啟動。(定義在劇本中。)
    state #有四種狀态,分别為:started--->啟動服務, stopped--->停止服務, restarted--->重新開機服務, reloaded--->重載配置
           

下載下傳httpd服務

[[email protected]_student41 ansible]$ ansible westos -m dnf -a 'name=httpd state=present'
172.25.41.1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [
        "Installed: mod_http2-1.11.3-3.module+el8.2.0+4377+dc421495.x86_64",
        "Installed: httpd-2.4.37-21.module+el8.2.0+5008+cca404a3.x86_64"
    ]
}
           

開啟服務

[[email protected]_student41 ansible]$ ansible westos -m service -a 'name=httpd state=started enabled=yes'
172.25.41.1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "enabled": true,
    "name": "httpd",
    "state": "started",
    "status": {
        "ActiveEnterTimestampMonotonic": "0",
        "ActiveExitTimestampMonotonic": "0",
......
        "UnitFileState": "disabled",
        "UtmpMode": "init",
        "Wants": "httpd-init.service",
        "WatchdogTimestampMonotonic": "0",
        "WatchdogUSec": "0"
    }
}

           

開啟火牆,immediate=yes 立即開啟

[[email protected]_student41 ansible]$ ansible westos -m firewalld -a 'service=http permanent=yes state=enabled immediate=yes'
172.25.41.1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "msg": "Permanent and Non-Permanent(immediate) operation, Changed service http to enabled"
}
           

測試:編輯預設釋出目錄

[[email protected]_student41 ansible]$ ansible westos -m copy -a 'dest=/var/www/html/index.html content="hello westos"'
172.25.41.1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "2ea0e1f1690c797cad50132d7b54dee484d428d4",
    "dest": "/var/www/html/index.html",
    "gid": 0,
    "group": "root",
    "md5sum": "dde5c4df2e17f6693afdd6769dcf4e09",
    "mode": "0644",
    "owner": "root",
    "secontext": "system_u:object_r:httpd_sys_content_t:s0",
    "size": 12,
    "src": "/home/devops/.ansible/tmp/ansible-tmp-1629970235.793403-20791-168836060169032/source",
    "state": "file",
    "uid": 0
}
           
企業運維----自動化運維工具-Ansible(二)Ansible中的常用子產品

12.user、group 子產品

13.lineinfile 子產品

14.replace 子產品

15.setup 子產品

16.debug子產品