• Igor Raits @ignatenkobrain ·
    • You can merge local tmpcfg line with the next one
    • I ended up using -p "$PWD" so that behaviour wrt relative paths in ansible.cfg is same as if you would execute ansible normally. Technically, it should be doing it reversibly (reverse loop and save into same location), but that is much more complicated.
    • The exit code of local will overwrite the exit code of mktemp if you merge both lines
    f() { local foo=$(mktemp -p /bar/ ansible-XXXXXXXXXX.cfg) ; echo $? ; }; f
    mktemp: failed to create file via template ‘/bar/ansible-XXXXXXXXXX.cfg’: No such file or directory
    0
    
    f() { local foo; foo=$(mktemp -p /bar/ ansible-XXXXXXXXXX.cfg) ; echo $? ; }; f
    mktemp: failed to create file via template ‘/bar/ansible-XXXXXXXXXX.cfg’: No such file or directory
    1

    In this simple script it doesn't really care but generally I would recommend to declare and assign variables separately.


    • Well.. yes, relative paths are a problem with this version. -p $PWD will only solve this for ./ansible.cfg (in current directory). If you defined relative paths in ~/.ansible.cfg then you would need an alternative solution. I came up with this IF condition:
      if [ -f "${ANSIBLE_CONFIG}" ] && [ -w "${ANSIBLE_CONFIG%/*}" ]; then
        tmpcfg=$(mktemp -p "${ANSIBLE_CONFIG%/*}" ansible-XXXXXXXXXX.cfg)
    
      elif [ -f ./ansible.cfg ]; then
        tmpcfg=$(mktemp -p "${PWD}" ansible-XXXXXXXXXX.cfg)
    
      elif [ -f ~/.ansible.cfg ]; then
        tmpcfg=$(mktemp -p "${HOME}" ansible-XXXXXXXXXX.cfg)
    
      elif [ -f /etc/ansible/ansible.cfg ] && [ -w /etc/ansible/ ]; then
        tmpcfg=$(mktemp -p /etc/ansible ansible-XXXXXXXXXX.cfg)
    
      else
        tmpcfg=$(mktemp -p /tmp/ ansible-XXXXXXXXXX.cfg)
    
      fi

    But this will also not solve the problem if you use relative paths in multiple files and is too much for this simple workaround.
    I think -p PWD will be the most use case, so I will adjust the script above. Thanks @ignatenkobrain :)

    Edited by Robért
  • Igor Raits @ignatenkobrain ·

    You welcome, thanks for creating this. It saved me a lot of trouble this week :)

    By the way, should not you create alias for ansible-pull too?

  • Added aliases for ansible-config, ansible-console, ansible-galaxy, ansible-pull and ansible-vault too

  • Sergey @sshnaidm ·

    It doesn't merge, it just overrides. Plugins path should be appended to existing, not overwritten.

  • It merges all possible files and overwrites options if they already exist, yes. There're certainly some usecases where real merging of some options is more desired. But this can get tricky in a hurry, so it goes beyond this simple workaround.

    But you could try developing your own solution to work around your problem.

    With crudini --get /etc/ansible/ansible.cfg defaults strategy_plugins you get the current value of the strategy_plugins option. Save the output to an variable and put a loop around it. At the end set your merged value with the --set of crudini option.

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment