Previous article 1: Creating an Ansible self-made module-Part 1: Life you want to receive arguments- Previous article 2: Creating ansible self-made module-Part 2: Life just wanting to execute commands- Previous article 3: Creating an Ansible self-made module-Part 3: Life that wants to judge the necessity of change-
--Made a module template (Part 1) --Received multiple arguments (1) --The argument was combined with the command and executed (Part 2) --Judging the necessity of change (Part 3)
The command to be executed and the conditions to be executed are as follows
-- mkfifo command
--When "the pipe does not exist yet" at the time of "changed" judgment
-- chown command,  chmod command
--When  changed is  True
 changed judgmentLast time,  changed = True was set uniformly when "named pipe does not exist" or "owner, permission is different", but only in the former case,  mkfifo command is required, so return I had to change the way I received the values.
mkfifo.py
: #(abridgement)
    #Check if there is a named pipe in path with the test command
    rc_mkfifo, stdout, stderr = module.run_command("test -p " + module.params['path'])
    #Check result
    if ( rc_mkfifo != 0 ):
        #Since it does not exist, it is subject to change
        changed = True
    else:
        #Owner if it exists:group:Get mode
        rc_chom, stdout, stderr = module.run_command("stat -c %U:%G:0%a " + module.params['path'])
        #Comparison. The command result is the line feed code at the end\Note that n is attached
        if ( stdout != module.params['owner'] + ':' + module.params['group'] + ':' + module.params['mode'] + '\n'):
            # owner:group:Since the mode is different, it is subject to change
            changed = True
    #End
    module.exit_json(changed=changed,stdout=stdout,stderr=stderr)
: #(abridgement)
As mentioned above, the result of test -p was changed to be included in  rc_mkfifo.
Then, it is judged whether or not the command needs to be executed.
mkfifo.py
: #(abridgement)
            # owner:group:Since the mode is different, it is subject to change
            changed = True
    #Did a named pipe exist?
    if ( rc_mkfifo != 0 ):
        #Command execution because it does not exist
        rc, stdout, stderr = module.run_command('mkfifo ' + module.params['path'])
        #If the return code is not 0, exit here
        if ( rc != 0 ):
            #Command error
            module.fail_json(msg = stderr, rc = rc)
    #Did the change occur?
    if ( changed == True ):
        #Change owner as changes are occurring
        rc, stdout, stderr = module.run_command('chown ' + module.params['owner'] + ':' + module.params['group'] + ' ' + module.params['path'])
        #If the return code is not 0, exit here
        if ( rc != 0 ):
            #Command error
            module.fail_json(msg = stderr, rc = rc)
        #Change permissions because changes have occurred
        rc, stdout, stderr = module.run_command('chmod ' + module.params['mode'] + ' ' + module.params['path'])
        #If the return code is not 0, exit here
        if ( rc != 0 ):
            #Command error
            module.fail_json(msg = stderr, rc = rc)
    #Processing Exit
    module.exit_json(changed = changed)
: #(abridgement)
Execution ~
#Confirm that the file does not exist in advance
$ 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
#Run
$ ansible -i test_grp 192.168.56.104 -m mkfifo -M library -u root -a "path=/tmp/testfifo owner=root group=root mode=0777"
192.168.56.104 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true
}
#Generation confirmation
$ ssh [email protected] "ls -l /tmp"
Total 0
prwxrwxrwx.1 root root 0 June 1 02:52 2020 testfifo
-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
#Confirm that it will be missed by re-execution
$ ansible -i test_grp 192.168.56.104 -m mkfifo -M library -u root -a "path=/tmp/testfifo owner=root group=root mode=0777"
192.168.56.104 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false
}
Congratulations.
Other contents to be added
--Branch commands for each OS and version (Factory class)
--Do not execute command during test run
I wonder what it is, but for the time being, this series is over because I made one. Ah, it was fun.
Finally, I will post the full text.
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(Mandatory,str type)
            path=dict(required=True,type='str'),
            #argument: owner(str type,Default=root)
            owner=dict(type='str',default='root'),
            #argument: group(str type,Default=root)
            group=dict(type='str',default='root'),
            #argument: mode(str type,Default=0644)
            mode=dict(type='str',default='0644'),
        ),
        #Argument check enabled
        supports_check_mode=True
    )
    #Initialize whether or not there is a change(No change)
    changed = False
    #Check if there is a named pipe in path with the test command
    rc_mkfifo, stdout, stderr = module.run_command('test -p ' + module.params['path'])
    #Check result
    if ( rc_mkfifo != 0 ):
        #Since it does not exist, it is subject to change
        changed = True
    else:
        #Owner if it exists:group:Get mode
        rc_chom, stdout, stderr = module.run_command('stat -c %U:%G:0%a ' + module.params['path'])
        #Comparison. The command result is the line feed code at the end\Note that n is attached
        if ( stdout != module.params['owner'] + ':' + module.params['group'] + ':' + module.params['mode'] + '\n'):
            # owner:group:Since the mode is different, it is subject to change
            changed = True
    #Did a named pipe exist?
    if ( rc_mkfifo != 0 ):
        #Command execution because it does not exist
        rc, stdout, stderr = module.run_command('mkfifo ' + module.params['path'])
        #If the return code is not 0, exit here
        if ( rc != 0 ):
            #Command error
            module.fail_json(msg = stderr, rc = rc)
    #Did the change occur?
    if ( changed == True ):
        #Change owner as changes are occurring
        rc, stdout, stderr = module.run_command('chown ' + module.params['owner'] + ':' + module.params['group'] + ' ' + module.params['path'])
        #If the return code is not 0, exit here
        if ( rc != 0 ):
            #Command error
            module.fail_json(msg = stderr, rc = rc)
        #Change permissions because changes have occurred
        rc, stdout, stderr = module.run_command('chmod ' + module.params['mode'] + ' ' + module.params['path'])
        #If the return code is not 0, exit here
        if ( rc != 0 ):
            #Command error
            module.fail_json(msg = stderr, rc = rc)
    #Processing Exit
    module.exit_json(changed = changed)
if __name__ == '__main__':
    main()
        Recommended Posts