<h5 dir="auto">ISSUE TYPE</h5>
<ul dir="auto">
<li>Bug Report</li>
</ul>
<h5 dir="auto">COMPONENT NAME</h5>
<p dir="auto">lineinfile</p>
<h5 dir="auto">ANSIBLE VERSION</h5>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="ansible 2.3.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides
"><pre class="notranslate"><code class="notranslate">ansible 2.3.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides

</code></pre></div>
<h5 dir="auto">CONFIGURATION</h5>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="[defaults]
log_path = ~/ansible/log/ansible.log
timeout = 1
ansible_managed = Ansible managed: {file} modified on %Y-%m-%d %H:%M:%S by {uid} on {host}
error_on_undefined_vars = True

[ssh_connection]
pipelining = True

[accelerate]
accelerate_port = 5099
accelerate_timeout = 30
accelerate_connect_timeout = 1.0
accelerate_multi_key = yes"><pre class="notranslate"><code class="notranslate">[defaults]
log_path = ~/ansible/log/ansible.log
timeout = 1
ansible_managed = Ansible managed: {file} modified on %Y-%m-%d %H:%M:%S by {uid} on {host}
error_on_undefined_vars = True

[ssh_connection]
pipelining = True

[accelerate]
accelerate_port = 5099
accelerate_timeout = 30
accelerate_connect_timeout = 1.0
accelerate_multi_key = yes
</code></pre></div>
<h5 dir="auto">OS / ENVIRONMENT</h5>
<p dir="auto">Ubuntu 16.04.1 LTS<br>
(last version from : <a href="https://github.com/ansible/ansible.git">https://github.com/ansible/ansible.git</a>)</p>
<h5 dir="auto">SUMMARY</h5>
<p dir="auto">When I use the lineinfile module, and when I use a variable in the line: parameter :<br>
if the variables contains "all", rhe playbook fails with : "An exception occurred during task execution. To see the full traceback, use -vvv. The error was: TypeError:  is not JSON serializable<br>
fatal: [localhost]: FAILED! =&gt; {"failed": true, "msg": "Unexpected failure during module execution.", "stdout": ""}"</p>
<h5 dir="auto">STEPS TO REPRODUCE</h5>
<p dir="auto">command line : ansible-playbook ./test_bracket_lineinfile.yml --inventory=localhost<br>
playbook : ./test_bracket_lineinfile.yml</p>
<div class="highlight highlight-source-yaml notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="---
- name: &quot;Test&quot;
  hosts: localhost
  pre_tasks:
    - name: &quot;Bug with 'all' as nextcons_ansible_group&quot;
      lineinfile:
        path: &quot;./test.txt&quot;
        line: &quot;[{{nextcons_ansible_group}}]&quot;
        create: yes
        state: present
      delegate_to: 127.0.0.1
      run_once: true
      become: no
      when: nextcons_ansible_group is defined and nextcons_ansible_group != &quot;&quot;
      vars:
        nextcons_ansible_group: 'all'
..."><pre class="notranslate">---
- <span class="pl-ent">name</span>: <span class="pl-s"><span class="pl-pds">"</span>Test<span class="pl-pds">"</span></span>
  <span class="pl-ent">hosts</span>: <span class="pl-s">localhost</span>
  <span class="pl-ent">pre_tasks</span>:
    - <span class="pl-ent">name</span>: <span class="pl-s"><span class="pl-pds">"</span>Bug with 'all' as nextcons_ansible_group<span class="pl-pds">"</span></span>
      <span class="pl-ent">lineinfile</span>:
        <span class="pl-ent">path</span>: <span class="pl-s"><span class="pl-pds">"</span>./test.txt<span class="pl-pds">"</span></span>
        <span class="pl-ent">line</span>: <span class="pl-s"><span class="pl-pds">"</span>[{{nextcons_ansible_group}}]<span class="pl-pds">"</span></span>
        <span class="pl-ent">create</span>: <span class="pl-s">yes</span>
        <span class="pl-ent">state</span>: <span class="pl-s">present</span>
      <span class="pl-ent">delegate_to</span>: <span class="pl-s">127.0.0.1</span>
      <span class="pl-ent">run_once</span>: <span class="pl-c1">true</span>
      <span class="pl-ent">become</span>: <span class="pl-s">no</span>
      <span class="pl-ent">when</span>: <span class="pl-s">nextcons_ansible_group is defined and nextcons_ansible_group != ""</span>
      <span class="pl-ent">vars</span>:
        <span class="pl-ent">nextcons_ansible_group</span>: <span class="pl-s"><span class="pl-pds">'</span>all<span class="pl-pds">'</span></span>
...</pre></div>
<h5 dir="auto">EXPECTED RESULTS</h5>
<p dir="auto">Working correctly, at when I replace 'all' by 'plop' as nextcons_ansible_group variable content :</p>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="ansible-playbook ./test_bracket_lineinfile.yml --inventory=localhost 
 [WARNING]: Host file not found: localhost

 [WARNING]: provided hosts list is empty, only localhost is available


PLAY [Test] *************************************************************************************************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************************************************************************************
ok: [localhost]

TASK [Bug with 'all' as nextcons_ansible_group] *************************************************************************************************************************************************************
ok: [localhost -&gt; 127.0.0.1]

PLAY RECAP **************************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0   "><pre class="notranslate">ansible-playbook ./test_bracket_lineinfile.yml --inventory=localhost 
 [WARNING]: Host file not found: localhost

 [WARNING]: provided hosts list is empty, only localhost is available


PLAY [Test] <span class="pl-k">*************************************************************************************************************************************************************************************************</span>

TASK [Gathering Facts] <span class="pl-k">**************************************************************************************************************************************************************************************</span>
ok: [localhost]

TASK [Bug with <span class="pl-s"><span class="pl-pds">'</span>all<span class="pl-pds">'</span></span> as nextcons_ansible_group] <span class="pl-k">*************************************************************************************************************************************************************</span>
ok: [localhost -<span class="pl-k">&gt;</span> 127.0.0.1]

PLAY RECAP <span class="pl-k">**************************************************************************************************************************************************************************************************</span>
localhost                  <span class="pl-c1">:</span> ok=2    changed=0    unreachable=0    failed=0   </pre></div>
<h5 dir="auto">ACTUAL RESULTS</h5>
<p dir="auto">The content 'all' in a variable that is used in the line: argument triggers an error.</p>
<p dir="auto">Output :</p>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="ansible-playbook ./test_bracket_lineinfile.yml --inventory=localhost 
 [WARNING]: Host file not found: localhost

 [WARNING]: provided hosts list is empty, only localhost is available


PLAY [Test] *************************************************************************************************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************************************************************************************
ok: [localhost]

TASK [Bug with 'all' as nextcons_ansible_group] *************************************************************************************************************************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: TypeError: &lt;built-in function all&gt; is not JSON serializable
fatal: [localhost]: FAILED! =&gt; {&quot;failed&quot;: true, &quot;msg&quot;: &quot;Unexpected failure during module execution.&quot;, &quot;stdout&quot;: &quot;&quot;}

NO MORE HOSTS LEFT ******************************************************************************************************************************************************************************************
	to retry, use: --limit @/home/ghusson/ownCloud/2014_entreprise_ssii/infra_informatique/99_ansible_dev/test_playbooks/test_bracket_lineinfile.retry

PLAY RECAP **************************************************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=1   "><pre class="notranslate">ansible-playbook ./test_bracket_lineinfile.yml --inventory=localhost 
 [WARNING]: Host file not found: localhost

 [WARNING]: provided hosts list is empty, only localhost is available


PLAY [Test] <span class="pl-k">*************************************************************************************************************************************************************************************************</span>

TASK [Gathering Facts] <span class="pl-k">**************************************************************************************************************************************************************************************</span>
ok: [localhost]

TASK [Bug with <span class="pl-s"><span class="pl-pds">'</span>all<span class="pl-pds">'</span></span> as nextcons_ansible_group] <span class="pl-k">*************************************************************************************************************************************************************</span>
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: TypeError: <span class="pl-k">&lt;</span>built-in <span class="pl-k">function</span> <span class="pl-en">all&gt;</span> is not JSON serializable
fatal: [localhost]: FAILED<span class="pl-k">!</span> =<span class="pl-k">&gt;</span> {<span class="pl-s"><span class="pl-pds">"</span>failed<span class="pl-pds">"</span></span>: true, <span class="pl-s"><span class="pl-pds">"</span>msg<span class="pl-pds">"</span></span>: <span class="pl-s"><span class="pl-pds">"</span>Unexpected failure during module execution.<span class="pl-pds">"</span></span>, <span class="pl-s"><span class="pl-pds">"</span>stdout<span class="pl-pds">"</span></span>: <span class="pl-s"><span class="pl-pds">"</span><span class="pl-pds">"</span></span>}

NO MORE HOSTS LEFT <span class="pl-k">******************************************************************************************************************************************************************************************</span>
	to retry, use: --limit @/home/ghusson/ownCloud/2014_entreprise_ssii/infra_informatique/99_ansible_dev/test_playbooks/test_bracket_lineinfile.retry

PLAY RECAP <span class="pl-k">**************************************************************************************************************************************************************************************************</span>
localhost                  <span class="pl-c1">:</span> ok=1    changed=0    unreachable=0    failed=1   </pre></div>
<p dir="auto">The question comes up how to distribute SSH keys to different users.</p>
<p dir="auto">In this case, suppose we have:</p>
<p dir="auto">users:<br>
- name:  xyz<br>
ssh_keys: [ /path/to/x, y, z ]<br>
- name: abc<br>
ssh_keys: [ xx, yy ]</p>
<p dir="auto">We might want to use the authorized_key module in a with_items loop, so it would be nice to say:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="with_subelements:
    - users:
    - 'ssh_keys'"><pre class="notranslate"><code class="notranslate">with_subelements:
    - users:
    - 'ssh_keys'
</code></pre></div>
<p dir="auto">And in this invocation, it would return {{ item.0 }} as each record, and {{ item.1 }} as each subelement of the given field name, as such:</p>
<ul dir="auto">
<li>authorized_key:  name={{ item.0.name }} key={{ lookup('file', item[1]) }}<br>
with_subelements:<br>
- users<br>
- 'ssh_keys'</li>
</ul>
<p dir="auto">Alternative proposals welcome.</p>
<p dir="auto">Having a sufficient number of lookup plugins for the 'odd' cases.</p>