代码改变世界

4 - Ansible common modules Part 2

2021-08-08 09:07  WayneWei  阅读(17)  评论(0编辑  收藏  举报
  1. Overview
    At this post we will reveal below ansible modules and get them on practice with examples
    command copy file
    user group service
    script    





  2. Ansible module - user
    [root@shell-master ~]# ansible-doc -s user
    - name: Manage user accounts

    Example: Create new user
    Run below command to create user wayne02

    [root@shell-master ~]# ansible webservers -m user -a "name=wayne02 home=/home/wayne02 shell=/bin/bash"

    After the command finished, switch to shell-node1, checking if the user created:

  3. Ansible module - group
    [root@shell-master ~]# ansible-doc -s group
    - name: Add or remove groups

    Example: Add new group
    Run below command to create new group testing-group

    [root@shell-master ~]# ansible webservers -m group -a "name=testing-group state=present"

     Switch back to shell-node1, verify if the group been created

  4. Ansible module - service
    [root@shell-master ~]# ansible-doc -s service
    - name: Manage services

    By follow the help documentation, you can use below command format to perform service management, like restart, stop etc.

    ansible webservers -m service -a "name=servicename ......"
  5. Ansible module - script
    [root@shell-master ~]# ansible-doc -s script
    - name: Runs a local script on a remote node after transferring it

    Example: Build a local script, then transfer it to remote server and execute it
    On shell-master, build a script file named testing.sh with below content (The scripts will create 1 readme.txt under /tmp/, and also create 10 data files under /tmp/):

    #!/bin/bash
    
    cd /tmp/
    # create readme.txt under
    /tmp/ cat > readme.txt << _EOF_ This is a testing script We'll create 10 data files under /tmp/ _EOF_ touch data{0..9}.data

    After create the shell script file, you need to change it's access mode, let it can be executed by using below command:

    [root@shell-master ~]# chmod 755 testing.sh
    [root@shell-master ~]# ls -lh testing.sh
    -rwxr-xr-x. 1 root root 169 Aug  8 08:53 testing.sh

    Alright, let's using Ansible to post this script to be execution on remote server. Running below command to invoke ansible script module:

    [root@shell-master ~]# ansible webservers -m script -a "testing.sh"

    Then switch back to shell-node1, let's verify if the readme.txt and data files been created or not:

  6. All good with Ansible common modules now, next let's research on Ansible playbook.