[PYTHON] Ansible self-made module creation-Part 2: Life just wanting to execute commands-

Previous article: Creating an Ansible self-made module-Part 1: Life you want to receive arguments-

Looking back

--Made a module template --Received multiple arguments

Just run the command

As usual, refer to the textbook Ansible module development (Python implementation), and first try run_command. View.

makefifo.py


#!/usr/bin/python
# -*- coding: utf-8 -*-

from ansible.module_utils.basic import AnsibleModule

#Main processing
#-----------------------------------------------------------
def main():
    #AnsibleModule class:Create module
    module = AnsibleModule(

        #Argument reception
        argument_spec=dict(

            #argument: path(str type,Mandatory)
            path=dict(type='str', required=True),
            #argument: owner(str type,Mandatory)
            owner=dict(type='str', required=True),
            #argument: group(str type,Mandatory)
            group=dict(type='str', required=True),
            #argument: mode(str type,Mandatory)
            mode=dict(type='str', required=True),
        ),
        #Argument check enabled
        supports_check_mode=True
    )

    #Do it without thinking
    rc, stdout, stderr = module.run_command("/bin/touch /tmp/udon.txt")

    #Return command result
    module.exit_json(
        changed=True,
        rc=rc,
        stdout=stdout,
        stderr=stderr
    )

if __name__ == '__main__':
    main()

Execution ~

$ ansible -i test_grp 192.168.56.104 -m makefifo -M library -u root -a "path=/tmp/hoge owner=root group=root mode=0644"
192.168.56.104 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "rc": 0,
    "stderr": "",
    "stderr_lines": [],
    "stdout": "",
    "stdout_lines": []
}

$ ssh [email protected] "ls -l /tmp"
Total 0
-rw-r--r--.1 root root 0 May 29 10:45 2020 udon.txt
-rw-------.1 root root 0 May 27 06:34 2020 yum.log

Good.

Command execution using arguments

In a hurry, use path and try mkfifo. I'll come back to ```owner later. By the way, I noticed that I changed the name here, "It's not" makefifo, but "mkfifo".

mkfifo.py


    #I want to use path
    rc, stdout, stderr = module.run_command("/usr/bin/mkfifo ")

Well, here ... what are you doing? Actually, it's my first time to use Python, so I don't know how to combine variables. Then, when I looked up ping.py, I used to refer to [[for beginners of python] meaning of asterisk (*) in function argument](https://dev.classmethod.jp/articles/what-does-asterisk It's written like -mean-at-args /)!

def loop2(before, *args, after):
    print(before)
    for arg in args:
        print(arg + '!')
    print(after)

I see, you can combine strings with +.

mkfifo.py


    #I want to use path
    rc, stdout, stderr = module.run_command("/usr/bin/mkfifo " + )

... well, what is this variable? I understand that it's like Hash in perl called dict type.

Ah! This was also written by Ansible module development (Python implementation)!

print '{"message_key":"%s"}' % (module.params['message']) 

That means this!

mkfifo.py


#!/usr/bin/python
# -*- coding: utf-8 -*-

from ansible.module_utils.basic import AnsibleModule

#Main processing
#-----------------------------------------------------------
def main():
    #AnsibleModule class:Create module
    module = AnsibleModule(

        #Argument reception
        argument_spec=dict(

            #argument: path(str type,Mandatory)
            path=dict(type='str', required=True),
            #argument: owner(str type,Mandatory)
            owner=dict(type='str', required=True),
            #argument: group(str type,Mandatory)
            group=dict(type='str', required=True),
            #argument: mode(str type,Mandatory)
            mode=dict(type='str', required=True),
        ),
        #Argument check enabled
        supports_check_mode=True
    )

    #I want to use path
    rc, stdout, stderr = module.run_command("/usr/bin/mkfifo " + module.params['path'])

    #Return command result
    module.exit_json(
        changed=True,
        rc=rc,
        stdout=stdout,
        stderr=stderr
    )

if __name__ == '__main__':
    main()

Now it's full! Run! !!

$ ansible -i test_grp 192.168.56.104 -m mkfifo -M library -u root -a "path=/tmp/udon_pipe owner=root group=root mode=0644"
192.168.56.104 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "rc": 0,
    "stderr": "",
    "stderr_lines": [],
    "stdout": "",
    "stdout_lines": []
}

$ ssh [email protected] "ls -l /tmp"
Total 0
-rw-r--r--.1 root root 0 May 29 10:45 2020 udon.txt
prw-r--r--.1 root root 0 May 29 10:54 2020 udon_pipe
-rw-------.1 root root 0 May 27 06:34 2020 yum.log

Alright, I'm making progress little by little. Until today.

Recommended Posts

Ansible self-made module creation-Part 2: Life just wanting to execute commands-
Ansible self-made module creation-Part 1: Life you want to receive arguments-
Ansible self-made module creation-Part 3: Life that wants to judge the necessity of change-
Ansible self-made module creation-Part 4: Life you want to make without a named pipe (complete)-
Ansible self-made module rpm operation
How to execute commands in jupyter notebook